Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically re-sizing a component within a GridBagLayout

In this code I have a panel in the GridBagLayout which contains a JLabel and a JTextField.

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

public class Simple
{
    JFrame simpleWindow = new JFrame("Simple MCVE");

    JPanel  simplePanel = new JPanel();

    JLabel lblSimple;
    JTextField txtSimple;

    public void numberConvertGUI()
    {
        simpleWindow.setBounds(10, 10, 420, 80);

        simpleWindow.setMinimumSize(new Dimension(420, 80));

        simpleWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        simpleWindow.setLayout(new GridLayout(1,1));

        createSimplePanel();

        simpleWindow.getContentPane().add(simplePanel);

        simpleWindow.setVisible(true);
    }

    public void createSimplePanel()
    {
        simplePanel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        lblSimple = new JLabel();
        c.weightx = 0.0;
        c.weighty = 1.0;
        c.fill = GridBagConstraints.BOTH;
        c.insets = new Insets(0,2,0,2);
        c.gridx = 0;
        c.gridy = 0;
        c.ipady = 0;
        lblSimple.setText("Next to me is a JTextField: ");
        lblSimple.setHorizontalAlignment(JLabel.RIGHT);
        simplePanel.add(lblSimple, c);

        txtSimple = new JTextField();
        c.weightx = 0.5;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 5;
        c.gridx = 1;
        c.gridy = 0;
        c.insets = new Insets(0,2,0,2);
        simplePanel.add(txtSimple, c);
    }

    public static void main(String[] args)
    {
        Simple s = new Simple();
        s.numberConvertGUI();
    }
}

I would like to be able to automatically re-size the text field dependent on the amount of data entered in it. For example when the string "How do I re-size this component automatically when the edge of it is reached?" is entered in the JTextField it looks like this.

What it looks like

However as I enter the string I would like the JTextBox and the JFrame to automatically re-size to produce something which looks a bit like this.

The re-sized version

The only problem is I am not aware of anything which allows me to do this. I would greatly appreciate any help with accomplishing this task.

Edit

When the component is re-sized automatically I would also like there to be a maximum size for that component. This way as more data is entered the component will not keep re-sizing off of someone's computer monitor

like image 966
Dan Avatar asked Oct 31 '22 04:10

Dan


1 Answers

There is no built in functionality to do this provided within Swing.

What you will need to do is add a DocumentListener to the document behind the text field and be notified whenever text is added or removed from it.

You will then need to calculate the new size you want for your text field (which can be tricky in of itself - you will probably need to use FontMetrics) and re-size the control to match. The maximum size you can implement easily at this point just by looking at the size you are re-sizing to compared to the maximum you wish to allow.

See here for info on DocumentListener: https://docs.oracle.com/javase/tutorial/uiswing/events/documentlistener.html

See here for info on FontMetrics: https://docs.oracle.com/javase/tutorial/2d/text/measuringtext.html

like image 52
Tim B Avatar answered Nov 11 '22 15:11

Tim B