Search code examples
javaswinggraphicsjframejbutton

Getting a JButton to Stay at the Side of a JTextField


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):

What it looks like now

So this looks all fine (sorry I cropped it a bit poorly), but when I resale it... Not going to work

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();

}

}

Solution

  • Just use a JPanel for the button. Set this panel layout to FlowLayout and set its alignment to RIGHT. Then add it to the NORTH position of your frame.

    Here is a sample code:

    import java.awt.BorderLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class FrameTest extends JFrame {
    
        private JTextField userText;
        private JButton imageButton;
        private JTextArea chatWindow;
    
        public FrameTest() {
    
            super("Test");
    
            userText = new JTextField();
    
            JPanel topPanel = new JPanel(new BorderLayout());
            topPanel.add(userText, BorderLayout.CENTER);
    
            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);
    
            }
    
            topPanel.add(imageButton, BorderLayout.EAST);
            add(topPanel, BorderLayout.NORTH);
            chatWindow = new JTextArea();
    
            setSize(600, 300);
            setVisible(true);
    
        }
    
        public static void main(String[] args) {
    
            FrameTest test = new FrameTest();
    
        }
    
    }