rick
rick

Reputation: 931

Resizing the components in a desktop GUI

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?

Upvotes: 0

Views: 676

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168845

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.

Upvotes: 4

Related Questions