I have a JButton
that I would like to keep at the very right of a JTextField
, regardless of how I scale the window. I am aware of BorderLayout.EAST
, but that doesn't seem to work. This is my current code (userText
is my JTextField
):
imageButton = new JButton("Attach an Image");
if(System.getProperty("os.name").equals("Mac OS X")){
imageButton.setLocation(455, 0);
imageButton.setSize(150, 30);
} else {
imageButton.setLocation(435, 0);
imageButton.setSize(150, 20);
}
imageButton.addActionListener(
//SOME FUNCTIONALITY CODE HERE
);
userText.add(imageButton);
I know this code is very bad. It produces this if I don't resale anything (disregard what the message is):
So this looks all fine (sorry I cropped it a bit poorly), but when I resale it...
This is obviously not good looking at all. When I chamge userText.add(imageButton)
to userText.add(imageButton, BorderLayout.EAST)
the button simply stays in the top left corner. When I tried adding this to the JFrame
, it was just a large button to the right side of the JTextArea
, so I'm not quite sure what to do?
So, how can I get the button stay at the right side of the JTextField
and should I even be adding the button to the JTextField
or should I be adding it to some other component?
As per request here is a simple but full example (sorry about the indentation):
public class Test extends JFrame{
private JTextField userText;
private JButton imageButton;
private JTextArea chatWindow;
public Test(){
super("Test");
userText = new JTextField();
add(userText, BorderLayout.NORTH);
imageButton = new JButton("Problem Button");
if(System.getProperty("os.name").equals("Mac OS X")){
imageButton.setLocation(455, 0);
imageButton.setSize(150, 30);
}
else{
imageButton.setLocation(435, 0);
imageButton.setSize(150, 20);
}
userText.add(imageButton);
chatWindow = new JTextArea();
setSize(600, 300);
setVisible(true);
}
public static void main(String[] args) {
Test test = new Test();
}
}