When resizing a java GUI to a smaller screen, how to make sure a particular component is always displayed?

I have constructed a GUI using java swing, in the GUI, I used GridBagConstraint to arrange components.

I added a JLable(nameJLable), a JTextField, a JCheckBox, then another JLabel (explanationLabel)

At the moment when I resize the GUI to a smaller screen, the explanation is always the last component visible - this is the least important component to be displayed. How do I make sure the nameJLable is always displayed when GUI is resized to s smaller GUI?

thanks.
tombomanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krakatoaCommented:
Check the components' preferred sizes and weights.
0
tombomanAuthor Commented:
thank krakatoa.

so if I have the following codes, how do I make sure the second component (jTF1) is always displayed when GUI is resized to a smaller screen?

thanks.


package testing;
 
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
 
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
 
public class TestResize extends JFrame {
    
    private static final long serialVersionUID = 1L;
    
    
    public TestResize() {
	
	JLabel jlabel = new JLabel("testinglable");
        JTextField jTF1 = new JTextField("");
        JCheckBox jc = new JCheckBox();
        JLabel jlabel2 = new JLabel("testinglable2");
        JPanel jpanel = new JPanel(new GridBagLayout());
	
	GridBagConstraints gbcNameLable = new GridBagConstraints();
	gbcNameLable.gridx = 0;
	gbcNameLable.gridy = 0;
	
	jpanel.add(jlabel,gbcNameLable);
	
	GridBagConstraints gbcNameTextField = new GridBagConstraints();
	
	gbcNameTextField.gridx = 1;
	gbcNameTextField.weightx = 3;
	gbcNameTextField.anchor = GridBagConstraints.WEST;
	gbcNameTextField.fill = GridBagConstraints.HORIZONTAL;
	
	jpanel.add(jTF1,gbcNameTextField);
	
	GridBagConstraints gbcNameCheckBox = new GridBagConstraints();
	gbcNameCheckBox.gridx = 4;
	gbcNameCheckBox.weightx = 0.5;
	
	jpanel.add(jc, gbcNameCheckBox);
	
	GridBagConstraints gbclNameMatchLabel = new GridBagConstraints();
	gbclNameMatchLabel.gridx = 5;
	gbclNameMatchLabel.weightx = 0;
	gbclNameMatchLabel.anchor = GridBagConstraints.WEST;
	
	jpanel.add(jlabel2, gbclNameMatchLabel);
	
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	
	Dimension winsize = this.getToolkit().getScreenSize();
	
	this.add(jpanel);
	this.setBounds(winsize.width/4, winsize.height/4, winsize.width/4, winsize.width/4);//(new Dimension(500,500));
	this.setVisible(true);
	
    }
    
    public static void main(String args[]){
	new TestResize();
    }
 
}

Open in new window

0
krakatoaCommented:
>>how do I make sure the second component (jTF1) is always displayed when GUI is resized to a smaller screen<<

jTF1 does resize already, just as it should, whether the resize is larger, or smaller.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

tombomanAuthor Commented:
hi krakatoa,

yes jTF1 does gets resized. It is the most component on the GUI, when I resize to a smaller screen, it is the first component to disappear which is undesirable. my question is: is it possible to make jTF1the last component to disappear?

thanks.
0
krakatoaCommented:
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.*;
 
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.*;
 
public class TestResize extends JFrame {
   
    private static final long serialVersionUID = 1L;
   
   
    public TestResize() {
      
        Box myBox = Box.createVerticalBox();
        JLabel jlabel = new JLabel("testinglable");
        JLabel jpadLabel = new JLabel("");
        JTextField jTF1 = new JTextField("      ");
       

        JCheckBox jc = new JCheckBox();
        JLabel jlabel2 = new JLabel("testinglable2");
        JPanel jpanel = new JPanel(new GridBagLayout());
      
      GridBagConstraints gbcNameLable = new GridBagConstraints();
      gbcNameLable.gridx = 0;
      gbcNameLable.gridy = 0;

      GridBagConstraints gbcNameTextField = new GridBagConstraints();
      gbcNameTextField.gridx = 1;
      gbcNameTextField.gridy = 0;
      
      
      GridBagConstraints gbcNameCheckBox = new GridBagConstraints();
      gbcNameCheckBox.gridx = 6;
      

      GridBagConstraints gbclNameMatchLabel = new GridBagConstraints();
      gbclNameMatchLabel.gridx = 7;
      gbclNameMatchLabel.gridy = 0;
      
      jpanel.add(jlabel,gbcNameLable);

      myBox.setMinimumSize(new Dimension(20,20));

      myBox.add(jTF1,gbcNameTextField);

      jpanel.add(myBox);

      
      jpanel.add(jc,gbcNameCheckBox);
      jpanel.add(jlabel2,gbclNameMatchLabel);
      
            
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
      
      Dimension winsize = this.getToolkit().getScreenSize();
      
      this.add(jpanel);
      this.setBounds(winsize.width/4, winsize.height/4, winsize.width/4, winsize.width/4);//(new Dimension(500,500));
      this.pack();
      this.setVisible(true);
      
    }
   
    public static void main(String args[]){
      new TestResize();
    }
 
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krakatoaCommented:
You'd be better off using a GridLayout in the jpanel, and adding that jpanel to possibly the  BorderLayout of the JFrame (or another Grid or BoxLayout).
0
tombomanAuthor Commented:
thanks krakatoa.
0
krakatoaCommented:
k. You'd be better off as I said using a GridLayout (just change the GridbagLayout for Grid in the jpanel constructor and drop the gbc's. The only trouble you'll have is resizing the JTextfield's height, but I'm sure there is a way to contain it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.