How to refresh JPanel when JRadioButton is select?

坚强是说给别人听的谎言 提交于 2019-12-24 07:09:15

问题



i have this class:

public class TF extends JPanel implements ActionListener
{
    private ButtonGroup buttonGroup = new ButtonGroup();
    private JRadioButton rdbtnFilm;
    private JRadioButton rdbtnSerieTv;
    private JPanel pnlGeneral;
    private JPanel pnlRadioButton;
    private JLabel lblTitolo;
    private JTextField tfTitolo;

    public TF() 
    {   
        pnlGeneral = new JPanel();
        pnlRadioButton = new JPanel();

        rdbtnFilm = new JRadioButton("Film");
        rdbtnFilm.addActionListener(this);
        pnlRadioButton.add(rdbtnFilm);
        buttonGroup.add(rdbtnFilm);

        rdbtnSerieTv = new JRadioButton("Serie Tv");
        rdbtnSerieTv.addActionListener(this);
        pnlRadioButton.add(rdbtnSerieTv);
        buttonGroup.add(rdbtnSerieTv);

        pnlGeneral.add(pnlRadioButton, gbc_panel_1);
        this.add(pnlGeneral);
    }

    public void actionPerformed(ActionEvent e) 
    {
        if(rdbtnFilm.isSelected())
        {
            lblTitolo = new JLabel("Titolo");
            pnlGeneral.add(lblTitolo, gbc_lblTitolo);

            tfTitolo = new JTextField();
            tfTitolo.setColumns(10);
            tfTitolo.setText("1");
            pnlGeneral.add(tfTitolo, gbc_tfTitolo);
        }

        if(rdbtnSerieTv.isSelected())
        {
            lblTitolo = new JLabel("Titolo");
            pnlGeneral.add(lblTitolo, gbc_lblTitolo);

            tfTitolo = new JTextField();
            tfTitolo.setColumns(10);
            tfTitolo.setText("1");
            pnlGeneral.add(tfTitolo, gbc_tfTitolo);
        }
    }
}

but when i select a radio button just sometimes JPanel show me JLabel and JTextfield. I don't know if i need use revalidate(), repaint() or something else to refresh JPanel. This happens because Swing isn't thread-safe?

Update

public class Main
{
    public static void main(String[] args) throws IOException
    {
        SwingUtilities.invokeLater(new Runnable() 
        {
              public void run() 
              {
                  JFrame gui=new GUI();
                  gui.setVisible(true);
                  gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  gui.setSize(800,720);
              }
        });
    }
}


public class GUI extends JFrame implements MouseListener
{
    private boolean selected=true;
    public GUI()
    {   
        //Creazione del Menu
        JMenuBar menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);

        JMenu menu = new JMenu("Switcha");
        menuBar.add(menu);
        menu.addMouseListener(this);
        this.add(new TF());
    }

    public void mouseClicked(MouseEvent e)
    {
        if(selected)
        {
            this.getContentPane().removeAll();
            this.getContentPane().add(new TF());
            // do something else
        }
        else
        {
            this.getContentPane().removeAll();
            this.getContentPane().add(new TF2());
            // do something else
        }
    }
}

回答1:


This happens because Swing isn't thread-safe?

No, this happens because you are adding components to an already displayed container, causing the components hierarchy be invalidated. See Container#add(Component comp) docs.

I don't know if I to need use revalidate(), repaint() or something else to refresh JPanel.

Yes, as per previous point. The tipical sequence in this case is:

panel.add(someComponent);
panel.revalidate();
panel.repaint();

Is there another way to accomplish this?

Yes, it is. As I've stated in my comment, IMHO CardLayout is the proper way to go through. See How to Use CardLayout tutorial.

In a nutshell:

  • Keep your panel with radio buttons.
  • Have two different panels: one for films and another one for TV series.
  • Have another JPanel with CardLayout.
  • Add these two panels (or many as needed) as cards.
  • On radio buttons change just switch the right "card" for the card layout panel.


来源:https://stackoverflow.com/questions/25644778/how-to-refresh-jpanel-when-jradiobutton-is-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!