Set width too 100% of window on JPanel - java

I'm currently using Luna Eclipse and Windows Builder to create a GUI.
I am using a static layout and have added a JPanel and my question is... How do i set the width of my JPanel to 100% of the window.
My code so far:
JPanel panel = new JPanel();
panel.setBackground(new Color(30, 144, 255));
panel.setBounds(0, 0, 643, 54);
panel.setSize(643,50);
frame.getContentPane().add(panel);
Using the panel.setSize() i can edit the width, as you can see i have 643. But I want to change this too 100% of the window.
Could someone please explain to me how to do this.
Thanks in advance,
Sam

Use BorderLayout:
frame.getContentPane().add(panel, BorderLayout.PAGE_START);

Related

How to have gap between the frame and a component?

I'm trying practice my GUI and I am having troubles putting gap between component and the frame.
The picture above is what I have so far. But I really want to put a gap between the left side of the frame and "label1".
private void initialize() {
frame = new JFrame();
frame.setTitle("WINDOW");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
panel = new JPanel();
panel.setLayout(new BorderLayout());
bottomPanel = new JPanel();
bottomPanel.setLayout(new GridLayout(1, 5));
l1 = new JLabel("Label1");
l2 = new JLabel("Label2");
l3 = new JLabel("Label3");
l4 = new JLabel("Label4");
l5 = new JLabel("Label5");
bottomPanel.add(l1);
bottomPanel.add(l2);
bottomPanel.add(l3);
bottomPanel.add(l4);
bottomPanel.add(l5);
panel.add(bottomPanel, BorderLayout.SOUTH);
frame.add(panel);
}
Above is part my code. I tried doing:
bottomPanel.setLayout(new GridLayout(1, 5, -20, 0));
to put some horizontal gap but that only added gap between the components. That didn't move "label1" away from the frame at all. Is there any other way of doing this? I am very new to Java so I don't really know much of the other tricks. I would appreciate any help! Thank you!
The other answers are fudges that won't achieve the desired effect when the GUI is resized. Instead use:
JLabel.setHorizontalAlignment(SwingConstants.CENTER);
By centering the text within the JLabel, combined with GridLayout stretching the components to the full width of the cell, each label will have as much space either side as the GUI can allow. E.G. here is the effect when the GUI is at minimum size.
And when stretched wider:
(The red border is added to show the bounds of each label.)
Add a Border to the panel:
bottomPanel = new JPanel();
bottomPanel.setBorder( new EmptyBorder(0, 10, 0, 0) );
Read the section from the Swing tutorial on How to Use Borders for more information about the different borders you can create.
Try the following:
bottomPanel.add(javax.swing.Box.createHorizontalStrut(10));

Add Scroll bar in JEditorPane, setLayout null

I can't add scroll bar on EditorPane.
private JEditorPane editorPane;
private JScrollPane scrollpane;
Container :
Container c = getContentPane();
c.setLayout(null);
setBounds(100, 100, 450, 300);
editorPane = new JEditorPane();
editorPane.setBounds(0, 54, 434, 208);
scrollpane = new JScrollPane(editorPane);
scrollpane.setPreferredSize(new Dimension(350, 110));
c.add(scrollpane);
..
..
Nothing added
You're shooting yourself in the foot here:
editorPane.setBounds(0, 54, 434, 208);
By setting the editorPane's absolute size, you prevent it from expanding when it needs to do so, preventing the JScrollBars from having to show.
Solution: don't do this. And yeah, avoid using null layouts. They'll bite you, as you're finding out. Set the width using CSS
getContentPane().setLayout(null);
This means "I give a damn on the help of others because I know better that anyone else how to layout a GUI!"
So this is where you are.
I'd recomment to go through the tutorials and learn how to build GUIs using LayoutManagers.

Border layout doesn't work as intended

