Creating Java Panels with BorderLayout

  • Context: Java 
  • Thread starter Thread starter Chrono
  • Start date Start date
  • Tags Tags
    Java
Click For Summary

Discussion Overview

The discussion revolves around a Java programming issue related to creating a JFrame with two panels using BorderLayout and adding buttons to those panels. Participants are exploring the correct structure and method calls necessary to ensure the GUI components display properly.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant presents a Java code snippet for creating a JFrame and adding panels and buttons, but notes that the buttons do not display when the program runs.
  • Another participant questions the placement of the GUI setup code in a method that may not be called automatically, suggesting it should be moved to the main method.
  • There is a discussion about the necessity of having a constructor with the same name as the class, with some participants suggesting that the setup code should be placed in the constructor instead of a separate method.
  • Some participants propose that the method ShowBorderLayout() should be explicitly called from the main method or the constructor to ensure it executes.
  • One participant suggests that the issue may stem from a misunderstanding of method calls in Java, emphasizing that user-defined methods do not execute automatically.
  • There are suggestions to rename the method to match the constructor name, which would allow the code to run when an object of the class is created.

Areas of Agreement / Disagreement

Participants generally agree that the method ShowBorderLayout() needs to be called explicitly for the GUI setup to work, but there are differing opinions on whether to keep it as a separate method or integrate it into the constructor. The discussion remains unresolved regarding the best approach to structure the code.

Contextual Notes

Some participants reference potential copy-and-paste errors and the implications of method naming conventions in Java, highlighting the importance of understanding how constructors and methods interact in the context of object creation.

Chrono
Messages
424
Reaction score
3
Ok, guys, I need some help here. This was done using Java, by the way. One of the homework questions we had a while ago was to create a frame and within the frame create two panels and on the two panels create three buttons using BorderLayout. Here's what I had:

import javax.swing.*;
import java.awt.*;

public class NO1HW2 extends JFrame
{
public void ShowBorderLayout()
{
Container mycontainer = getContentPane();

mycontainer.setLayout(new BorderLayout(10,10));

JPanel p1 = new JPanel();

for(int i = 1; i <= 3; i++)
p1.add(new JButton("Button " + i));

JPanel p2 = new JPanel();

for(int i = 4; i <= 6; i++)
p2.add(new JButton("Button " + i));

mycontainer.add(p1, BorderLayout.CENTER);

mycontainer.add(p2, BorderLayout.SOUTH);
}


public static void main(String args[])
{
NO1HW2 frame = new NO1HW2();
frame.setTitle("Show BorderLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);

}
}

When I run the program the frame will pop up but that's it. The panels or buttons won't show. I asked my professor what the problem was and after making sure I set the frame visible to true he didn't know what else was wrong. Do y'all see anything that can cause it to not show the buttons?
 
Technology news on Phys.org
I'm not really up to date with Java (I parted ways with it before Java 1.2), but why are you writing your code in the ShowBorderLayout method? I don't even know if it's called automatically or not. Try moving all that code into the main method, before the call to setVisible.
 
I think you started from a program which started something like this

import javax.swing.*;
import java.awt.*;

public class ShowBorderLayout extends JFrame
{
public ShowBorderLayout ()
{


and then changed the name of the class. The ShowBorderLayout () is the constructor and has to have the same name as the class.
 
chronon,

No. The name of the class is NO1HW2, and ShowBorderLayout() is a public member method of the class. The class has a default constructor.

Chrono,

The problem is that your code to setup the JFrame is in a method, ShowBorderLayout(), which is never called. You should either call this method from main() after creating the NO1HW2 object, or you should move the setup code into the class's constructor, which would be declared as simply "public NO1HW2() {...}"

- Warren
 
Greetings all, and permit me to shed some light on this issue. There is no default method called ShowBorderLayout within Swing that one could try to override in this manner. Setting that hypothesis aside, this is probably a copy-and-paste error where the name of the class was changed, but not the name of the constructor, as chronon suggested. My guess is that the old constructor originally failed to compile (since constructors do not return anything), and the return type was added to void.

In fact, a Google search quickly reveals some candidates for the original class:
http://cspeech.ucd.ie/~fred/teaching/oldcourses/cs1001/lectures/ShowBorderLayout.java
http://www.utdallas.edu/~csteinh/CS%201337/examples/ShowBorderLayout.java

... and possibly others. There are any numbers of solutions, some more elegant than others. The method can be called explicitly from main , eg
NO1HW2 frame = new NO1HW2();
frame.ShowBorderLayout();
...
... but having a separate class method for this task seems superfluous - unless of course it would be too costly to perform this operation every time an object is generated, which is not the case here. The ShowBorderLayout() method can be converted to the class constructor (chroot), or the code in the method can be moved into the main method (zefram_c). For my part, I would go along with chroot's approach as it seems the most natural way to encapsulate the code. Farewell.
 
Last edited by a moderator:
chroot said:
The problem is that your code to setup the JFrame is in a method, ShowBorderLayout(), which is never called. You should either call this method from main() after creating the NO1HW2 object, or you should move the setup code into the class's constructor, which would be declared as simply "public NO1HW2() {...}"

I thought it was called from the line "NO1HW2 frame = new NO1HW2();".
 
Chrono said:
I thought it was called from the line "NO1HW2 frame = new NO1HW2();".
What makes you think that? The ShowBorderLayout() method is not a standard method or anything -- it's just one you made up. It won't be called unless you explicitly call it.

- Warren
 
chroot said:
What makes you think that? The ShowBorderLayout() method is not a standard method or anything -- it's just one you made up. It won't be called unless you explicitly call it.

I guess I should have realized that since it wasn't working. How would I call it?
 
As has been said already, you can either call it from main as "frame.ShowBorderLayout()", or by calling it explicitly from the NO1HW2 class's constructor, like so:

PHP:
public NO1HW2() {
   ShowBorderLayout();
}

- Warren
 
  • #10
How's this for a solution, Warren? Take out the void and rename the ShowBorderLayout() to NO1HW2().

import javax.swing.*;
import java.awt.*;

public class NO1HW2 extends JFrame
{
public void ShowBorderLayout()
{
}
 
  • #11
Yes, Chrono, that would do fine. All you'd be doing is putting that code into the NO1HW2's constructor, rather than in a method called "ShowBorderLayout()." The code in the constructor will be run automatically when the NO1HW2 object is created.

- Warren
 
  • #12
chroot said:
Yes, Chrono, that would do fine. All you'd be doing is putting that code into the NO1HW2's constructor, rather than in a method called "ShowBorderLayout()." The code in the constructor will be run automatically when the NO1HW2 object is created.

And it works! Thanks, Warren, I appreciate your help with this.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
2K
  • · Replies 14 ·
Replies
14
Views
6K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K