How to Set Hard Limit on a Jcomponent When Setmaximumsize() and Setprefferedsize() Don't Work

Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

  1. Should I completely avoid the use of those methods?

    Yes for application code.

  2. The methods have been defined for a reason. So when should I use them? In which context? For what purposes?

    I don't know, personally I think of it as an API design accident. Slightly forced by compound components having special ideas about child sizes. "Slightly", because they should have implemented their needs with a custom LayoutManager.

  3. What exactly are the negative consequences of using those methods? (I can only think adding portability between systems with different screen resolution.)

    Some (incomplete, and unfortunately the links are broken due to migration of SwingLabs to java.net) technical reasons are for instance mentioned in the Rules (hehe) or in the link @bendicott found in his/her comment to my answer. Socially, posing tons of work onto your unfortunate fellow who has to maintain the code and has to track down a broken layout.

  4. I don't think any LayoutManager can exactly satisfy all desired layout needs. Do I really need to implement a new LayoutManager for every little variation on my layout?

    Yes, there are LayoutManagers powerful enough to satisfy a very good approximation to "all layout needs". The big three are JGoodies FormLayout, MigLayout, DesignGridLayout. So no, in practice, you rarely write LayoutManagers except for simple highly specialized environments.

  5. If the answer to 4 is "yes", won't this lead to a proliferation of LayoutManager classes which will become difficult to maintain?

    (The answer to 4 is "no".)

  6. In a situation where I need to define proportions between children of a Component (for example, child 1 should use 10% of space, child 2 40%, child 3 50%), is it possible to achieve that without implementing a custom LayoutManager?

    Any of the Big-Three can, can't even GridBag (never bothered to really master, too much trouble for too little power).

JDesktopPane inside JScrollPane resize issue

JScrollPane viewports don't respect size or bounds but rather preferred sizes. So

class Main extends JFrame {

private static final int DT_WIDTH = 1920;
private static final int DT_HEIGHT = 1080;
private JDesktopPane container = new JDesktopPane();

public Main(){
super("JDesktopPane SS");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1280, 720);

// setLayout(new ScrollPaneLayout()); // ?????

// container.setBounds(new Rectangle(1920, 1080));
container.setPreferredSize(new Dimension(DT_WIDTH, DT_HEIGHT));

Java: Difference between the setPreferredSize() and setSize() methods in components

Usage depends on whether the component's parent has a layout manager or not.

  • setSize() -- use when a parent layout manager does not exist;
  • setPreferredSize() (also its related setMinimumSize and setMaximumSize) -- use when a parent layout manager exists.

The setSize() method probably won't do anything if the component's parent is using a layout manager; the places this will typically have an effect would be on top-level components (JFrames and JWindows) and things that are inside of scrolled panes. You also must call setSize() if you've got components inside a parent without a layout manager.

Generally, setPreferredSize() will lay out the components as expected if a layout manager is present; most layout managers work by getting the preferred (as well as minimum and maximum) sizes of their components, then using setSize() and setLocation() to position those components according to the layout's rules.

For example, a BorderLayout tries to make the bounds of its "north" region equal to the preferred size of its north component---they may end up larger or smaller than that, depending on the size of the JFrame, the size of the other components in the layout, and so on.

Graphics object gets supressed

Got solved.. used an Graphics object for the top layer. i was using this.graphics earlier. Replaced it by layer1.graphics object and it solved the problem,

Resize makes things wrong

I think this would be better done with a BorderLayout. In a BorderLayout, the component specified as the center component will expand to fill as much space as possible, and the other components will remain at their preferred sizes.

int hgap = 5;
int vgap = 5;
internalFrame.getContentPane().setLayout(new BorderLayout(hgap, vgap));
internalFrame.getContentPane().add(this.result_scroll, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
bottomPanel.add(this.cmd_label);
bottomPanel.add(this.cmd_input);
bottomPanel.add(this.submit);
internalFrame.getContentPane().add(bottomPanel, BorderLayout.SOUTH);

java pie chart height is not properly placed

I have found how to fix it, all I had to do is not use a flowLayout, but a grouplayout, since the panel was making the chart be enlarged, just like if you'd put a button in a flowLayout it would become the size of it's containing zone. Thank you everyone for the amazing help.



Related Topics



Leave a reply



Submit