0

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

}

}
dsiegler19
  • 369
  • 1
  • 5
  • 20
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Mar 23 '16 at 21:47
  • You could use something like `BuddySupport` from SwingLab's SwingX libraries, for [example](http://stackoverflow.com/questions/20578568/java-swing-listen-an-action-in-a-text-field-of-a-form/20578601#20578601). I think Camrik has a version, I'm sure he'll post – MadProgrammer Mar 23 '16 at 21:50
  • `if(System.getProperty("os.name").equals("Mac OS X")){ imageButton.setLocation(455, 0);` *"I know this code is very bad"* That's a start. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Mar 23 '16 at 21:51
  • So, first picture is your desired design? – rdonuk Mar 23 '16 at 21:52
  • I guess. I would like it to look like that at any scale, but currently when I scale it, well that's the 2nd picture. – dsiegler19 Mar 23 '16 at 21:57

1 Answers1

1

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

    }

}
rdonuk
  • 3,921
  • 21
  • 39
  • [`BuddySupport`](http://stackoverflow.com/questions/20578568/java-swing-listen-an-action-in-a-text-field-of-a-form/20578601#20578601) – MadProgrammer Mar 23 '16 at 22:01
  • @dsiegler19 I updated my code based on your test example. – rdonuk Mar 23 '16 at 22:07
  • @MadProgrammer I heard for the first time about BuddySupport. It can be a better solution for this situation. But it is required swingx.jar right? Maybe dsiegler19 don't want to use extra jar, I don't know. – rdonuk Mar 23 '16 at 22:12
  • Sorry. I'm fairly new and often forget. – dsiegler19 Mar 23 '16 at 22:21
  • It comes down to needs and requirements, the buddy support will install the component inside the text field (which seems to be what the OP is trying to do), where as you've placed the components side by side - changing the requirements – MadProgrammer Mar 23 '16 at 22:29
  • But I think OP is not sure about it "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?". I think the requirement is only keep the button top right corner of the frame and just right side of the text field. – rdonuk Mar 23 '16 at 22:41