0

I am making a GUI for data-structure in Java. I wanted a feature that whenever the user clicks on the maximize button present at the top of the form the components and everything in the form should also get resized as the windows expands and vice-versa. I've searched a lot but couldn't find the solution.

How to scale the GUI?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
rick
  • 913
  • 6
  • 13
  • 28
  • i have written "stack" code for GUI – rick Jun 14 '12 at 14:55
  • 1
    It is usually not best to scale every component, but instead to supply extra width or height to specific components. The [Nested Layout Example](http://stackoverflow.com/a/5630271/418556) does that - resize it to see what I mean. In that case, it really comes down to the layout managers that are used to position the components within the GUI, and the constraints used when adding them. – Andrew Thompson Jun 14 '12 at 14:59
  • Not getting anything wat Nested Layout Example is doing. Can you help me with some short code like how to resize a Toolbar when the maximize button is pressed – rick Jun 14 '12 at 15:06
  • 1
    rick copy the code and run then resize the window and say if that sort of resizing you are after? You will see that gridbaglayout bit and table both resize to fit the available space with appropriate aspect ratio. @AndrewThompson *"The Nested Layout Example..."* I think this answers the OP's problem, doesn't it? Thus it might as well be an answer rather then a comment (+1 anyway). – Boro Jun 14 '12 at 15:25

1 Answers1

4

Can you help me with some short code like how to resize a Toolbar when the maximize button is pressed..

I'll do better. Here's a short code sample that shows 5 of them with different resize behavior depending on where they are placed in a BorderLayout.

ResizableToolBars

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

public class ResizableToolBars {

    public static void showFrameWithToolBar(String toolBarPosition) {
        // the layout is important..
        JPanel gui = new JPanel(new BorderLayout());

        JToolBar tb = new JToolBar();
        // ..the constraint is also important
        gui.add(tb, toolBarPosition);
        tb.add(new JButton("Button 1"));
        tb.add(new JButton("Button 2"));
        tb.addSeparator();
        tb.add(new JButton("Button 3"));
        tb.add(new JCheckBox("Check 1", true));

        JFrame f = new JFrame(toolBarPosition + " Sreeeetchable Tool Bar");
        f.setContentPane(gui);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setLocationByPlatform(true);
        f.pack();

        // we don't normally set a size, this is to show where 
        // extra space is assigned.
        f.setSize(400,120);
        f.setVisible(true);
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                showFrameWithToolBar(BorderLayout.PAGE_START);
                showFrameWithToolBar(BorderLayout.PAGE_END);
                showFrameWithToolBar(BorderLayout.LINE_START);
                showFrameWithToolBar(BorderLayout.LINE_END);
                showFrameWithToolBar(BorderLayout.CENTER);
            }
        });
    }
}

If you come back to the Nested Layout Example after that, you should be able to figure out how I put it together from smaller groups of components, each in their own layout (in a panel) in one area of a parent container.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433