2

I'm building a user interface using JComponents. The UI should work in this way: depending on the value set (e.g. through a JComboBox), different JComponents are displayed.

My though was to @Override the isVisible() method of each component or, alternatively, to set them visible or not through the JComboBox. Indeed, both the two methods works, in the sense that the desired components appear and disappear from JPanel when I change the JComboBox value.

The problem is that, even if a component is not visible inside the panel, it is still active and clicking on the position in which it should be (if visible), it triggers its action. E.g. I move the mouse over the position in which another JComboBox should be; the combobox is not visible, but mouse cursor turns into Cursor.HAND_CURSOR and, if I click, the popup list of combobox items appears and I'm able to select one of these items.

Here is a salmple of the code I'm using:

public class MyPanel extends JPanel{
    public MyPanel(){
        super(new GridBagLayout());
        GridBagConstraints g = new GridBagConstraints(
            0,                              // int gridx
            -1,                             // int gridy
            1,                              // int gridwidth
            1,                              // int gridheight
            1,                              // double weightx
            1,                              // double weighty
            GridBagConstraints.NORTHWEST,   // int anchor
            GridBagConstraints.NONE,        // int fill
            new Insets(0,0,0,0),            // Insets insets
            0,                              // int ipadx
            0                               // int ipady
         );

        String[] comboItems = {"ShowCombo1","ShowCheckbox","ShowTextField"};
        JComboBox combo = new JComboBox(comboItems);

        JComboBox combo1 = new JComboBox(){
            @Override
            public boolean isVisible(){
                return combo.getSelectedIndex()==0;
            }
        };

        JCheckBox checkBox= new JCheckBox(){
            @Override
            public boolean isVisible(){
                return combo.getSelectedIndex()==1;
            }
        };

        JTextField textField= new JTextField(){
            @Override
            public boolean isVisible(){
                 return combo.getSelectedIndex()==2;
            }
        };

        add(combo,g);
        g.gridy += 1;
        add(combo1,g);
        g.gridy += 1;
        add(checkBox,g);
        g.gridy += 1;
        add(textField,g);
    }
}

Is there a better way to manage these kind of user interfaces?

4
  • 1
    I think you would have to show some code. Otherwise it is hard to analyse.
    – Vampire
    May 12, 2016 at 10:35
  • @Vampire i think you are soo right!! :)
    – Elltz
    May 12, 2016 at 10:36
  • Maybe you could both set not visible the component and setEnable(false) its subcomponent if you don't want to trigger the action of the non visible component But yes with the code it would be more clear May 12, 2016 at 10:39
  • 2
    Yes, there's a better way. Create a separate JPanel for each combination of JComponents you want based on the JComboBox selection, and manage the JPanels with a CardLayout. May 12, 2016 at 12:23

1 Answer 1

1

I think with your approach you are just return the flag to true based on the selection. Internally the setVisible(false) does more than just switching the flag off. It calls super.setVisible(false) which does the below.

/**
     * Shows or hides this component depending on the value of parameter
     * <code>b</code>.
     * <p>
     * This method changes layout-related information, and therefore,
     * invalidates the component hierarchy.
     *
     * @param b  if <code>true</code>, shows this component;
     * otherwise, hides this component
     * @see #isVisible
     * @see #invalidate
     * @since JDK1.1
     */
    public void setVisible(boolean b) {
        show(b);
    }

    /**
     * @deprecated As of JDK version 1.1,
     * replaced by <code>setVisible(boolean)</code>.
     */
    @Deprecated
    public void show() {
        if (!visible) {
            synchronized (getTreeLock()) {
                visible = true;
                mixOnShowing();
                ComponentPeer peer = this.peer;
                if (peer != null) {
                    peer.setVisible(true);
                    createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,
                                          this, parent,
                                          HierarchyEvent.SHOWING_CHANGED,
                                          Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
                    if (peer instanceof LightweightPeer) {
                        repaint();
                    }
                    updateCursorImmediately();
                }

                if (componentListener != null ||
                    (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||
                    Toolkit.enabledOnToolkit(AWTEvent.COMPONENT_EVENT_MASK)) {
                    ComponentEvent e = new ComponentEvent(this,
                                                          ComponentEvent.COMPONENT_SHOWN);
                    Toolkit.getEventQueue().postEvent(e);
                }
            }
            Container parent = this.parent;
            if (parent != null) {
                parent.invalidate();
            }
        }
    }

Though i do not understand what they actually do :-), I feel this part of code needs to be executed.

1
  • 1
    Thanks a lot. I also recognized that the two approaches are different and override the isVisible() method is not enough.
    – Roberto
    May 12, 2016 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.