[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

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

Posted on 2008-02-02
8
Medium Priority
?
583 Views
Last Modified: 2010-04-21
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.
0
Comment
Question by:tomboman
  • 5
  • 3
8 Comments
 
LVL 17

Expert Comment

by:krakatoa
ID: 20808478
Check the components' preferred sizes and weights.
0
 

Author Comment

by:tomboman
ID: 20933486
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
 
LVL 17

Expert Comment

by:krakatoa
ID: 20936496
>>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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:tomboman
ID: 20941990
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
 
LVL 17

Accepted Solution

by:
krakatoa earned 200 total points
ID: 20944034
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
 
LVL 17

Expert Comment

by:krakatoa
ID: 20947405
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
 

Author Closing Comment

by:tomboman
ID: 31427577
thanks krakatoa.
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 20952431
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month8 days, 13 hours left to enroll

611 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question