Auto Resize Component in GridBagLayout

In this code, I have a panel in a GridBagLayout that 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 resize the text field depending on the amount of data entered into it. For example, when the line "How to resize this component automatically when its edge is reached?" introduced in JTextField, it looks like this.

What it looks like

However, when I enter the line, I would like the JTextBox and JFrame automatically resize to create something similar to this.

The re-sized version

The only problem is I don’t know anything that allows me to do this. I would really appreciate any help with this task.

Edit

When a component resizes automatically, I would also like the maximum size for this component. Thus, when more data is entered, the component will not reinstall the computer monitor computer

+5
source share
2 answers

There are no built-in functions that can be done in Swing.

What you need to do is add a DocumentListener to the document behind the text box and get notified when the text is added or removed from it.

Then you will need to calculate the new size you want for your text box (which may seem complicated in yourself - you probably have to use FontMetrics ) and resize the control to fit. The maximum size that you can easily implement at this point, just by looking at the size that you are updating, compared to the maximum that you want to allow.

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

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

+1
source

After changing my layout in MigLayout I managed to get my code to do what I wanted. For those who read this, who are interested in how to do this, see the code below.

 import java.awt.*; import javax.swing.*; import java.lang.Object.*; import javax.swing.event.*; import javax.swing.text.*; import net.miginfocom.swing.MigLayout; public class Simple { JFrame simpleWindow = new JFrame("Simple MCVE"); JPanel simplePanel = new JPanel(); JLabel lblTitle; JLabel lblSimple; JTextField txtSimple; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String []fontFamilies = ge.getAvailableFontFamilyNames(); int adv; int widthOftxtSimple; int i; int xDimension; public static int GetScreenWorkingWidth() { return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width; } int maxSize = GetScreenWorkingWidth()-50; public void numberConvertGUI() { simpleWindow.setBounds(10, 10, 800, 100); simpleWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); simpleWindow.setResizable(false); simpleWindow.setLayout(new GridLayout(1,1)); createSimplePanel(); simpleWindow.getContentPane().add(simplePanel); simpleWindow.setVisible(true); } public void createSimplePanel() { MigLayout layout = new MigLayout("" , "[][grow]"); simplePanel.setLayout(layout); lblTitle = new JLabel(); lblTitle.setText("This is a Title"); simplePanel.add(lblTitle, "wrap, align center,span 2"); lblSimple = new JLabel(); lblSimple.setText("Next to me is a JTextField: "); simplePanel.add(lblSimple); String sMPMConstraints = "width 615::"+maxSize; txtSimple = new JTextField(); simplePanel.add(txtSimple, "grow, "+sMPMConstraints); myDocumentListener(); } public void myDocumentListener() { Document doc = txtSimple.getDocument(); DocumentListener listener = new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { System.out.println("Insert"); String simpleString = txtSimple.getText(); Graphics graphics = txtSimple.getGraphics(); Font test = txtSimple.getFont(); FontMetrics metrics = graphics.getFontMetrics(test); adv = metrics.stringWidth(simpleString); System.out.println("The width of the string is: "+adv); widthOftxtSimple = txtSimple.getWidth(); System.out.println("The width of the JTextField is: "+widthOftxtSimple); if(xDimension<maxSize) { System.out.println("The x dimension is shorter than the max size which is: "+maxSize); if(widthOftxtSimple-20<adv) { i = 615-20-adv; //615 in this case is the original length of the text box //change for different text boxes System.out.println("Value of i is: "+i); xDimension = 800-i; System.out.println("Value of xDimension is: "+xDimension); simpleWindow.setBounds(10, 10, xDimension, 100); } } else { System.out.println("The x dimension is longer than the max size which is: "+maxSize); simpleWindow.setBounds(10, 10, maxSize, 100); String lastCharSize = simpleString.charAt(simpleString.length() - 1)+""; int adv2 = metrics.stringWidth(lastCharSize); xDimension = xDimension+adv2; } } @Override public void removeUpdate(DocumentEvent e) { System.out.println("Remove"); String simpleString = txtSimple.getText(); Graphics graphics = txtSimple.getGraphics(); Font test = txtSimple.getFont(); FontMetrics metrics = graphics.getFontMetrics(test); widthOftxtSimple = txtSimple.getWidth(); System.out.println("The width of the JTextField is: "+widthOftxtSimple); System.out.println("Value of xDimension is: "+xDimension); if(xDimension<maxSize) { if(xDimension>800) //Original length of the JFrame { System.out.println("last char = " + simpleString.charAt(simpleString.length() - 1)); String lastCharSize = simpleString.charAt(simpleString.length() - 1)+""; int adv2 = metrics.stringWidth(lastCharSize); int newX = xDimension-adv2; simpleWindow.setBounds(10, 10, newX, 100); xDimension = newX; if(xDimension<800) { simpleWindow.setBounds(10, 10, 800, 100); } } } else { System.out.println("last char = " + simpleString.charAt(simpleString.length() - 1)); String lastCharSize = simpleString.charAt(simpleString.length() - 1)+""; int adv2 = metrics.stringWidth(lastCharSize); int new3 = xDimension-adv2; System.out.println("This is the else statement"); System.out.println("The new `xDimension` will be:"+ new3); xDimension = new3; } } @Override public void changedUpdate(DocumentEvent e) { System.out.println("Change"); } }; doc.addDocumentListener(listener); } public static void main(String[] args) { Simple s = new Simple(); s.numberConvertGUI(); } } 

To compile this without errors, you will need the MigLayout package downloaded

+1
source

All Articles