0

I am trying to toggle visibility of a JTextField with a checkbox. If the checkbox is selected I want the JTextField to be displayed and vice-versa. My program works fine until I add the line that initializes the JTextField to be invisible at the start. If I remove this the segment works fine! Can you help me?

final JCheckBox chckbxNewCheckBox_1 = new JCheckBox("New Folder");
        panel_3.add(chckbxNewCheckBox_1);

        final JTextField textField_3 = new JTextField();
        panel_3.add(textField_3);

        textField_3.setColumns(20);
        //textField_3.setVisible(false); if a comment it in.. it never becomes visible

        chckbxNewCheckBox_1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                if(chckbxNewCheckBox_1.isSelected()){
                    textField_3.setVisible(true);
                }
                else 
                    textField_3.setVisible(false);
            }
        });
user1803551
  • 12,965
  • 5
  • 47
  • 74
JmRag
  • 1,443
  • 7
  • 19
  • 56
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) For many (or one & 'none') components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). 3) For a `JCheckBox`, use an `ActionListener` rather than a `MouseListener`. – Andrew Thompson May 04 '14 at 14:34

4 Answers4

2

Try with ActionListener instead of MouseListener

checkBox.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        textField_3.setVisible(checkBox.isSelected());
    }
});

--EDIT--

call panel_3.revalidate(); after changing its visibility.

Braj
  • 46,415
  • 5
  • 60
  • 76
  • It doens't work either... If I change the setVisible with the setEditable it works. – JmRag May 04 '14 at 15:09
  • both are different methods. Don't know what's wrong with your code because your code is not visible to us. – Braj May 04 '14 at 15:10
  • If it works with `setEditable()` then call this one also along with `setVisible()`. – Braj May 04 '14 at 15:11
  • I know it should work but it doesn't :P This is all the code that is related about. – JmRag May 04 '14 at 15:18
  • For more info have a look at [Java Swing revalidate() vs repaint()](http://stackoverflow.com/questions/1097366/java-swing-revalidate-vs-repaint) – Braj May 04 '14 at 15:27
1

When an element is invisible during container initialization, it never gets its dimensions initialized. You can check it by calling getWidth() and getHeight() on the text area after you set it to visible. They're both zero. So follow @Braj edit and call panel.revalidate() after you change element visibility to let layout manager know that it's time to reposition/recalculate some elements and give them proper size.

Denis Tulskiy
  • 19,012
  • 6
  • 50
  • 68
0

You will do better with ItemListener

chckbxNewCheckBox_1.addItemListener(new ItemListener() {

    @Override
    public void itemStateChanged(ItemEvent e) {

        if (e.getStateChange() == ItemEvent.DESELECTED))
            textField_3.setVisible(false);
        else if (e.getStateChange() == ItemEvent.SELECTED))
            textField_3.setVisible(true);
        textField_3.revalidate();
    }
});

Note: pelase follow naming conventions and use underscores only for constants.

user1803551
  • 12,965
  • 5
  • 47
  • 74
-1

Consider calling pack() method

Below is the complete code I experimented with:

import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Test {

    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout());

        final JCheckBox chckbxNewCheckBox_1 = new JCheckBox("New Folder");
        final JPanel panel_3 = new JPanel();
        frame.add(panel_3);

        panel_3.add(chckbxNewCheckBox_1);

        final JTextField textField_3 = new JTextField();
        panel_3.add(textField_3);

        textField_3.setColumns(20);
        textField_3.setVisible(false); //if a comment it in.. it never becomes visible

        chckbxNewCheckBox_1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                if (chckbxNewCheckBox_1.isSelected()) {
                    textField_3.setVisible(true);
                } else
                    textField_3.setVisible(false);

                frame.pack();
            }
        });

        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}
ka3ak
  • 2,435
  • 2
  • 30
  • 57