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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JPanel question

Status
Not open for further replies.

thumpkidd9

Programmer
Mar 27, 2005
42
0
0
US
Hello,

I have a bunch of components in a JPanel, some 5 JTextFields and JButtons. The buttons were added to the JPanel first, then the textfields. I was wondering if there was a command to set the position to the textfields when I say, click a button.

Thanks,

~mp
 
I guess setLocation can be a good starting point.

Cheers,
Dian
 
Not if you're using a LayoutManager on the panel. Without one, setLocation will probably work. With one, the position of the components are constrained by the LayoutManager.

Tim
 
Ok, well how does setLocation work?

say I want to default to 2nd textfield, here is just some quick code to get the idea

Code:
//class fields
JFrame frame = new JFrame();
JPanel pane = new JPanel();
JTextField textfield = new JTextField();
JTextField textfield2 = new JTextField();

// inside some method
JButton button = new JButton();
button.addActionListener(this);

pane.add(textfield);
pane.add(textfield2);
.
.
.
public void actionPerformed(ActionEvent e){ 
   Object source = e.getSource();
   if(source==button) {
     // what goes here
   }
}

I tried filling in frame.setLocation(textField2) and pane.setLocation(textField2) and neither recognized the method setLocation().

Thanks,
~mp
 
Do you not use the Java API Docs? It saves a lot of time. Anyway, setLocation() is a method defined an a java.awt.Component (and available to its subclasses) which takes either an x, y coordinate (ints) or a java.awt.Point instance. It moves the component to a new physical location within its container (LayoutManagers usually make this impossible).

From the context of your last post, it looks more like you are wanting to set the focus to a particular component. For that, use the requestFocus() method on the component needing to acquire the focus.

Tim
 
Thanks, I got it working. Good idea about testing all the methods I will remember that in future.

Thanks,
~mp
 
Thumpkidd9, I think Dian was being 'ironic' with his 'way of testing' remark. It's not a performant way to write code. Use the Java Apidocs. You CANNOT be an effective Java programmer without them (unless you are telepathic).

Tim
 
Arghh, damned english, you can never know if your irony will be understood. But tim is out there to keep everyhting in order ...

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top