Other placement of fields everytime i run the code - java

I'm stuck with my application. Every time I run my code, my fields e.g. fromTextField once have width 180 other time my width private static final Integer widthField = 232.
Code:
package gui;
import javax.swing.*;
import java.awt.*;
public class MailGui extends JFrame {
private static final Integer widthField = 232;
private MailGui() {
JPanel northPanel = new JPanel();
northPanel.setLayout(new GridLayout(5, 5));
JLabel fromLabel = new JLabel("From: ", SwingConstants.LEFT);
JLabel passwdLabel = new JLabel("Password: ", SwingConstants.LEFT);
JLabel toLabel = new JLabel("To: ", SwingConstants.LEFT);
JLabel subjectLabel = new JLabel("Subject: ", SwingConstants.LEFT);
JLabel textLabel = new JLabel("Content: ", SwingConstants.LEFT);
JTextField fromTextField = new JTextField();
JTextField toTextField = new JTextField();
JPasswordField passwdPasswordField = new JPasswordField();
JTextField subjectTextField = new JTextField();
JTextArea textArea = new JTextArea(8, 30);
JButton sendButton = new JButton("Send");
textArea.setLineWrap(true);
northPanel.add(fromLabel);
northPanel.add(fromTextField);
northPanel.add(passwdLabel);
northPanel.add(passwdPasswordField);
northPanel.add(toLabel);
northPanel.add(toTextField);
northPanel.add(subjectLabel);
northPanel.add(subjectTextField);
northPanel.add(textLabel);
northPanel.add(textArea);
this.add(northPanel, BorderLayout.NORTH);
JScrollPane scrollPane = new JScrollPane(textArea);
this.add(scrollPane, BorderLayout.CENTER);
JPanel southPanel = new JPanel();
southPanel.add(sendButton);
add(southPanel, BorderLayout.SOUTH);
this.pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
fromTextField.setBounds(textArea.getX() + 100, 0, widthField, 19);
passwdPasswordField.setBounds(textArea.getX() + 100, 19, widthField, 19);
toTextField.setBounds(textArea.getX() + 100, 38, widthField, 19);
subjectTextField.setBounds(textArea.getX() + 100, 57, widthField, 19);
}
public static void main(String[] args) {
new MailGui();
}
}
What I have:
Or
What I except:
Thanks for every help. Q.

Invoking the setBounds() method does nothing. The layout manager will override the size/location based on the rules of the layout manager. In the case of the GridLayout all components will be sized to the preferred size of the largest component or as the frame size is changes each cell will adjust to fill the space available in the frame.
When you create a JTextField the code should be something like:
JTextField textField = new JTextField(15);
This will allow the text field to determine its own preferred size to display 15 "W" characters based on the font of the text field.
Then the layout manager can do a better job at determining the size of each component
If you want the widths of the labels and the text fields to be different, then you need to use a different layout manager. Probably a GridBagLayout. Read the Swing tutorial on How to Use GridBagLayout for more information and examples.
Note the tutorial examples show you how to create the GUI on the Event Dispatch Thread (EDT).

Call pack() at the end of the GUI composition in order to layout it in a definite way once.
The dependency of the geometry to textArea is unfortunate. Put it in the layout too.
There are nice GUI editors with more complex layout managers.

Related

I can't set Properly the size of each element on my Java Swing GUI