I would like to achieve the below layout.
There are 6 panels. The 4 buttons at the top are one panel, and the 3 buttons at the right side of the image are also in one panel. Apart from those two there are 4 other panels as indicated by the borders. I tried the below code but displays everything in a scattered way.
mainPanel.add(topToolBarPanel,BorderLayout.PAGE_START);
mainPanel.add(lefsideToolBarPanel,BorderLayout.LINE_START);
mainPanel.add(descriptionPanel,BorderLayout.LEFT);
mainPanel.add(mapPanel,BorderLayout.CENTER);
mainPanel.add(propertiesPanel,BorderLayout.EAST);
mainPanel.add(tablePanel,BorderLayout.PAGE_END);
How can I achieve the design as shown in the image? I need all the panels to be arranged inside that mainPanel. I cannot use null layout though. Please advice.
After trashgod's answer :
JPanel gridPanel = new JPanel(new GridLayout(1, 0));
gridPanel.add(jInternalFrame1);
gridPanel.add(descriptionPanel);
mainPanel.add(gridPanel, BorderLayout.LINE_START);
mainPanel.add(topToolBarPanel,BorderLayout.PAGE_START);
mainPanel.add(tablePanel,BorderLayout.PAGE_END);
mainPanel.add(mapPanel,BorderLayout.CENTER);
mainPanel.add(PropertiesPanel,BorderLayout.LINE_END);
What I get :
Add lefsideToolBarPanel and descriptionPanel to a panel having GridLayout; add the new panel to the BorderLayout.
Panel p new Panel(new GridLayout(1, 0));
p.add(lefsideToolBarPanel);
p.add(descriptionPanel);
//mainPanel.add(lefsideToolBarPanel, BorderLayout.LINE_START);
//mainPanel.add(descriptionPanel, BorderLayout.LEFT);
mainPanel.add(p, BorderLayout.LINE_START);
There is no BorderLayout.LEFT. See also A Visual Guide to Layout Managers.
Addendum: Your updated question shows elements of topToolBarPanel, which should be added to PAGE_START, rather than LINE_START.
//mainPanel.add(topToolBarPanel,BorderLayout.LINE_START);
mainPanel.add(topToolBarPanel,BorderLayout. PAGE_START);
The width of the propertiesPanel and height of the tablePanel need to be increased. I used setSize()…
For the propertiesPanel, you can override getPreferredSize(), as discussed here. For the tablePanel, override getPreferredScrollableViewportSize() to customize the size of the table's enclosing JScrollPane, for example.
I suggest using a JLabel as your "layout" to use exact positioning of yout objects with setBounds(x, y, width, height). It would look similar to this :
JButton button = new JButton("Text or Image");
JLabel backgr = new JLabel();
JFrame frame = new JFrame("JLabel as Layout");
button.setBounds(100, 200, 340, 40);
backgr.add(button);
frame.add(backgr);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setLocation(40, 40);
frame.validate();
frame.setVisible(true);
I know that this is just a quick example for you, but I think it should do for explanation... so just add everything on the backgr JLabeland your good to go. Quick and dirty example but the a way to go.

Image does not appear

Im trying to use JLabel to insert a image into my GUI. However it does not appear
Here is a partial of my code
public FirstAid() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 700, 507);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
contentPane.add(tabbedPane, BorderLayout.CENTER);
panel = new JPanel();
ImageIcon imageIcon = new ImageIcon("src/method_1.png");
JLabel label = new JLabel(imageIcon);
panel.add(label);
tabbedPane.addTab("name", null, panel, null);
You create a JPanel called panel, add a JLabel to it, but do nothing with the panel variable after this. You must add it to your GUI for the JLabel and the image it might hold to be seen. I'm guessing that you want to add panel to your JTabbedPane, but without more information, I can only guess.
Edit: your edited question now shows that you're adding the JPanel to the JTabbedPane. If you're still not seeing the image, then the problem is likely in your reading in of the image. Myself, I use ImageIO.read(...) and try to read the image in as an InputStream or as a URL. Key to all of these methods is to make sure that you have the image path correct, and don't make any assumptions about how you might think that it is correct. The only way to know for sure is to test it.

JScrollPane Contents Not Showing

I have a JTextArea inside of a JPanel that is then placed into the JScrollPane. When the JPanel that contains the JScrollPane is first show the JScrollPane shows up but not the contents. As soon as the JFrame is resized the contents show up.
JTextArea area = new JTextArea(6, 20);
area.setText("Some test text");
JPanel panel = new JPanel(new BorderLayout());
panel.add(area, BorderLayout.CENTER);
JScrollPane pane = new JScrollPane();
pane.setBounds(20, 20, WIDTH - 40, 300 - 40);
pane.setPreferredSize(new Dimension(WIDTH - 40, 300 - 40));
add(pane);
pane.setViewportView(panel);
pane.setBounds(20, 20, WIDTH - 40, 300 - 40);
pane.setPreferredSize(new Dimension(WIDTH - 40, 300 - 40));
Those two lines of code doen't make sense (although they are not the cause of your problem)
The first line is used when you are using a "null layout".
The second is used when you are using layout managers.
They should not be used together.
The second is preferred since you should be using layout managers.
In the application different JPanels are swapped out in a manner similar to a slide-show. So something like this would be found in the code:
panel.remove(slide1);
panel.add(slide2);
panel.repaint();
The problem being that all of the contents of the second slide, slide2, would not show up. The solution is to add
frame.validate();
Where frame is the parent window of panel.
new JScrollPane(panel);
I believe that you need to add the panel to the scroll pane constructor.

Categories