Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create fixed size TextField in the Center of a BordLayout?

Status
Not open for further replies.

Landzg

Programmer
Aug 2, 2001
8
US
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 a question. Have you tryed Sun's java forum at ? They have a ton of experts there that helped create the java language. You'll get an answer almost imediately (if you can log in). You must be a member to post though (membership is free). "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
Any component added to the CENTER region of the BorderLayout will expand to fill the whole space of the region. Sorry, as far as I am aware this always happens and it means you may have to consider another Layout manager OR just setting it to null and hardcoding in the JTextfield position on the the screen.
bruce21.jpg
 

Hey try this........


import java.awt.*;

public class FrameEx2 extends Frame{

TextField textField1 = new TextField();

FrameEx2(){

setLayout(null);
setSize(500,400);
setVisible(true);
add(textField1);
textField1.setBounds(180,168,129,29);
}

public static void main(String args[]) {
new FrameEx2();
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top