JTextField to extend to end of JPanel

M

Mike Ditka

I'm adding several JButtons to a JPanel with a horizontal FlowLayout, then
adding a JTextField. I want the JTextField to take up all the space in the
JPanel not used by the JButtons. If the window is resized so that the panel
gets longer or shorter, I want the JTextField to be adjusted accordingly.

How can I build a horizontal panel with buttons and a text field at the end,
so that the text field takes up the remainder of the panel?

Thanks,
Mike
 
K

Knute Johnson

Mike said:
I'm adding several JButtons to a JPanel with a horizontal FlowLayout, then
adding a JTextField. I want the JTextField to take up all the space in the
JPanel not used by the JButtons. If the window is resized so that the panel
gets longer or shorter, I want the JTextField to be adjusted accordingly.

How can I build a horizontal panel with buttons and a text field at the end,
so that the text field takes up the remainder of the panel?

Thanks,
Mike

Mike:

See the code below for one way to get what you want. Your Buttons go on
one Panel with a FlowLayout and the Panel and your TextArea or Field go
in the center area of a BorderLayout. The BorderLayout will expand the
size of the center area when the window is resized.

import java.awt.*;
import java.awt.event.*;

public class test3 extends Frame {
public test3() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
setLayout(new BorderLayout());

Panel p = new Panel();
p.setLayout(new FlowLayout());

Button b = new Button("Button1");
p.add(b);

b = new Button("Button2");
p.add(b);

add(p,BorderLayout.WEST);

TextArea ta = new TextArea("TextArea",5,20);
add(ta,BorderLayout.CENTER);

pack();
setVisible(true);
}

public static void main(String[] args) {
new test3();
}
}
 
R

rfractal30

Mike said:
I'm adding several JButtons to a JPanel with a horizontal FlowLayout, then
adding a JTextField. I want the JTextField to take up all the space in the
JPanel not used by the JButtons. If the window is resized so that the panel
gets longer or shorter, I want the JTextField to be adjusted accordingly.

How can I build a horizontal panel with buttons and a text field at the end,
so that the text field takes up the remainder of the panel?

Thanks,
Mike

Try looking at the GridBagLayout (also GridBagConstraints). These will
allow you to assign proportions to individual elements.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top