1


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
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Raziel
  • 53
  • 1
  • 8
  • See [How to Use CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). IMHO it perfectly suits your problem. – dic19 Sep 03 '14 at 12:55
  • Because your guide use javax.swing.SwingUtilities.invokeLater(new Runnable()? – Raziel Sep 03 '14 at 13:17
  • Because of this: [Initial Threads](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) – dic19 Sep 03 '14 at 13:20
  • @dic19 i have update my post. I already use Thread in main class. You think i need use it again in TF class? – Raziel Sep 03 '14 at 13:28
  • Sorry but I didn't undesrtand your last update. Please see my answer. Hope it helps! :) – dic19 Sep 03 '14 at 14:11

1 Answers1

4

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.
Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69