Hi everyone,
I have being tried to create some fixed size TextFields in the Center of a BorderLayout but even I overloaded the setPreferredSize, setMaxiumSize and setMinimalSize of the TextField, their sizes are not fixed. I know I could put them into the NORTH as this will respect the preferred size, but that is not what I want because of other requirements. So I am throwing the code here for any help.
import javax.swing.*;
import java.awt.*;
public class MyTextField extends JPanel {
public MyTextField() {
setLayout(new GridLayout(0, 1));
JTextField tf = new JTextField(5) {
public void setPreferredSize(Dimension d) {
super.setPreferredSize(new Dimension(20, 100));
}
public void setMaximumSize(Dimension d) {
setPreferredSize(d);
}
public void setMinimalSize(Dimension d) {
setPreferredSize(d);
}
};
add(tf);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(new MyTextField(), BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
}
Thank you very much!
Land
I have being tried to create some fixed size TextFields in the Center of a BorderLayout but even I overloaded the setPreferredSize, setMaxiumSize and setMinimalSize of the TextField, their sizes are not fixed. I know I could put them into the NORTH as this will respect the preferred size, but that is not what I want because of other requirements. So I am throwing the code here for any help.
import javax.swing.*;
import java.awt.*;
public class MyTextField extends JPanel {
public MyTextField() {
setLayout(new GridLayout(0, 1));
JTextField tf = new JTextField(5) {
public void setPreferredSize(Dimension d) {
super.setPreferredSize(new Dimension(20, 100));
}
public void setMaximumSize(Dimension d) {
setPreferredSize(d);
}
public void setMinimalSize(Dimension d) {
setPreferredSize(d);
}
};
add(tf);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(new MyTextField(), BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
}
Thank you very much!
Land