Implement Java methods for showing/hiding components

I am implementing a user-interface where there is a main JPanel called centerPanel containing two smaller JPanels, source panel and target panel. The source panel contains a JTextPane, and the target panel also contains a JTextPane and another JPanel for holding JButtons. The structure of this user-interface is described below.

centerPanel (JPanel with GridLayout)

  • sourceWindowPanel (JPanel)
    • JTextPane
  • targetSourceWindowPanel (JPanel)
    • jTextPane
    • JPanel
      • JButton1
      • JButton2
      • JList
      • JTextField

The issues are:

  •  If I set centerPanel layout as BoxLayout, size of the target panel will be much bigger than the source panel. I think that it’s because the target panel contains more components than the source panel. So, BoxLayout assigns higher weight to the target panel than the source panel.
  • If I set centerPanel layout as GridLayout, size of the target panel is equal to that of the source panel, which is what I want. However, when I implement a Show/Hide method for hiding and showing one of the two JPanels (source and target) using setVisible(false) or setVisible(true), a JPanel is hiden but not collapsed. For example, if I want to show a source panel and hide a target panel, I implement the following code.

<code>

 public void maximizeSourceWindow() {
        
         if (!this.sourceWindowPanel.isVisible()) {
            this.sourceWindowPanel.setVisible(true);
        }

        if (this.targetWindowPanel.isVisible()) {
            this.targetWindowPanel.setVisible(false);
        }
    }

</code> 

The result is that the target panel is hiden and the source panel is still displayed, as expected. However, since the target panel is just invisible not collapsed, size of the source panel is still the same (a half of the main JFrame size). The area where the target panel was becomes grey.

To overcome these issues, I switch between BoxLayout and GridLayout of the centerPanel. I set layout of the centerPanel to GridLayout when I want to show both target and source panels, and I switch the layout of the centerPanel to BoxLayout when I want to show only one of them. The code below is what I have implemented.

<code>

//Show source panel 

public void maximizeSourceWindow() {
       
        this.centerPanel.setLayout(new javax.swing.BoxLayout(centerPanel, javax.swing.BoxLayout.LINE_AXIS));
       
        if (!this.sourceWindowPanel.isVisible()) {
            this.sourceWindowPanel.setVisible(true);
        }

        if (this.targetWindowPanel.isVisible()) {
            this.targetWindowPanel.setVisible(false);
        }
    }

//Show target panel

public void maximizeTargetWindow() {
       
        this.centerPanel.setLayout(new javax.swing.BoxLayout(centerPanel, javax.swing.BoxLayout.LINE_AXIS));
       
        if (!this.targetWindowPanel.isVisible()) {
            this.targetWindowPanel.setVisible(true);
        }

        if (this.sourceWindowPanel.isVisible()) {
            this.sourceWindowPanel.setVisible(false);
        }

}

//Show both source and target panels.

public void tileWindowVertically() {
       
        this.centerPanel.setLayout(new java.awt.GridLayout());
       
        if (!this.targetWindowPanel.isVisible()) {
            this.targetWindowPanel.setVisible(true);
        }

        if (!this.sourceWindowPanel.isVisible()) {
            this.sourceWindowPanel.setVisible(true);
        }
}

</code>  

This entry was posted in Java. Bookmark the permalink.

Leave a comment