In my JFrame, I have in each line two label
and one JComboBox
ordred as follow:
Label JComboBox Label
**Bouton**
The user can add a new line with the same entities. So, I add a bouton and when user submit a new line is created.
Label JComboBox Label
**Bouton**
Label JComboBox Label
However the position of the bouton keep the same position. How can i change the position of the bouton (at the end of the page) even if a new line is created ?
My code is bellow:
public class Display extends JFrame{
public Display(){
super("Test");
setTitle("Test");
setSize(800,800);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents();
}
private void initComponents(){
setLayout(new GridLayout(0, 3));
JPanel po = new JPanel();
JLabel label = new JLabel("Resource");
po.add(label);
add(po);
JPanel po2 = new JPanel();
po2.add(new JComboBox<>(new String[] { "option1", "option2", "option3",}));
add(po2);
JPanel po3 = new JPanel();
JLabel label3 = new JLabel("Something");
po3.add(label3);
add(po3);
//new
JPanel PanelBouton = new JPanel();
JButton bouton = new JButton(new AddResourceAction("Add new"));
add(bouton);
PanelBouton.add(bouton);
add(PanelBouton);
PanelBouton = new JPanel();
JLabel vide = new JLabel("");
PanelBouton.add(vide);
add(PanelBouton);
PanelBouton = new JPanel();
vide = new JLabel("");
PanelBouton.add(vide);
add(PanelBouton);
}
class AddResourceAction extends AbstractAction {
public AddResourceAction(String n){
super(n);
}
@Override
public void actionPerformed(ActionEvent e) {
JPanel po = new JPanel();
JLabel label = new JLabel("Resource");
po.add(label);
add(po);
JPanel po2 = new JPanel();
po2.add(new JComboBox<>(new String[] { "option1", "option2", "option3",}));
add(po2);
JPanel po3 = new JPanel();
JLabel label3 = new JLabel("Something");
po3.add(label3);
add(po3);
revalidate();
}
}
public static void main(String[] args) {
/*display panel*/
SwingUtilities.invokeLater(new Runnable() {
@Override public void run() {
new Display().setVisible(true);
}
});
}
}