Search code examples
javaswingjbuttonjtextfield

Having a JTextField popup at a certain location after clicking a JButton


I have my three JButtons located where I want them (at the top center of the frame), and when the user clicks one, a JTextField pops up in the BoxLayout like wanted.

The problem is, when the JTextField shows up, it is to the left of the buttons, and it moves them.

I tried setting the alignment of the JTextField and using various glues, but the JTextField doesn't move.

If I want to have the JTextField pop up below my JButtons and in the center of the screen, what should I use?

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class Library extends JFrame implements ActionListener {

    private JFrame jf1;
    private JPanel jp1;
    private JTextField jtf1;
    private JButton jb1;
    private JButton jb2;
    private JButton jb3;

    public Library() {
        try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(Exception q) {
        q.printStackTrace();
        }
    JFrame.setDefaultLookAndFeelDecorated(false);
    jf1 = new JFrame("Library");
    jf1.setVisible(true);
    jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf1.setSize(1080, 900);
    jf1.setResizable(true);

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    jf1.setLocation(dim.width/2-jf1.getSize().width/2, dim.height/2-jf1.getSize().height/2);

    jp1 = (JPanel) jf1.getContentPane();
    jp1.setLayout(new BoxLayout(jp1, BoxLayout.LINE_AXIS));

    jb1 = new JButton("Genre");
    jb1.addActionListener(this);
    jb1.setMinimumSize(new Dimension(140, 60));
    jb1.setPreferredSize(new Dimension(150, 60));
    jb1.setMaximumSize(new Dimension(150, 60));
    jb1.setAlignmentY(-70.0f);
    jb2 = new JButton("Author");
    jb2.addActionListener(this);
    jb2.setMinimumSize(new Dimension(140, 60));
    jb2.setPreferredSize(new Dimension(150, 60));
    jb2.setMaximumSize(new Dimension(150, 60));
    jb2.setAlignmentY(-70.0f);
    jb3 = new JButton("Title");
    jb3.addActionListener(this);
    jb3.setMinimumSize(new Dimension(140, 60));
    jb3.setPreferredSize(new Dimension(150, 60));
    jb3.setMaximumSize(new Dimension(150, 60));
    jb3.setAlignmentY(-70.0f);



    jp1.add(Box.createHorizontalGlue());
    jp1.add(jb1);
    jp1.add(jb2);
    jp1.add(jb3);
    jp1.add(Box.createHorizontalGlue());

    jf1.validate();
}

@Override
public void actionPerformed(ActionEvent e) {
    Object code = e.getSource();
    if (code == jb1) {
        jtf1 = new JTextField("Enter Text");
        jtf1.setPreferredSize(new Dimension(200,20));
        jtf1.setMaximumSize(new Dimension(200,20));
        jtf1.setMinimumSize(new Dimension(10,10));
        jp1.add(jtf1);

        jp1.validate();
    }
    else if (code == jb2) {

    }
    else if (code == jb3) {

    }
}

public static void main(String[] args) {
    Library shoe = new Library();
    }
}

Solution

  • Suggestion: do not add/remove UI elements dynamically. Just add all of those things initially, and simply call setVisible(false) on your text field then.

    (instead of adding/removing fields using your action listener)