I want to create a gui, which has on the top two horizontal components(a combobox and a button) and on the bottom I would like to add several components. I created everything like that:
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
public class minimumExample extends JFrame {
private JButton addItem;
private JComboBox itemBox;
private String[] itemSelect = { "test1", "test2" };
private JPanel addUpperPane;
private JPanel addLowerPane;
public void createControlPane() {
setLayout(new BorderLayout());
addUpperPane = new JPanel(new BorderLayout(5, 5));
addLowerPane = new JPanel(new GridLayout(0, 1));
addItem = new JButton("Add item");
itemBox = new JComboBox(itemSelect);
addItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getContentPane().setLayout(new GridLayout(0, 1));
if(itemBox.getSelectedItem().toString().equals("test1")) {
addLowerPane.add(new Button("Lolonator"));
validate();
repaint();
}
}
});;
addUpperPane.add(itemBox);
addUpperPane.add(addItem);
addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));
//put everything together
add(addUpperPane);
add(addLowerPane);
repaint();
}
private void makeLayout() {
setTitle("Test App");
setLayout(new BorderLayout());
setPreferredSize(new Dimension(1000, 500));
createControlPane();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
/**
* starts the GUI
*/
public void start() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
makeLayout();
}
});
}
public static void main(String[] args) throws IOException {
minimumExample ex = new minimumExample();
ex.start();
}
}
My problem is that nothing gets shown and I also thing that the layouts are not correct. Any recommendations what I should change to fix my problem?
I appreciate your answer!
UPDATE
Here is a simple wireframe of how my gui should look like:
UPDATE 2
Changing everything to:
addUpperPane.add(itemBox, BorderLayout.EAST);
addUpperPane.add(addItem, BorderLayout.WEST);
addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));
//put everything together
add(addUpperPane, BorderLayout.NORTH);
add(addLowerPane, BorderLayout.SOUTH);
Gives me that:
Any recommendations how to remove the gap?
Since you used BorderLayout
you need the specify the location of each component of the layout what you are doing is that you are only adding all the component on the same position of the layout which by default is CENTER
.
solution:
addUpperPane.add(itemBox,BorderLayout.EAST);
addUpperPane.add(addItem,BorderLayout.WEST);
addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));
//put everything together
add(addUpperPane,BorderLayout.NORTH);
add(addLowerPane,BorderLayout.SOUTH);
Also this doesn't make since setLayout(new BorderLayout());
that JFrame's default layout is already BorderLayout
so no need to set the layout to it again.
EDIT:
If you want your component to be side by side then FlowLayout
is the way to go:
addUpperPane = new JPanel(); //default component of JPanel is FlowLayout
addUpperPane.add(itemBox);
addUpperPane.add(addItemT);
EDIT number 2:
problem:
getContentPane().setLayout(new GridLayout(0, 1)); //remove it
sample:
addItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(itemBox.getSelectedItem().toString().equals("test1")) {
addLowerPane.add(new Button("Lolonator"));
revalidate();
}
}
});;