Search code examples
javaswingmouselistenerjradiobutton

Initially hidden radioButton dont be visible on event


I'm trying to make a component visible when i clicked on a radio button. RadioButton initialized hidden in initComponents method.

I have put a mouseClickListener on radio button. It doesnt work if I initialize radioButton hidden. Works if i initialize it visible (default):

look at this

I have also tried to initialize at pre-post init.

These are myListeners and txtMaas is initialized hidden:

private void rbOgretmenMouseClicked(java.awt.event.MouseEvent evt) {                                        
    // TODO add your handling code here:
    txtMaas.setVisible(true);
}                                       

private void rbOgrenciMouseClicked(java.awt.event.MouseEvent evt) {                                       
    // TODO add your handling code here:
    txtMaas.setVisible(false);
}       

Solution

    1. Use an ItemListener on your JRadioButton, not a MouseListener.
    2. After setting a component visible or invisible, be sure to call revalidate() and repaint() on the parent container, usually a JPanel, that holds the component whose visibility you've changed. The revalidate allows the container's layout manager to re-layout its components and the repaint allows the Graphics object to re-paint the container and all its components.
    3. For more detailed help, consider creating and posting a minimal example program (please check out the link).

    e.g.,

    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    
    import javax.swing.*;
    
    public class RadioFun extends JPanel {
        private static final int PREF_W = 300;
        private static final int PREF_H = 150;
        private JRadioButton rbOgret = new JRadioButton("Ogret");
        private JTextField txtMaas = new JTextField(10);
    
        public RadioFun() {
            setLayout(new FlowLayout(FlowLayout.LEADING));
            add(rbOgret);
            add(txtMaas);
    
            txtMaas.setVisible(false);
    
            rbOgret.addItemListener(new ItemListener() {
    
                @Override
                public void itemStateChanged(ItemEvent e) {
                    rbOgretItemStateChanged(e);
                }
            });
        }    
    
        // to make the GUI large enough
        @Override
        public Dimension getPreferredSize() {
            if (isPreferredSizeSet()) {
                return super.getPreferredSize();
            }
            return new Dimension(PREF_W, PREF_H);
        }
    
        private void rbOgretItemStateChanged(ItemEvent e) {
            boolean visible = e.getStateChange() == ItemEvent.SELECTED;
            txtMaas.setVisible(visible);
    
            // call repaint and revalidate on the holding JPanel:
            revalidate();
            repaint();
        }
    
        // the two methods below are to create the GUI on the Swing event thread
        private static void createAndShowGui() {
            JFrame frame = new JFrame("RadioFun");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new RadioFun());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGui();
                }
            });
        }
    }