I try to make multiple lines which fill the whole frame at vertical and horizontal. I tried it with a GridLayout with a GridBagLayout and now with the BoxLayout - the same problem ever! White lines by resizing...
I dont know which LayoutManager I should use, because all have the same problem! I want a responsive Layout so I need to use a LayoutManager and can't fix the elements. Can anybody help me?
Container parentPane = frame.getContentPane();
JPanel parentPanel = new JPanel();
parentPane.add(parentPanel);
parentPanel.setLayout( new javax.swing.BoxLayout(parentPanel, javax.swing.BoxLayout.Y_AXIS));
JPanel[] parentPanelFields = new JPanel[6];
for(int i = 0; i < 6; i++) {
parentPanelFields[i] = new JPanel();
parentPanelFields[i].setBackground(Color.RED);
if(i != 5) parentPanelFields[i].setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.WHITE));
parentPanel.add (parentPanelFields[i]);
}
The result of the code is perfect, but on resizing the frame a big white line fade in at the bottom of the frame...
Perfect result, but with a big bold line at the bottom:
This is my first question with the same problem, but with the GridBagLayout: https://stackoverflow.com/questions/35384891/gridbaglayout-shows-white-lines-at-top-and-bottom?noredirect=1#comment58475366_35384891
How can I make these lines in Y-AXIS without these white lines by resizing the frame?
Please help me - I despair for 3 days with this problem...
A big thank you, Waldi
EDIT1: The whole code:
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class test {
public static void addComponentsToPane(JFrame frame) {
Container parentPane = frame.getContentPane();
JPanel parentPanel = new JPanel();
parentPane.add(parentPanel);
parentPanel.setLayout( new javax.swing.BoxLayout(parentPanel, javax.swing.BoxLayout.Y_AXIS));
JPanel[] parentPanelFields = new JPanel[6];
for(int i = 0; i < 6; i++) {
parentPanelFields[i] = new JPanel();
parentPanelFields[i].setBackground(Color.RED);
if(i != 5) parentPanelFields[i].setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.WHITE));
parentPanel.add (parentPanelFields[i]);
}
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("GridBagLayout Source Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
EDIT2: The solution from camickr:
First download the class RelativeLayout from https://tips4java.wordpress.com/2008/11/02/relative-layout/ (bottom of the page)
Second step is to write the code for your problem -> in my case:
RelativeLayout rl = new RelativeLayout(RelativeLayout.Y_AXIS);
rl.setFill( true );
JPanel somePanel = new JPanel();
somePanel.setBackground(Color.BLACK);
somePanel.setLayout( rl );
JPanel blue = new JPanel();
blue.setBackground(Color.BLUE);
somePanel.add(blue, new Float(1));
parentPane.add(somePanel);
JPanel green = new JPanel();
green.setBackground(Color.GREEN);
somePanel.add(green, new Float(1));
parentPane.add(somePanel);
JPanel red = new JPanel();
red.setBackground(Color.RED);
somePanel.add(red, new Float(1));
parentPane.add(somePanel);
Use the RelativeLayout like other LayoutManagers!
If you have 6 panels that are added to a frame and the frame is packed then each panel will display as expected.
If you increase the frame size by 5 pixels there is not enough space to give each panel an extra pixel so you have 5 extra pixels at the bottom as the layout manager does not know how to allocate these extra pixels to the components.
If you then expand the frame 1 more pixel then each panel will receive an extra pixel and the space will go away.
You could try using the Relative Layout. This layout will allocate these extra pixels to 1 or more components depending of the property you set for the layout manager.
Also using the Relative Layout
there will be no need to add a Border to each component. You can just set the "gap property" to leave 1 pixel between each component. The background of the parent panel will then show through.
Edit:
The basic code would be:
RelativeLayout rl = new RelativeLayout(RelativeLayout.Y_AXIS);
rl.setFill( true ); // to fill horizontal width
rl.setGap( 1 ); // so you don't need the border
somePanel.setLayout( rl );
Then in the loop:
somePanel.add(someComponent, new Float(1.0f));