package View;
import javax.swing.*;
import javax.swing.plaf.synth.ColorType;
import modulo.PatenteTypes;
import java.awt.*;
public class FrameLogin extends JFrame {
private static final long serialVersionUID = 1L;
Color Arancione = new Color(255, 150, 0);
public FrameLogin(String ArrayClienti[] ){
this.setTitle("BiKar Prenotazioni");
this.setSize(new Dimension(1000,600));
this.setBackground(Color.black);
this.setForeground(Color.black);
JPanel Pannello = new JPanel();
this.setLayout(new BorderLayout());
Pannello.setLayout(new BorderLayout());
Pannello.setBackground(Color.black);
Pannello.setForeground(Color.orange);
this.add(Pannello);
JComboBox TipoCliente = new JComboBox(ArrayClienti);
TipoCliente.setPreferredSize(new Dimension(200,250));
TipoCliente.setMaximumSize(TipoCliente.getPreferredSize());
TipoCliente.setSize(200, TipoCliente.getPreferredSize().height);
JTextField Nome = new JTextField();
JTextField Cognome = new JTextField();
JTextField ID = new JTextField();
JTextField Eta = new JTextField();
JComboBox<PatenteTypes> TipoPatente= new JComboBox<PatenteTypes>(PatenteTypes.values());
this.setLayout(new BorderLayout());
Pannello.setLayout(new BorderLayout());
this.setBackground(Color.black);
this.setForeground(Color.black);
this.add(Pannello);
this.add(Nome);
this.add(Cognome);
this.add(ID,BorderLayout.WEST);
this.add(Eta, BorderLayout.EAST);
this.add(TipoPatente,BorderLayout.CENTER);
this.add(TipoCliente,BorderLayout.NORTH);
TipoCliente.setBackground(Arancione);
TipoCliente.setForeground(Arancione);
Nome.setBackground(Arancione);
Nome.setForeground(Arancione);
Cognome.setBackground(Arancione);
Cognome.setForeground(Arancione);
ID.setBackground(Arancione);
ID.setForeground(Arancione);
Eta.setBackground(Arancione);
Eta.setForeground(Arancione);
TipoPatente.setBackground(Arancione);
TipoPatente.setForeground(Arancione);
this.setVisible(true);
Pannello.setVisible(true);
TipoCliente.setVisible(true);
ID.setVisible(true);
Eta.setVisible(true);
TipoPatente.setVisible(true);
Nome.setVisible(false);
Cognome.setVisible(false);
}
}
Result:
As you can see in the image the output are 2 very big ComboBox and no TextAreas at all.
I should have gotten 2 small ComboBox. one at the north, the other at the center, and 2 text ares at sides of the combobox at the center. thanks for your help.
I would like to be able to set their dimensione and their position ( I don't want this to be resizable).
also at the border you can see 2 orange lines. I think those where supposed to be the text areas, but all of the space was taken by the JComboBox

Trouble aligning text in a JLabel centrally within a JPanel

I'm fairly new to Java and programming in general and I've run into a problem that I can't seem to solve. In my program I have two JPanels, containing a JLabel and three JButtons respectively, and the panels are arranged in a GridLayout within the frame.
My problem is that I can't get the text in the JLabel to sit centrally to the north of the first JPanel. Originally I was using a FlowLayout that arranged it in the orientation I wanted, but when the contained String became longer than a certain length, it just went off-screen. With the current setup, the text wraps appropriately, but always starts off aligned to the left.
Example screenshot of UI
I've searched around the site and looked at multiple answers - indeed I thought my issue would be solved by using HTML (This does solve the wrapping issue and aligns the second line centrally, as seen below) - but I still can't figure out how to align the first line centrally.
Example using longer String
Here's my current code:
import java.awt.*;
import javax.swing.*;
public class UITests extends JFrame {
private JLabel txtWindow = new JLabel();
private JButton jb1 = new JButton();
private JButton jb2 = new JButton();
private JButton jb3 = new JButton();
private final Font font = new Font("Serif", Font.PLAIN, 12);
public UITests() {
//Creating the top panel for the JLabel
JPanel panelA = new JPanel(new BorderLayout());
txtWindow.setForeground(Color.white);
txtWindow.setFont(new Font("", Font.PLAIN, 15));
txtWindow.setText("<html><div style='text-align: center;'>" + "Mellon"
+ "</div></html>");
panelA.setBackground(Color.BLACK);
panelA.add(txtWindow, BorderLayout.NORTH);
//Creating the bottom panel for the JButtons
JPanel panelB = new JPanel(new GridLayout(3, 1, 5, 5));
//setting up button properties
jb1.setBackground(Color.BLACK);
jb2.setBackground(Color.BLACK);
jb3.setBackground(Color.BLACK);
jb1.setForeground(Color.white);
jb2.setForeground(Color.white);
jb3.setForeground(Color.white);
jb1.setFocusPainted(false); //Stopping highlighting of button
jb2.setFocusPainted(false);
jb3.setFocusPainted(false);
jb1.setFont(font);
jb2.setFont(font);
jb3.setFont(font);
panelB.setBackground(Color.BLACK);
panelB.add(jb1);
panelB.add(jb2);
panelB.add(jb3);
setLayout(new GridLayout(2, 1, 0, 0));
add(panelA);
add(panelB);
setTitle("Screen");
setSize(500, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new UITests();
}
}
Any help with this would be greatly appreciated, and apologies if there exists a very similar question; I did my best to find it!
Just use txtWindow.setHorizontalAlignment(JLabel.CENTER);, like so:
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame {
private JLabel txtWindow = new JLabel();
private JButton jb1 = new JButton();
private JButton jb2 = new JButton();
private JButton jb3 = new JButton();
private final Font font = new Font("Serif", Font.PLAIN, 12);
public Main() {
//Creating the top panel for the JLabel
JPanel panelA = new JPanel(new BorderLayout());
txtWindow.setForeground(Color.white);
txtWindow.setFont(new Font("", Font.PLAIN, 15));
txtWindow.setText("Mellon");
txtWindow.setHorizontalAlignment(JLabel.CENTER);
panelA.setBackground(Color.BLACK);
panelA.add(txtWindow, BorderLayout.NORTH);
//Creating the bottom panel for the JButtons
JPanel panelB = new JPanel(new GridLayout(3, 1, 5, 5));
//setting up button properties
jb1.setBackground(Color.BLACK);
jb2.setBackground(Color.BLACK);
jb3.setBackground(Color.BLACK);
jb1.setForeground(Color.white);
jb2.setForeground(Color.white);
jb3.setForeground(Color.white);
jb1.setFocusPainted(false); //Stopping highlighting of button
jb2.setFocusPainted(false);
jb3.setFocusPainted(false);
jb1.setFont(font);
jb2.setFont(font);
jb3.setFont(font);
panelB.setBackground(Color.BLACK);
panelB.add(jb1);
panelB.add(jb2);
panelB.add(jb3);
setLayout(new GridLayout(2, 1, 0, 0));
add(panelA);
add(panelB);
setTitle("Screen");
setSize(500, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
This will center the text in the label.
Then you won't need to add HTML, so you can also setText("Melon"); directly, instead of using HTML.
Edit 1
For both short and long strings though try combining your centered HTML div tag and the setHorizontalAlignment(JLabel.CENTER); call, like so:
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame {
private JLabel txtWindow = new JLabel();
private JButton jb1 = new JButton();
private JButton jb2 = new JButton();
private JButton jb3 = new JButton();
private final Font font = new Font("Serif", Font.PLAIN, 12);
public Main() {
//Creating the top panel for the JLabel
JPanel panelA = new JPanel(new BorderLayout());
txtWindow.setForeground(Color.white);
txtWindow.setFont(new Font("", Font.PLAIN, 15));
final String text = "Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon Mellon";
txtWindow.setText("<html><div style='text-align: center;'>" + text + "</div></html>");
txtWindow.setHorizontalAlignment(JLabel.CENTER);
panelA.setBackground(Color.BLACK);
panelA.add(txtWindow, BorderLayout.NORTH);
//Creating the bottom panel for the JButtons
JPanel panelB = new JPanel(new GridLayout(3, 1, 5, 5));
//setting up button properties
jb1.setBackground(Color.BLACK);
jb2.setBackground(Color.BLACK);
jb3.setBackground(Color.BLACK);
jb1.setForeground(Color.white);
jb2.setForeground(Color.white);
jb3.setForeground(Color.white);
jb1.setFocusPainted(false); //Stopping highlighting of button
jb2.setFocusPainted(false);
jb3.setFocusPainted(false);
jb1.setFont(font);
jb2.setFont(font);
jb3.setFont(font);
panelB.setBackground(Color.BLACK);
panelB.add(jb1);
panelB.add(jb2);
panelB.add(jb3);
setLayout(new GridLayout(2, 1, 0, 0));
add(panelA);
add(panelB);
setTitle("Screen");
setSize(500, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
Hope this is what you are looking for.

My GUI is not showing up fully

When I run my program, it only shows a few lines, and then it shows gray under it. Can someone explain to me why this happens? I wanted the grid layout to have 8 rows, which should include the labels and text boxes. I am not sure why only a few of them appear.
public class Application extends JFrame {
private JPanel panel;
private JLabel label1, label2, label3, label4, label5, label6, label7,
label8;
private JTextField text1, text2, text3, text4, text5, text6, text7, text8;
public Application() {
JFrame gui = new JFrame();
gui.setLayout(new GridLayout(8, 2));
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setTitle("Vacation Expenses");
gui.setSize(500, 500);
panel = new JPanel();
gui.add(panel);
label1 = new JLabel("Number of days on the trip");
label2 = new JLabel("Amount of airfare");
label3 = new JLabel("Amount of car rental fees");
label4 = new JLabel(
"Number of miles driven, if a private vehicle was used");
label5 = new JLabel("Amount of parking fees, if any");
label6 = new JLabel("Amount of taxi charges, if any");
label7 = new JLabel("Conference or seminar registration fees, if any");
label8 = new JLabel("Lodging charges, per night");
text1 = new JTextField("0", 10);
text2 = new JTextField("0", 10);
text3 = new JTextField("0", 10);
text4 = new JTextField("0", 10);
text5 = new JTextField("0", 10);
text6 = new JTextField("0", 10);
text7 = new JTextField("0", 10);
text8 = new JTextField("0", 10);
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(text3);
panel.add(label4);
panel.add(text4);
panel.add(label5);
panel.add(text5);
// JButton button = new JButton("Button");
// panel.add(button);
gui.setVisible(true);
}
public static void main(String[] args) {
new Application();
}
}
You should be setting the GridLayout to your panel and not the frame. The panel is the container for the components, so should be the one with the GridLayout
Get rid of gui.setLayout(new GridLayout(8, 2));
And use panel = new JPanel(new GridLayout(8, 2));
Side Notes
Also note you haven't added all your components. You've only added five of each. You're forgetting to add the other three.
Also, your class is already a JFrame. There's no need to create another one. Choose one or the other. Either use the instance JFrame and don't extends JFrame or extend JFrame and don't use the extra instance. I'd go with the former.
Also, it's best to pack() your frame, instead of setSize(). The pack() should be done after adding all your components.
Also, Swing apps should be run from the Event Dispatch Thread. You can accomplish this by wrapping the main method content in a SwingUtilities.invokeLater(...). See more at Initial Threads

How to set the height and the width of a textfield in Java?

I was trying to make my JTextField fill the width and set a height for it but still failed. I tried adding the code setPreferredSize(new Dimension(320,200)); but still failed. Is there any way I can make my JTextField fill the width and set the height to 200 or something?
You should not play with the height. Let the text field determine the height based on the font used.
If you want to control the width of the text field then you can use
textField.setColumns(...);
to let the text field determine the preferred width.
Or if you want the width to be the entire width of the parent panel then you need to use an appropriate layout. Maybe the NORTH of a BorderLayout.
See the Swing tutorial on Layout Managers for more information.
set the height to 200
Set the Font to a large variant (150+ px). As already mentioned, control the width using columns, and use a layout manager (or constraint) that will respect the preferred width & height.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class BigTextField {
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
// the GUI as seen by the user (without frame)
JPanel gui = new JPanel(new FlowLayout(5));
gui.setBorder(new EmptyBorder(2, 3, 2, 3));
// Create big text fields & add them to the GUI
String s = "Hello!";
JTextField tf1 = new JTextField(s, 1);
Font bigFont = tf1.getFont().deriveFont(Font.PLAIN, 150f);
tf1.setFont(bigFont);
gui.add(tf1);
JTextField tf2 = new JTextField(s, 2);
tf2.setFont(bigFont);
gui.add(tf2);
JTextField tf3 = new JTextField(s, 3);
tf3.setFont(bigFont);
gui.add(tf3);
gui.setBackground(Color.WHITE);
JFrame f = new JFrame("Big Text Fields");
f.add(gui);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
There's a way which maybe not perfect, but can meet your requirement. The main point here is use a special dimension to restrict the height. But at the same time, width actually is free, as the max width is big enough.
package test;
import java.awt.*;
import javax.swing.*;
public final class TestFrame extends Frame{
public TestFrame(){
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p.setPreferredSize(new Dimension(500, 200));
p.setMaximumSize(new Dimension(10000, 200));
p.add(new JLabel("TEST: "));
JPanel p1 = new JPanel();
p1.setLayout(new BoxLayout(p1, BoxLayout.X_AXIS));
p1.setMaximumSize(new Dimension(10000, 200));
p1.add(new JTextField(50));
p.add(p1);
this.setLayout(new BorderLayout());
this.add(p, BorderLayout.CENTER);
}
//TODO: GUI CREATE
}
xyz.setColumns() method is control the width of TextField.
import java.awt.*;
import javax.swing.*;
class miniproj extends JFrame {
public static void main(String[] args)
{
JFrame frame=new JFrame();
JPanel panel=new JPanel();
frame.setSize(400,400);
frame.setTitle("Registration");
JLabel lablename=new JLabel("Enter your name");
TextField tname=new TextField(30);
tname.setColumns(45);
JLabel lableemail=new JLabel("Enter your Email");
TextField email=new TextField(30);
email.setColumns(45);
JLabel lableaddress=new JLabel("Enter your address");
TextField address=new TextField(30);
address.setColumns(45);
address.setFont(Font.getFont(Font.SERIF));
JLabel lablepass=new JLabel("Enter your password");
TextField pass=new TextField(30);
pass.setColumns(45);
JButton login=new JButton();
JButton create=new JButton();
login.setPreferredSize(new Dimension(90,30));
login.setText("Login");
create.setPreferredSize(new Dimension(90,30));
create.setText("Create");
panel.add(lablename);
panel.add(tname);
panel.add(lableemail);
panel.add(email);
panel.add(lableaddress);
panel.add(address);
panel.add(lablepass);
panel.add(pass);
panel.add(create);
panel.add(login);
frame.add(panel);
frame.setVisible(true);
}
}
What type of LayoutManager are you using for the panel you're adding the JTextField to?
Different layout managers approach sizing elements on them in different ways, some respect SetPreferredSize(), while others will scale the compoenents to fit their container.
See: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
ps. this has nothing to do with eclipse, its java.
f.setLayout(null);
add the above lines ( f is a JFrame or a Container where you have added the JTestField )
But try to learn 'LayoutManager' in java ; refer to other answers for the links of the tutorials .Or try This http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
maybe applying an EmptyBorder to your JTextField would be the simplest solution to simulate a height, and for the width use the setColumns() method
JTextField input=new JTextField();
input.setColumns(10);
input.setBorder(new EmptyBorder(15,0,15,0);
the 4 arguments of the EmptyBorder constructor are: top, left, bottom and right respectively.
Or if you want something more technical you can override the getPreferredSize, getMinimumSize and getMaximumSize methods so that it returns the values ​​you want to apply as width and height.
JTexField input=new JTextField(){
#Override
public Dimension getPreferredSize(){
return new Dimension(200,40);
};
public Dimension getMinimumSize(){
return new Dimension(200,40);
};
public Dimension getMaximumSize(){
return new Dimension(200,40);
};
};
package myguo;
import javax.swing.*;
public class MyGuo {
JFrame f;
JButton bt1 , bt2 ;
JTextField t1,t2;
JLabel l1,l2;
MyGuo(){
f=new JFrame("LOG IN FORM");
f.setLocation(500,300);
f.setSize(600,500);
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1=new JLabel("NAME");
l1.setBounds(50,70,80,30);
l2=new JLabel("PASSWORD");
l2.setBounds(50,100,80,30);
t1=new JTextField();
t1.setBounds(140, 70, 200,30);
t2=new JTextField();
t2.setBounds(140, 110, 200,30);
bt1 =new JButton("LOG IN");
bt1.setBounds(150,150,80,30);
bt2 =new JButton("CLEAR");
bt2.setBounds(235,150,80,30);
f.add(l1);
f.add(l2);
f.add(t1);
f.add(t2);
f.add(bt1);
f.add(bt2);
f.setVisible(true);
}
public static void main(String[] args) {
MyGuo myGuo = new MyGuo();
}
}
setBounds is working only in BorderLayout use BorderLayout for frame or container or panel and use setBounds to set the width and height of text field.
see this code simple code
import java.awt.*;
import javax.swing.*;
class uni1
{
public static void main(String[] args)
{
JFrame frm = new JFrame();
TextField txt = new TextField();
txt.setBounds(0, 0, 1200, 400);
frm.add(txt,BorderLayout.NORTH);
frm.setLayout(new BorderLayout());
frm.setVisible(true);
frm.setDefaultCloseOperation(frm.EXIT_ON_CLOSE);
}
}

JTextField Fixed Height

How do I get the JTextField to have a fixed height when the Frame is maximized? I want it to look sort of similar to the Skype application on Ubuntu.
private JTextField username;
private JPasswordField password;
private JLabel usernamelabel;
private JLabel passwordlabel;
public LoginPanel(){
setSize(200,200);
setLayout(new GridLayout(4,4));
setBackground(new Color(85,153,187));
setBorder(BorderFactory.createEmptyBorder(70, 70, 70, 70));
username = new JTextField();
password = new JPasswordField();
usernamelabel= new JLabel("Username");
passwordlabel= new JLabel("Password");
username.setBounds(5, 5, 100, 100);
username.setPreferredSize(new Dimension(80,20));
password.setPreferredSize(new Dimension(80,20));
add(usernamelabel);
add(username);
add(passwordlabel);
add(password);
Don't use a layout other than GridLayout or put the text field in another panel that has a FlowLayout that sits inside of your GridLayout.
I think this solves your problem.
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main {
/**
* #param args
*/
public static void main(String[] args) {
JFrame failFrame = new JFrame();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(0, 4));
JPanel leftTextFieldPanel = new JPanel();
leftTextFieldPanel.setLayout(new FlowLayout());
JPanel rightTextFieldPanel = new JPanel();
rightTextFieldPanel.setLayout(new FlowLayout());
JTextField leftTextField = new JTextField();
leftTextField.setPreferredSize(new Dimension(150,20));
JTextField rightTextField = new JTextField();
rightTextField.setPreferredSize(new Dimension(150,20));
leftTextFieldPanel.add(leftTextField);
mainPanel.add(leftTextFieldPanel);
rightTextFieldPanel.add(rightTextField);
mainPanel.add(rightTextFieldPanel);
mainPanel.add(new JPanel());
mainPanel.add(rightTextFieldPanel);
mainPanel.add(new JPanel());
failFrame.add(mainPanel);
failFrame.setSize(600, 400);
failFrame.setLocationRelativeTo(null);
failFrame.setVisible(true);
}
}
I used nested Layouts. On the Top is your GridLayout and the textfields are in a FlowLayout which are added on the gridLayout panel. So the textfields are no longer stretched.
You can refer to here.
The setMaximumSize method can be found in JComponent and the JTextField gets to use this method from JComponent.
EDIT: However, it seems its not a good practice to hardcode the size of the JTextField particularly when you are running it on multiple platforms as you may face issues with the font-size.
Thanks for all the help guys, hope this helps out others in the future. I got the visual look that I was looking for.
private JTextField username;
private JPasswordField password;
private JPanel [] login = {new JPanel(), new JPanel()};
private JLabel usernamelabel;
private JLabel passwordlabel;
public LoginPanel(){
setSize(200,200);
setBackground(new Color(85,153,187));
setBorder(BorderFactory.createEmptyBorder(70, 70, 70, 70));
username = new JTextField();
password = new JPasswordField();
usernamelabel= new JLabel("Username");
passwordlabel= new JLabel("Password");
login[0].add(usernamelabel);
login[0].add(username);
login[1].add(passwordlabel);
login[1].add(password);
for(JPanel p : login){
p.setBackground(new Color(85,153,187));
this.add(p);
}
username.setBounds(5, 5, 100, 100);
username.setPreferredSize(new Dimension(120,20));
password.setPreferredSize(new Dimension(120,20));
}
In a pinch, doing
tf.setMaximumSize(tf.getMinimumSize())
will keep it constant size and platform neutral.

Categories

Resources