0

I have an application which a JFrame as main window. (The content pane has a tabbedPane, which has a JPanel named "sfp").

Sfp contains two labels, two text fields, one button and other JPanel, called "detail", as show in this image.

Detail panel has few elements, as show this image, but the relevant elements are a check box and the scroll pane.

I want that when I toggle the state of check box, toggle the visibility of the scroll pane (by default not visible), and resizing correctly all JPanel and JFrame.

To switch the visibility of scroll pane, I add this code as action of check box:

protected class DebugAction extends AbstractAction {
    public DebugAction() {
        this.putValue(Action.NAME, "");
        this.putValue(Action.SHORT_DESCRIPTION, "Debug Window");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (SFDetail.this.scrollPane.isVisible()) {
            SFDetail.this.scrollPane.setVisible(false);
        } else {
            SFDetail.this.scrollPane.setVisible(true);
        }
        SFDetail.this.revalidate();
        SFDetail.this.repaint();
    }
}

But I don't know how to change the JPanel's and JFrame sizes to fit correctly to the visible content.

  • Try using `SFDetail.this.pack()` after setting the visibility. Let me know if it helps. Else if possible post the code for the complete frame, there might be more adjustments required. – Prateek Feb 10 '16 at 10:29
  • 2
    SwingUtilities.windowForComponent will return the window/frame, from which you can try calling pack. Generally I'd advise trying to get the size of the view defined up front as best you can, maybe even allowing the scroll pane to be visible, as the window changing size can be distracting to the user – MadProgrammer Feb 10 '16 at 10:32
  • @Prateek *"which has a JPanel named "sfp")"* so SFDetail is a JPanel, contained in a JTabbedPane, which is added to the frame, so it's at least three containers deep... – MadProgrammer Feb 10 '16 at 10:35
  • @MadProgrammer Thank you so much for the info! It works so close I expected. The problem is that I don't set a preferred size in the JFrame, (I don't use a layout in this frame), and if I resize the width of frame, on toggle the check box, it set the width as the default width. Exist any function to obtain the new height obtained by pack() without use this method?. If not, I think that with a little bit of workaround, I will get it. Can you tag (or how I can done it) this comment as correct answer? – Pablo Martínez Feb 10 '16 at 12:21
  • *"(I don't use a layout in this frame)"* Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). When there is no layout, **expect problems.** – Andrew Thompson Feb 10 '16 at 13:27
  • @AndrewThompson Sorry,I wrote it wrong. I wanted to say that I'm not use the same layout. For JPanels, I use a _GridBagLayout_ , and for the contentPane in JFrame I use a _BorderLayout_ – Pablo Martínez Feb 10 '16 at 13:51
  • *"I wrote it wrong."* A [MCVE] communicates everything that we need to know about code. – Andrew Thompson Feb 10 '16 at 14:47

0 Answers0