0

I have a group of jRadioButton that I created,

each button has an action listener that creates a JTable in a separate window.

I want that when I press another button, the frame will be cleaned and then the other JTable to be performed,

ButtonGroup group = new ButtonGroup();
JRadioButton[] jRadioButton = new JRadioButton[5];
for (int i = 0; i < 5; i++) {
    jRadioButton[i] = new JRadioButton("machine "+i);
    jRadioButton[i].setBounds(x, y, width, height);// x, y, width, height are place parameters
    group.add(jRadioButton[i]);
    frame.getContentPane().add(jRadioButton[i]);
    frame.update(frame.getGraphics());//update the frame and add the buttons
}

let's say, I pressed machine 1 and a table popped up in a different window, now when I press machine 2 and a different table will pop up I want to clear the new window before the second table is shown.

So my question is, is it possible to clean a window and if yes how?

theB
  • 6,450
  • 1
  • 28
  • 38

2 Answers2

2

If the only difference is JTable you can just change the table's model with theTable.setModel(properJRadioButtonDependentModel).

If you have more controls you can either use a CardLayout swapping panels (for each JRadioButton instance you can create a panel and swap them)

OR

remove all controls using removeAll() method of container, add new controls and call

container.revalidate();
container.repaint();
StanislavL
  • 56,971
  • 9
  • 68
  • 98
0

the table creation

public void createMachineTable(int row) {
    model = new DefaultTableModel(col, row);
    table = new JTable(model) {
        /**
         * 
         */
        @Override
        public boolean isCellEditable(int rows, int columns) {
            if (columns == 0)
                return false;//left side uneditable
            else
                return true;//right side editable
        }
    };
    pane = new JScrollPane(table);// the new window of te table
    getContentPane().add(pane);
    setVisible(true);
    setSize(500, 600);
    getContentPane().setLayout(new FlowLayout());
    setDefaultCloseOperation(1);

}

now, when pressing a different button, the following levels will be applied

pane.getViewport().remove( table );
createMachineTable(rowsNumer);

the table will be deleted and a new one will be created.