Z-Order with Java Swing Components - java

I have been used setComponentZOrder(Component com,int index) for JPanel(panel1) shown as below
panel1.setComponentZOrder(panel2, 0);
Here panel2 is my second panel. Now it is shown panel2 instead of panel1. I want to rearrange panel1 as original panel1. How to do it?

ZOrder is used to control the order in which child components are painted on a parent component.
If you want to display panel1, in the frame instead of panel2, then you should not be playing with ZOrder.
Instead you should be using layout managers. Specifically you should be using a CardLayout on a parent panel and then you add panel1 and panel2 to this panel. Then you can use the CardLayout to swap panels.
The basics of using a CardLayout is:
JPanel parent = new JPanel( new CardLayout() );
JPanel child1 = new JPanel();
JPanel child2 = new JPanel();
parent.add(child1, "child1");
parent.add(child2, "child2");
Then when you want to display a different panel you use code like:
CardLayout cl = (CardLayout)(parent.getLayout());
cl.show(parent, "child2");
Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples.

Related

Align JButtons in center of nested JPanel (BoxLayout)

I have a JFrame's child class and have the followiwng layout inside it. I have one big panel and one small buttonsPanel with two JButtons. I add buttons to the smaller panel and add that panel to the first one. Buttons are supposed to be centered, but it doesn't happen.
panel=new JPanel();
add(panel);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton button1=new JButton("button1");
JButton button2=new JButton("button2");
buttonsPanel=new JPanel();
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
buttonsPanel.add(button1, CENTER_ALIGNMENT);
buttonsPanel.add(button2, CENTER_ALIGNMENT);
panel.add(buttonsPanel, BorderLayout.CENTER);
What should I do?
You really need to read the Swing tutorial on Layout Managers. You need to understand what a "constraint" is and when to use it.
buttonsPanel.add(button1, CENTER_ALIGNMENT);
The buttons panel uses a BoxLayout. It does not support any constraint, so the CENTER_ALIGNMENT makes no sense.
panel.add(buttonsPanel, BorderLayout.CENTER);
Again, panel uses a BoxLayout. You can't just use a BorderLayout constraint.
The easiest way to center a component (vertically and horizontally on a frame is to use a GridBagLayout.
So the basic code might be something like:
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(button1);
buttonsPanel.add(button2);
frame.setLayout( new GridBagLayout() );
frame.add(buttonsPanel, new GridBagConstraints());
If you want to try to use a BoxLayout then you need to use "glue" before and after the panel:
Box vertical = Box.createVerticalBox();
vertical.add(Box.createVerticalGlue());
vertical.add(buttonsPanel);
vertical.add(Box.createVerticalGlue());
Again, read the tutorial for more basic information about the BoxLayout.

Outputting 2 panels on a JFrame [duplicate]

I want to add two jPanels to a JFrame side by side. the two boxes are jpanels and the outer box is a jframe
I have these lines of code. I have one class called seatinPanel that extends JPanel and inside this class I have a constructor and one method called utilityButtons that return a JPanel object. I want the utilityButtons JPanel to be on the right side. the code I have here only displays the utillityButtons JPanel when it runs.
public guiCreator()
{
setTitle("Passenger Seats");
//setSize(500, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
seatingPanel seatingPanel1 = new seatingPanel();//need to declare it here separately so we can add the utilityButtons
contentPane.add(seatingPanel1); //adding the seats
contentPane.add(seatingPanel1.utilityButtons());//adding the utility buttons
pack();//Causes this Window to be sized to fit the preferred size and layouts of its subcomponents
setVisible(true);
}
The most flexible LayoutManager I would recommend is BoxLayout.
You can do the following :
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
//panel1.set[Preferred/Maximum/Minimum]Size()
container.add(panel1);
container.add(panel2);
then add container to object to your frame component.
You need to read up on and learn about the layout managers that Swing has to offer. In your situation it will help to know that a JFrame's contentPane uses BorderLayout by default and you can add your larger center JPanel BorderLayout.CENTER and the other JPanel BorderLayout.EAST. More can be found here: Laying out Components in a Container
Edit 1
Andrew Thompson has already shown you a bit on layout managers in his code in your previous post here: why are my buttons not showing up?. Again, please read the tutorial to understand them better.

How to assign a JPanel to JFrame in netbeans gui editor?

I have a JFrame with JMenuBar at the top.
I put a JPanel on my JFrame in the middle and call it Panel1.
Next I create another class called Panel2 extended JPanel and I put some items on that.
Now on my JFrame when user chose one of the JMenuItem I want to assign Panel1 to Panel2 class which I created. Therefore I can see other panel in my JFrame somehow!
How can I do that?
In the class JFrame I have written:
JPanel Panel2 = new JPanel();
Panel1 = Panel2;
It's not working, any suggestion?
JPanel has FlowLayout as default LayoutManager, implemented in API
empty JPanel returns zero PreferredSize
you can't add one JPanel to another by Panel1 = Panel2;, then second JPanel isn't added to 1st. JPanel, is required to myPanel1.add(myPanel2)
carrefully with reserved Java words and methods names Panel is java.awt.Panel
all important informations is in Oracle tutorials A Visual Guide to Layout Managers, How to Use Panels and JPanel API
Several things.
A first remark. Better name the fields, methods, variables with an initial small letter. Very wide convention.
In the GUI editor for panel2 select "Custom creation code" and there you could type new Panel2().
To dynamically exchange a JPanel panel1 with a Panel2 panel2, overwriting the variables would have no effect; the object of panel1 was added to some swing Container of the JFrame.
Do in the action handling of the menu item:
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
remove(panel1);
panel1 = new Panel2();
add(panel1);
invalidate();
}
});
Seldom done in praxis however.

