ProgramME
ProgramME

Reputation: 653

How can I write logic for changing windows in a Swing app?

I have a JFrame that contains a JTabbedPane that has two tabs and a Button. The first pane consists of a series of JRadioButtons and the other one consists of a JTextField.

I want the button to go to the next frame only if

  1. tabbed pane 1 and one of the radio buttons is selected, or
  2. tabbed pane 2 is selected and has a non-empty text field

I have implemented the functionality to go to the next page. How can I check for my conditions?

Upvotes: 0

Views: 149

Answers (2)

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103135

Use getSelectedIndex() method of the tabbedpane to determine which tab is currently selected. Use getSelectedComponent() method to get the component that is currently selected.

Once you have the selected component it depends on how you structure the components such as your radio buttons etc. You can access them and determine if they are selected or not.

If you have a JTextField named textfield then you can:

  if(textfield.getText().trim().equals("")){
         //nothing was entered
  }

to determine if any text was entered.

For the radio button use the isSelected() method to determine if it was selected or not.

Upvotes: 1

jzd
jzd

Reputation: 23629

Add logic to the button to determine the selected tab and either check the text field or the radio button. Only move to the next page if your criteria is met.

You might even want to create an interface that the tab component implement that report back true/false if the internal components are valid/not valid.

Upvotes: 1

Related Questions