Reputation: 1543
I'm using netbeans 8. I have 2 radio button that I want to hide when the frame is shown. How can I do that? I successfully do that when I click other button such as this:
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jRadioButton3.setVisible(false);
jRadioButton4.setVisible(false);
}
but that's not what I want. I want to set it to invisible and only show it when I click other radio button. For some reason netbean prevent me from editing certain area in my source code so I can't test or explore it. Please help and thanks in advance.
Upvotes: 0
Views: 4002
Reputation: 3103
You can set the radio button to invisible when you add it to the frame and make it visible on some event:
public class InvisibleRadioButton {
public static void main(String[] args) {
JFrame frame = new JFrame();
final JRadioButton jRadioButton1 = new JRadioButton("1");
JRadioButton jRadioButton2 = new JRadioButton("2");
frame.setLayout(new FlowLayout());
frame.add(jRadioButton1);
frame.add(jRadioButton2);
frame.setVisible(true);
jRadioButton1.setVisible(false); // initialize as invisible
jRadioButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jRadioButton1.setVisible(true); // set it to be visible
}
});
frame.pack();
}
}
Upvotes: 2
Reputation: 22005
Set the JRadioButton
setVisible
method as false
by default and then change it when an action is performed.
For example, here under, the JRadioButtons
will be visible once the first JRadioButton
is selected. If it is deselected, they disappear.
I did it with a JRadioButton
but it can be done with other components of course.
public class Test extends JFrame{
private JRadioButton but1, but2, but3;
public Test(){
setSize(new Dimension(200,200));
initComp();
setVisible(true);
}
private void initComp() {
but1 = new JRadioButton();
but1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
but2.setVisible(but1.isSelected());
but3.setVisible(but1.isSelected());
}
});
but2 = new JRadioButton();
but2.setVisible(false);
but3 = new JRadioButton();
but3.setVisible(false);
setLayout(new FlowLayout());
JPanel pan = new JPanel();
pan.add(but1);
pan.add(but2);
pan.add(but3);
getContentPane().add(pan);
}
public static void main(String[] args) {
new Test();
}
}
Upvotes: 2