Hello,

I am wondering if it's possible to hide/show components on a form, depending on which radio button is selected. For example lets say i have two radio buttons (in a buttongroup) and two buttons. When i select the first radio button i want the first button to show, and the second one to hide (vice-versa with the second radio button). Since i am quite new to java, i've only tried simply removing/adding components with the container class, but that doesn't seem to work.

Any help would be much appriciated.

Hm, now i have another problem. I've completed my form which consists of two radio buttons(which are always visible) and some other stuff which aren't always visible.
The problem kind of changes every time i run the application. For example:
the first time i ran it only one radio was visible and i had to move my mouse over the other to show it. The next time both were visible, the third time none, the fourth time the whole form went white and nothing was visible and so on.

Any ideas?

//appForm.java
package javaprojekt5;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class appForm extends JFrame implements ActionListener{
    private Container formComponents=this.getContentPane();
    private JButton buttonWrite=new JButton();
    private JButton buttonRead=new JButton();
    private JLabel label1=new JLabel();
    private JLabel label2=new JLabel();
    private ButtonGroup options=new ButtonGroup();
    private JTextField filePath=new JTextField();
    private JTextArea text=new JTextArea();
    private JRadioButton radioRead=new JRadioButton();
    private JRadioButton radioWrite=new JRadioButton();
    private JScrollPane scroll;
    private JLabel label3=new JLabel();
    boolean rRead=false;
    boolean rWrite=true;
    public appForm(){
        this.setSize(400,600);
        this.setTitle("RH Writer");
        this.setLocation(300,100);
        this.setResizable(false);
        this.show(true);
        buttonWrite.setLocation(10,450);
        buttonWrite.setSize(150,80);
        buttonWrite.setText("Write!");
        buttonRead.setLocation(230,450);
        buttonRead.setSize(150,80);
        buttonRead.setText("Read!");
        label1.setLocation(10,110);
        label1.setSize(150,30);
        label1.setText("Full path");
        filePath.setLocation(150,110);
        filePath.setSize(200,30);
        label2.setLocation(10,180);
        label2.setSize(240,30);
        label2.setText("Text to be written into the file");
        buttonWrite.addActionListener(this);
        buttonRead.addActionListener(this);
        scroll=new JScrollPane(text);
        scroll.setLocation(10,210);
        scroll.setSize(370,200);
        formComponents.add(scroll);
        radioRead.setLocation(10,40);
        radioRead.setSize(150,20);
        radioRead.setText("Read from a file");
        radioWrite.setLocation(10,60);
        radioWrite.setSize(150,20);
        radioWrite.setText("Write to a file");
        formComponents.add(radioRead);
        formComponents.add(radioWrite);
        ButtonGroup options=new ButtonGroup();
        options.add(radioWrite);
        options.add(radioRead);
        radioWrite.addActionListener(this);
        radioRead.addActionListener(this);
        label3.setLocation(10,5);
        label3.setSize(100,20);
        label3.setText("Choose");
        formComponents.add(label3);
        radioWrite.setSelected(true);
        formComponents.add(label1);
        formComponents.add(buttonWrite);
        formComponents.add(filePath);
        formComponents.add(label2);
        formComponents.add(buttonRead);
        radioWrite.setVisible(true);
        radioRead.setVisible(true);
        label3.setVisible(true);
        scroll.setVisible(true);
        buttonRead.setVisible(false);
        formComponents.setLayout(null);
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==radioWrite){
            if(rRead){
                buttonRead.setVisible(false);
                rRead=false;
            }
            rWrite=true;
            filePath.setVisible(true);
            scroll.setVisible(true);
            buttonWrite.setVisible(true);
            label1.setVisible(true);
            label2.setVisible(true);
            radioRead.setVisible(true);
            radioWrite.setVisible(true);
            if(e.getSource()==buttonWrite){
                if(filePath.getText().equals("")){
                    JOptionPane.showMessageDialog(null,"Error","Error",JOptionPane.INFORMATION_MESSAGE);
                }
                else if(text.getText().equals("")){
                    JOptionPane.showMessageDialog(null,"Error","Error",JOptionPane.INFORMATION_MESSAGE);
                }
                else if(text.getText().equals("") && filePath.getText().equals("")){
                    JOptionPane.showMessageDialog(null,"Error","Error",JOptionPane.INFORMATION_MESSAGE);
                }
                else{
                    try{
                        BufferedWriter writing=new BufferedWriter(new FileWriter(filePath.getText()));
                        writing.write(text.getText());
                        writing.close();
                    }
                    catch(IOException ex){
                    
                    }
                }
            }
            
        }
        else if(e.getSource()==radioRead){
            if(rWrite){
                scroll.setVisible(false);
                buttonWrite.setVisible(false);
                label2.setVisible(false);
                rWrite=false;
            }
            rRead=true;
            filePath.setVisible(true);
            buttonRead.setVisible(true);
            label1.setVisible(true);
            radioRead.setVisible(true);
            radioWrite.setVisible(true);
            if(e.getSource()==buttonRead){
                if(filePath.getText().equals("")){
                    JOptionPane.showMessageDialog(null,"Error","Error",JOptionPane.INFORMATION_MESSAGE);
                }
                else{
                    try{
                        BufferedReader reading=new BufferedReader(new FileReader(filePath.getText()));
                        String vrstica="",text="";
                        while((vrstica=reading.readLine())!=null){
                            text=text+vrstica+"\n";
                        }
                        reading.close();
                        JOptionPane.showMessageDialog(null,text,"File content",JOptionPane.INFORMATION_MESSAGE);
                    }
                    catch(IOException ex){
                      
                    }
                }
            }
            
        }
    }
}

//Main.java
package javaprojekt5;


public class Main {

    public static void main(String[] args) {
        appForm mainForm=new appForm();
    }

}

I did not get such problem, but this could be caused by the fact that you do not have closing sorted out. So when you press X in right top corner to shut down the window, the window will visually disappear but resources are still in use and that may do lot of strange things.
Add this line to JFrame declaration this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); .

One small advice, try to keep the whole element declaration together (example button-name, size, position, visibility, listeners) it will save you time searching for errors in the logic

This question has already been answered. Start a new discussion instead.