Jonathan Beaudoin
Jonathan Beaudoin

Reputation: 2188

Java GUI Automatically Resizing

Here is where the GUI is drawn(note, the class extends JFrame).

public Cache() {
    SubstanceColorChooserUI col = new SubstanceColorChooserUI();
    while (mode == 0);
    setResizable(false);
    setTitle("Cache");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 483, 374);
    setLocationRelativeTo(null);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(new BorderLayout());

    textField = new JTextField();
    textField.setBounds(10, 11, 328, 20);
    contentPane.add(textField);
    textField.setColumns(10);

    JButton btnLoadCache = new JButton("Load cache");
    btnLoadCache.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                String loc = textField.getText();
                if (loc.equals("")) {
                    JOptionPane.showMessageDialog(Cache.this, "Please specify a location for the cache.", "Unable to load", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if (!loc.endsWith("\\"))
                    loc = loc + "\\";
                cache = new Store(loc);
                loadImages();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(Cache.this, "Cache failed to initialize.\n" + e.getMessage(), "Unable to load", JOptionPane.ERROR_MESSAGE);
            }
        }
    });
    btnLoadCache.setBounds(351, 9, 112, 23);
    contentPane.add(btnLoadCache);

    JSplitPane splitPane = new JSplitPane();
    splitPane.setDividerLocation(150);
    splitPane.setContinuousLayout(true);
    splitPane.setBounds(10, 44, 453, 279);
    contentPane.add(splitPane);

    JPanel panellie = new JPanel();
    panellie.setLayout(new BorderLayout(0, 0));
    panel = new ImagePanel(null);

    scrollPane_1 = new JScrollPane();
    panellie.add(scrollPane_1, "Center");

    scrollPane_1.setViewportView(panel);
    splitPane.setRightComponent(panellie);

    JPanel panel_1 = new JPanel();
    splitPane.setLeftComponent(panel_1);
    panel_1.setLayout(new BorderLayout(0, 0));

    JScrollPane scrollPane = new JScrollPane();
    panel_1.add(scrollPane, BorderLayout.CENTER);

    model = new DefaultListModel<ListedImage>();
    list = new JList<ListedImage>(model);
    list.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent arg0) {
            if (arg0.getValueIsAdjusting())
                return;
            ListedImage img = list.getModel().getElementAt(list.getSelectedIndex());
            panel.setImage(img.getImage());
        }
    });
    scrollPane.setViewportView(list);

    progressBar = new JProgressBar();
    progressBar.setStringPainted(true);
    progressBar.setDoubleBuffered(true);
    progressBar.setBounds(10, 328, 453, 14);
    contentPane.add(progressBar);

}

How would I make it so when I setResizeable to true and I start dragging the program to be bigger and smaller, how do I make it so that the components inside of the frame(buttons, labels, etc) resize when the whole Frame is resizes

Upvotes: 2

Views: 26325

Answers (2)

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

1. You can use setSize() method to set your JFrame size.

2. You can choose to make the size of JFrame fixed by setting isResizable(false).

3. Use GroupLayout, developed by NetBeans team in 2005, try using the Windows Builder Pro provided by Google now for free. Here you can select GroupLayout, and then drag and drop your components in the JFrame, even if you want you can manually edit the GUI code.

Upvotes: 1

Rob Wagner
Rob Wagner

Reputation: 4421

Use a layout manager instead of setting the bounds for each component.

It is going to vary from program to program how you want your components to move.

Take a look at this, and try to see which layout will work best for you.

Upvotes: 6

Related Questions