Question

I have a complicated GUI with lot of components (JButtons, JLabels, JComboBoxes, JSpinners, etc). That's why I have to split it on several classes (add components to JPanels, this JPanels add to bigger JPanels, this JPanels add to JTabbedPane, and JTabbedPane add to JFrame). Depend on user choises and filling in data some components enabled or disabled or get some value and set not editable (in a word - interact). It's easy to done and worked properly, if components (which are interact) are in the same class, but if only it are in different classes - any results... AAA!!!

I made simple example to explane what I need. There are four classes. First one create JFrame and add JTabbedPane:

public class MainFrame extends JFrame {

MainFrame() {
    super("MainFrame");
    go();
}

public void go() {
    Tabs tabs = new Tabs();
    getContentPane().add(tabs);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 300);
    setVisible(true);
}

public static void main(String[] args) {
    MainFrame frame = new MainFrame();
}

}

The second class create JTabbedPane and add two JPanels as tabs. Second tab.setEnabledAt(1, false):

public class Tabs extends JTabbedPane {

public Tabs() {
    go();
}

public void go() {
    TabData data = new TabData();
    add("  Data  ", data);
    TabCalculation calculation = new TabCalculation();
    add("Calculation", calculation);
    setEnabledAt(1, false);
}

}

The third class create JPanel with JComboBox:

public class TabData extends JPanel {

public TabData() {
    go();
}

JComboBox someData; 

public void go() {      
    String type[] = { "    ", "Type 1", "Type 2", "Type 3" };
    someData = new JComboBox(type);
    add(someData);
    someData.addActionListener(new DataListener());
}

public class DataListener implements ActionListener {

    public void actionPerformed(ActionEvent ev) {           
        if (someData.getSelectedIndex() > 0) {
            Tabs tabs = new Tabs();
            tabs.setEnabledAt(1, true);
        }
    }
}

}

... and fourth class create some JPanel. Second tab with this JPanel disabled. When user set some value in JComboBox (selectedIndex>0) - tab have to enabled. But Tabs tabs = new Tabs(); tabs.setEnabledAt(1, true); didn't help...

How can I do that? PLEASE HELP!!! I can't sleep... I can't work... I always thinking about it and try to find out a solution...

Was it helpful?

Solution

When user set some value in JComboBox (selectedIndex>0) - tab have to enabled.

If you need to have all of these classes split, then I would suggest you make this change in your 3rd class:

public class TabData extends JPanel {

    JComboBox someData;

    ...

    // Get rid of DataListener class and add this public method instead:
    public void addActionListenerToComboBox(ActionListener listener) {
        someData.addActionListener(listener);
    }
}

And make this change in your 2nd class:

public class Tabs extends JTabbedPane {

    public Tabs() {
        go();
    }

    public void go() {
        TabData data = new TabData();           
        data.addActionListenerToComboBox(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox comboBox = (JComboBox)e.getSource();
                boolean enableSecondTab = comboBox.getSelectedIndex() > -1;
                setEnabledAt(1, enableSecondTab);
            }

        });
        add("  Data  ", data);
        TabCalculation calculation = new TabCalculation();
        add("Calculation", calculation);
        setEnabledAt(1, false);
    }
}

Take a look to EventObject.getSource() javadoc for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top