Question

I'm developing a Java application for homework. This is my code

 JLabel queryHandlerL = new JLabel("Create php to handle query results", JLabel.CENTER);
 final JCheckBox queryHandlerCB = new JCheckBox();
 JPanel checkBoxPanel = new JPanel(new FlowLayout());
 checkBoxPanel.add(queryHandlerL);
 checkBoxPanel.add(queryHandlerCB);
 
 // Query Panel
 // set image
 picLabelQuery = new JLabel("",JLabel.LEFT);
 picLabelQuery.setIcon(currentPicForm);
 
 JPanel queryPanel = new JPanel();
 final JButton queryButton = new JButton("Insert a query");
 
 queryPanel.add(queryButton);
 queryPanel.add(picLabelQuery);
 
 // Panel create
 final JButton createButton = new JButton("Create");
 JPanel createPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
 createPanel.add(createButton);
 
 
 JPanel finalPanel = new JPanel(new GridLayout(0, 1,5,2)); 
 finalPanel.add(queryPanel);
 finalPanel.add(checkBoxPanel);
 finalPanel.add(createPanel);
 
 finalPanel.setBorder(BorderFactory.createTitledBorder("SQL connection"));

 setLayout(new GridBagLayout());
 add(finalPanel); 

I have a CardLayout and this is a Window inside this CardLayout. The last add(finalPanel) refers to the panel of the CardLayout.

This piece of code works but this is the result enter image description here

How do I remove the space that is automatically created between the panels?

Was it helpful?

Solution

How do I remove the space that is automatically created between the panels?

Use a different layout manager for the panel. The GridLayout will always resize components to take up all the space in the panel.

Maybe you can use a BoxLayout or a GridBagLayout. You can also nest panels with different layout managers to get your desired effect.

Read the section from the Swing tutorial on Layout Managers for more information and examples.

OTHER TIPS

You should pack() your surrounding panel or set the height to a desired value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top