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?
This question is related to
java
swing
jtextfield
jcreator
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();
}
}
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);
}
}
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
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
}
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.
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.
Source: Stackoverflow.com