Java add more than one JPanel objects into JFrame - java

I am a beginer and I dont know how to add more objects into JFrame.
How could I add more than one JPanel objects into JFrame?
Below is what I have tried.
Thanks for your help.
public class Init extends JFrame{
public Init(){
super("Ball");
Buttons t = new Buttons();
JumpingBall b1 = new JumpingBall();
JumpingBall b2 = new JumpingBall();
t.addBall(b1);
t.addBall(b2);
add(b1);
add(b2);
setSize(500,500);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
}

Assuming that JumpingBall extends JPanel, you might want to have a look at the java layout managers here: Link.
The default Layout for a JFrame is the BorderLayout and if you didn't specify where you want to add your component, The BorderLayout will put it in the center by default. In BorderLayout, you cannot have more that one component in the same area. So, in your example you will end up having only the second JumpingBall panel in your frame. If you want to have more than one component at the center, then you will have to create a JPanel and add those components to it using different Layout. The common three Layouts are the BorderLayout, FlowLayout and GridLayout Please have a look at the provided link above to see how the components are arranged.

You can add a number of JPanel objects in a JFrame, using the add method. If only one is displayed, you might need to change your Layout options or use a Layout Manager (Look here for more).

You are seeing only one because it overlapping each other. Just provide setbound(x,y,x1,y1) for you panel component and you will see your panel at location.
or use setLayout(new FlowLayout()); which is going to order your component in respective to other so you will not override each-other.

Related

I can't to get Label and Buttons components in created window

I tryed to learn about GUI and tryed to create the window with two buttons and jne Label on the screen. But I don't understand why I can't see these elements simultaneously. When I comment out lines for buttons I can see the Label element.
Here is my code:
import java.awt.*;
import javax.swing.*;
public class MyWin {
public static void main(String[] args) {
JFrame w = new JFrame("My Window");
w.setSize(1000,800);
w.setVisible(true);
JButton b = new JButton("My button");
b.setVisible(true);
b.setSize(150, 100);
b.setLocation(500, 20);
JButton b2 = new JButton("Second button");
b2.setVisible(true);
b2.setSize(150,100);
b2.setLocation(500, 600);
JLabel l = new JLabel("My label");
l.setVisible(true);
w.getContentPane().add(b);
w.getContentPane().add(b2);
w.getContentPane().add(l);
}
}
The default layout for the JFrame is BorderLayout and when you add your JLabel through single parameter add method you add it with a BorderLayour.CENTER constraint as a default, this causes to fill all the available space. So you might want to use layout manager suitable for your needs, then the components won't overlay themselves.
Visual Guide to Layour Managers
First of all, JFrame uses BorderLayout as a default layout and just adding the components (w.getContentPane().add(b)) sets them in BorderLayout.CENTER; where they occupy the whole JFrame to fill the empty space. Thus, is recommended to add components in a JPanel. So, you should create first a JPanel, add the components to the JPanel and finally add it to the JFrame.
The setSize(...); statement is not applied due to the default layout (FlowLayout) in JPanels and also is discouraged. (Because it won't work properly in different computers with different screen resolutions)
If you want to change the size of the components you should change the default layout and use instead a customLayout, borderLayout, gridLayout...
If you want to understand deeply how layouts work and all the available layouts in Java check this

Store components in ArrayList

I want to store components like JButton, JTextField, JTextArea etc. all in same ArrayList and later loop through it and add every component to JFrame. I tried storing them in ArrayList but when I looped through it and added every component it included to my frame, nothing showed up in frame. Does anyone know how this can be done? Thanks in advance.
go ahead with this:
public class Example {
public static void main(String[] args) {
JFrame frame = new JFrame();
List<Component> components = new ArrayList<Component>();
components.add(new JButton("test1"));
components.add(new JButton("test3"));
components.add(new JButton("test3"));
frame.setLayout(new FlowLayout());
for(Component component: components)
frame.getContentPane().add(component);
frame.pack();
frame.setVisible(true);
}
}
add a layout manager to your frame
call pack() to resize the frame according to your components
set the frame visible
Try declaring the ArrayList like this:
List<JComponent> = new ArrayList<JComponent>();
The above works because JComponent is a common ancestor for JButton, JTextField, JTextArea - that is, it's a super class common to all JComponents.
It's not clear to me: why do you want to add the components first to an ArrayList? add them directly to the JFrame.
Do you know how to use Layout Managers?
A layout manager is an object that implements the LayoutManager interface* and determines the size and position of the components within a container. Although components can provide size and alignment hints, a container's layout manager has the final say on the size and position of the components within the container.
Using Layout Managers

Why do the JList and TextArea components not resize using a GridBagLayout?

http://pastebin.com/VaaTRsuf
I would like to have the JList and JTextArea resize with the window, but the JPanel stays in the center.
Your LogView class extends JPanel and thus unless you change it, it uses JPanel's default layout, FlowLayout. Components held in a FlowLayout-using container do not change size when the container changes size, and so if you want this behavior, you don't need a component Listener -- you just need to change the layout manager for the LogView JPanel to BorderLayout or something similar that allows its held component to expand, that's it. One line of code:
public LogView(final JFrame contentPane) {
// .......
setLayout(new BorderLayout()); // add this, that's it
add(mainPanel);
}
Another option is to get rid of mainPanel as it doesn't appear to be necessary at all, to set the layout of your LogView object to be GridBagLayout and to add your components directly to the LogView object.

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.

Why does BorderLayout overwrite components when new ones are added?

BorderLayout does something strange. If I add two panels to a Container with the same constraint (BorderLayout.CENTER for instance), then the first one goes away, even if the second one is deleted or made invisible
It seems as though it would make sense for it to "stack" each element on top of the previous ones.
Is this correct and by design? If so, is there some documentation on it?
Has anyone else been frustrated by it? Have you a solution, such as a custom LayoutManager?
Sample code:
JFrame frame = new JFrame();
frame.setSize(500, 500);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.blue);
frame.getContentPane().add(panel1);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.red);
frame.getContentPane().add(panel2);
panel2.setVisible(false); // Seems like it should allow us to see panel1.
frame.setVisible(true);
This creates and displays a 500x500 blank box.
BorderLayout was simply not designed to do what you want. Separation of responsibility. If you want that behavior you should compose: combine the BorderLayout with a CardLayout. Though for the actual stack behavior, you'll have to code something yourself (or find someone who already has.)
Is this correct and by design?
Yes.
You need to understand the basics of how layout managers work. One of the jobs of the layout manager is to set the "location" and "size" of the components added to the panel. In the case of a BorderLayout it only tracks 5 components so only the last component added to the CENTER is known by the layout manager.
Layout management is not done when components are added to the panel. It is done when the frame is packed, or made visible (or the revalidate() method is invoked) . In this case the blue panel is not part of the components managed by the BorderLayout so its size remains (0, 0), which means there is nothing to paint.
Try changing your code to:
JPanel panel1 = new JPanel();
panel1.setSize(200, 200);
and you will see the blue panel painted at the specified size.
Now try commenting out:
//panel2.setVisible(false);
and you will see both panels. This is because as components are added to the panel they are assigned a ZOrder. Basically the last component added is painted first, which is why the blue panel is painted on top of the red panel. Check out the setComponentZOrder() method of the Container class for more information.
The CardLayout is probably the layout manager you should be using, but you can check out the Overlap Layout as well.

Categories

Resources