Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPanel taking up more space than it needs. How can I shrink it?

UPDATE: I have received justified criticism for posting non working code. I've taken that to heart and am updating this post with a complete working example. I'm also updating the description accordingly:

I have a very simple java swing GUI whose components take up what looks to be an equal amount of vertical (Y) space as is used by the largest Y extent component, but completely unnecessarily so. I have tried to shrink those components that don't need that much vertical space using preferredSize hints but to no avail.

The basic layout is simple: There's a main window and three vertical panels. The layout is a simple GridLayout (and I would prefer to keep it that way, unless someone shows me what I need cannot be done with GridLayout). All three panels seem to be occupying the same amount of vertical space, even though in the case of the sliders, this is massive waste of space. How can I get each of the sub-panes to only use as much space as they each need? i.e. I would like the two slider windows to be only as tall as the sliders and their description need to be.

The code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class test {
  public static void main(String[] arg) {
    JFrame mainWindow = new JFrame();
    JSlider slider1 = new JSlider(0,100,50);
    JSlider slider2 = new JSlider(0,100,50);

    JPanel pnlSlider1 = new JPanel();
    pnlSlider1.setLayout(new GridLayout(1,1));  // 1 row, 1 column
    pnlSlider1.add(new JLabel("Description for slider1"));
    pnlSlider1.add(slider1);

    JPanel pnlSlider2 = new JPanel();
    pnlSlider2.setLayout(new GridLayout(1,1));  // 1 row, 1 column
    pnlSlider2.add(new JLabel("Description for slider2"));
    pnlSlider2.add(slider2);

    // label should now be to the left of slider

    String content = "<html>Some rather long winded HTML content</html>";
    JEditorPane ep = new JEditorPane("text/html", content);

    // this is the main window panel
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(3,1)); // 3 rows, 1 column
    panel.add(ep);
    panel.add(pnlSlider1);
    panel.add(pnlSlider2);

    // tie it all together and display the window
        mainWindow.setPreferredSize(new Dimension(300, 600));
        mainWindow.setLocation(100, 100);
        mainWindow.getContentPane().add(panel);
        mainWindow.pack();
        mainWindow.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        mainWindow.setVisible(true);

  }
}

(removed rant about not having seen any GUI coding advances in 30 years as that's not pertinent to the problem and likely won't be solved in this post either)

like image 567
Balthasar Avatar asked Jun 19 '15 13:06

Balthasar


People also ask

Can you set the size of a JPanel?

We change the background colour to red, and give the JPanel a size using setSize(width, height). We also give the widget a location using setLocation(x pixels, y pixels). This sets the top left hand corner of the widget to the specified position.

What is the difference between JPanel and container?

The difference is two fold. One, the first example declares a Container rather than a JPanel so that "content" can be any implementation of Container and is not limited to a JPanel. Two, the first example is fetching the contentPane for "myFrame" where as the second is instantiating a new JPanel.

What does JPanel repaint do?

Ideally if you want to draw in an area then you would extend JPanel with a custom paint and place it below the button. To explain repaint further - the point of 'repaint' is to tell the window manager that you have changed something that requires the component to be redrawn.

What is the use of LayoutManager?

A layout manager is an object that implements the LayoutManager interface* and determines the size and position of the components within a container. Although components can provide size and alignment hints, a container's layout manager has the final say on the size and position of the components within the container.


1 Answers

..components take up what looks to be an equal amount of vertical (Y) space as is used by the largest Y extent component, but completely unnecessarily so.

Yes, that is the way GridLayout is designed to work.

Use a GridBagLayout or BoxLayout or GroupLayout instead, each of which can do a single column or row of components of variable size (width and height).

like image 93
Andrew Thompson Avatar answered Sep 29 '22 18:09

Andrew Thompson