selomojemalo selomojemalo - 5 months ago 8
Java Question

Button does not create JPanel

I am new in Java and am doing some window application for my college.
I am trying to do some kind of menu with three buttons in start, and when one of buttons is clicked it's supposed to create a JPanel with two more buttons, but my code doesn't work.

Here is the code:

import java.awt.*;

public class mainScreen extends JFrame {

private JPanel contentPane;

public mainScreen() {
super("Aplikacija za atletska natjecanja");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0, 0, screenSize.width, screenSize.height);
contentPane = new JPanel();
contentPane.setBackground(SystemColor.info);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);

JPanel top = new JPanel();
top.setBounds(200, 11, screenSize.width - 400, screenSize.height - (screenSize.height - 100));
contentPane.add(top);

JPanel mainMenu = new JPanel();
mainMenu.setBounds(200, 110, screenSize.width - 400, screenSize.height - (screenSize.height - 30));
contentPane.add(mainMenu);
mainMenu.setLayout(new GridLayout(1, 0, 0, 0));

JButton btnTrkacke = new JButton("Trka\u010Dke");
btnTrkacke.setBackground(SystemColor.text);
mainMenu.add(btnTrkacke);
btnTrkacke.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

JPanel panTrk = new JPanel();
panTrk.setBounds(201, 140, (screenSize.width - 400) / 3, 30);
contentPane.add(panTrk);
panTrk.setLayout(new GridLayout(1, 0, 0, 0));

JButton btnTrkAtl = new JButton("Atleti\u010Dari");
btnTrkAtl.setBackground(SystemColor.text);
panTrk.add(btnTrkAtl);

JButton btnTrkDisc = new JButton("Discipline");
btnTrkDisc.setBackground(SystemColor.text);
panTrk.add(btnTrkDisc);
}
});

JButton btnSkakacke = new JButton("Skaka\u010Dke");
btnSkakacke.setBackground(SystemColor.text);
mainMenu.add(btnSkakacke);

JButton btnBacacke = new JButton("Baca\u010Dke");
btnBacacke.setBackground(SystemColor.text);
mainMenu.add(btnBacacke);
}
}


That same panel should also be created when I click on the other two buttons, but on other the position... Is it better to create class for that pane and then call it when button is clicked?

Answer

You're forgetting to call revalidate and repaint on the contentPane after changing its components:

contentPane.revalidate();
contentPane.repaint();

e.g.,

     @Override
     public void actionPerformed(ActionEvent e) {

        JPanel panTrk = new JPanel();
        panTrk.setBounds(201, 140, (screenSize.width - 400) / 3, 30);
        contentPane.add(panTrk);
        panTrk.setLayout(new GridLayout(1, 0, 0, 0));

        JButton btnTrkAtl = new JButton("Atleti\u010Dari");
        btnTrkAtl.setBackground(SystemColor.text);
        panTrk.add(btnTrkAtl);

        JButton btnTrkDisc = new JButton("Discipline");
        btnTrkDisc.setBackground(SystemColor.text);
        panTrk.add(btnTrkDisc);

        contentPane.revalidate();  // ***** added *****
        contentPane.repaint();     // ***** added *****
     }
  • revalidate tells the container to re-lay out its components.
  • repaint requests to the paint manager that the component and any children should be re-drawn.

As an aside: you're using null layout manager and absolute positioning with setBounds(...), and you really don't want to do this. While to a newbie this seems the best way to create complex GUI's, the more you deal with Swing GUI creation, the more you will find that doing this will put your GUI in a straight-jacket, painting it in a very tight corner and making it very hard to extend or enhance. Just don't do this.