adding multiple jPanels to jFrame

I want to add two jPanels to a JFrame side by side. the two boxes are jpanels and the outer box is a jframe
I have these lines of code. I have one class called seatinPanel that extends JPanel and inside this class I have a constructor and one method called utilityButtons that return a JPanel object. I want the utilityButtons JPanel to be on the right side. the code I have here only displays the utillityButtons JPanel when it runs.
public guiCreator()
{
setTitle("Passenger Seats");
//setSize(500, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
seatingPanel seatingPanel1 = new seatingPanel();//need to declare it here separately so we can add the utilityButtons
contentPane.add(seatingPanel1); //adding the seats
contentPane.add(seatingPanel1.utilityButtons());//adding the utility buttons
pack();//Causes this Window to be sized to fit the preferred size and layouts of its subcomponents
setVisible(true);
}
The most flexible LayoutManager I would recommend is BoxLayout.
You can do the following :
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
//panel1.set[Preferred/Maximum/Minimum]Size()
container.add(panel1);
container.add(panel2);
then add container to object to your frame component.
You need to read up on and learn about the layout managers that Swing has to offer. In your situation it will help to know that a JFrame's contentPane uses BorderLayout by default and you can add your larger center JPanel BorderLayout.CENTER and the other JPanel BorderLayout.EAST. More can be found here: Laying out Components in a Container
Edit 1
Andrew Thompson has already shown you a bit on layout managers in his code in your previous post here: why are my buttons not showing up?. Again, please read the tutorial to understand them better.

How can I make JFrame resize automatically to display all buttons

I have a simple swing application which consists of a JLabel and three buttons. The three buttons are in their own JPanel which is in a JFrame along with the JLabel. The JPanel uses flowlayout manager to arrange the buttons horizontally and the JFrame uses the BorderLayout manager to arrange the JLabel and JPanel vertically.
My problem is when I launch the application, during the course of use the text on one of the buttons changes which increases its width. However, the window doesn't resize to accomdate this and one of the buttons disappears. I thought about calling pack() again, but the JFrame is a local variable in my constructor, also, I shouldn't have to tell my program to resize, right? I haven't been able to find anything on google or here to help me but there must be a simple solution, what am I missing? Code is below.
playButton = new JButton("Play");
pauseButton = new JButton("Pause");
stopButton = new JButton("Stop");
curTrackLabel = new JLabel("No Track Selected");
JFrame myFrame = new JFrame("MediaPlayer");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setTitle("MediaPlayer");
myFrame.setLocation(400,300);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
myFrame.add(topPanel);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(playButton);
buttonPanel.add(pauseButton);
buttonPanel.add(stopButton);
topPanel.add(buttonPanel, BorderLayout.CENTER);
topPanel.add(curTrackLabel, BorderLayout.NORTH);
playButton.addActionListener(new playButtonHandler());
pauseButton.addActionListener(new pauseButtonHandler());
stopButton.addActionListener(new stopButtonHandler());
myFrame.pack();
myFrame.setVisible(true);
Maybe try
((JFrame)myButton.getTopLevelAncestor()).pack();
Where myButton is the button whose text is modified during execution.
As with learning any GUI software, experimentation is best. Try messing with BorderLayouts with nested JPanels.
Ultimately, you use JPanel with a BorderLayout (Flow Layout is OK but really when resizing the window, it epically fails). See http://download.oracle.com/javase/tutorial/uiswing/layout/border.html to learn more about BorderLayouts.
Now for your layout scheme it should be something along the lines of:
Top Level Container: JFrame
JFrame contains a JPanel (Call this
JPanel 1) with a BorderLayout.
The three buttons should be in a
SEPARATE jPanel (JPanel 2). JPanel
1 should add the three buttons as
BorderLayout.CENTER. In this way,
the window will resize if the button
changes its width and/or hright.
The JLabel should be added as
BorderLayout.LINE_START.
The tutorial at: http://download.oracle.com/javase/tutorial/uiswing/layout/border.html should help you with this. But in general, use the following:
Use JPanel and nest JPanels as necessary
BorderLayout.CENTER will accomodate size changes---this is the key! (Experiment with this)
JFrame should only be used as a top level container (for more complex GUIs, this is true).
If you require more flexibility, check out JGoodies: http://www.jgoodies.com/ . This is more along the lines of creating forms.

Categories

Resources