I am trying to add a JTextField at a position where a mouse click happens.The size of the text field should be variable based on the text entered. Below is my code.
public class Paint {
public static void main(String[] args) {
final JFrame frame = new JFrame("Test TextField");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setSize(400, 250);
final JTextField text = new JTextField();
frame.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent me) {
}
});
frame.setVisible(true);
}
}
I want to add the text field at the position where the mouse click happened. The size of the text field should increase based on the text entered in the text field. Can someone let me know how to implement the mouse click method to achieve the same?
I tried to set bounds of the text box using the event x and y positions like
text.setBounds(event.getX(), event.getY(), event.getX(), event.getY());
but with this the size of textField is same as the full frame. but i want a small text box with varying size
First, we need to remove the layout to be able to add component where we want
frame.setLayout(null);
Then, we can check the result of the event with :
frame.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent event) {
System.out.println(event.getX() + " " + event.getY());
}
}
we can notice if we click on the upper left corner that we can't get a (0,0) coordinate. Because A frame is the complete screen, so we need to use the ContentPane or we can simply use a JPanel keep it simple.
final JPanel panel = new JPanel();
panel.setLayout(null);
frame.getContentPane().add(panel);
panel.addMouseListener(new MouseAdapter() {}); //Use the MouseAdapter to not be force to override the full interface of MouseListener.
Now, let's add a component on click. First, we create it during the event (to create a new one each time and we set the size :
final JTextField text = new JTextField();
text.setSize(new Dimension(50, 18)); //I took those randomly...
The important part is the location, set it to the component like :
text.setLocation(event.getX(), event.getY());
And add it to the panel
panel.add(text);
The result is a new JTextField added at the specific position of the click.
public static void main(String[] args) {
final JFrame frame = new JFrame("Test TextField");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 250);
final JPanel panel = new JPanel();
panel.setLayout(null);
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent event) {
final JTextField text = new JTextField("abc");
text.setLocation(event.getX(), event.getY());
text.setSize(new Dimension(50, 18));
panel.add(text);
}
});
frame.getContentPane().add(panel);
frame.setVisible(true);
}
Note that this is not the best usage a Swing, layouts are there to build efficient screen easily.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With