2

I have:

public class BaseStationFrame1 extends JFrame
{

    JButton activateButton;
    JButton deactivateButton;
    BaseStation bs;

    JTextField networkIdField;
    JTextField portField;



    public BaseStationFrame1(BaseStation _bs){

        bs = _bs;

        setTitle("Base Station");
        setSize(600,500); 
        setLocation(100,200);  
        setVisible(true);

        activateButton = new JButton("Activate");
        deactivateButton = new JButton("Deactivate");
        Container content = this.getContentPane();
        content.setBackground(Color.white);
        content.setLayout(new FlowLayout()); 
        content.add(activateButton);
        content.add(deactivateButton);

        networkIdField = new JTextField("networkId : "+ bs.getNetworkId());
        networkIdField.setEditable(false);

        content.add(networkIdField);

        portField = new JTextField("portId : "+ bs.getPort());
        portField.setEditable(false);

        content.add(portField);}
    }

My problem is that i don't want the two TextFields to appear on the right of Activate and Deactivate buttons but below them. How can i fix that?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
nikos
  • 2,893
  • 11
  • 30
  • 39

2 Answers2

3

Specify your layout manager, like this:

content.setLayout(new GridLayout(2,2));

That would use the Grid Layout Manager to establish a grid with 2 columns and 2 rows, that your components would then be placed in.

The layout manager you are currently using, FlowLayout, only adds contents onto the end of the current row. it will wrap around once it reaches the constrained edge of the pane, though. You should also check the other layout managers here

You could alternatively use GridBagLayout , but you will have to specify a GridBagConstraints object you then add alongside the individual elements, like so:

content.add(networkIdField, gridConstraints);

see more on that in the linked tutorial.

Sheriff
  • 495
  • 4
  • 16
-1

can I suggest that you use a Null Layout for the parent component?

setLayout(null);

then use a setBounds(xPos,yPos, Width, Height);

to position the components on the panel etc?

Doing this will prevent Java's UI Manager to manage the components to the Frame, Panel etc.

That seems to be the easiest and less painful way.

Regards

Theron084
  • 155
  • 2
  • 3
  • 11
  • -1 for null layout suggestion. Layout manages exist for a reason. – camickr Dec 08 '11 at 20:52
  • Ok, thought it would be the easiest, then you can try GridBagLayout which will also require you to set rows and Columns to position the component. [GridBagLayout](http://www.google.co.za/url?sa=t&rct=j&q=gridBagLayout&source=web&cd=1&ved=0CCMQFjAA&url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2Ftutorial%2Fuiswing%2Flayout%2Fgridbag.html&ei=PSThTqm8Eu-imQXq7eH0BA&usg=AFQjCNHKarD9oHgot_7Z7zHPurIFKvX56w&sig2=8kNOBW2nvBxgMdq35mQOKA&cad=rja). It might work, but with a lot of trouble. – Theron084 Dec 08 '11 at 20:56