Can't edit JTextField from within a popup window
In Java swing, I cannot edit a JTextField from within a popup window. The following code is a simple program that allows me to push a button and JTextField with the number 0 filled in appears. I want to be able to edit that 0 but I don't seem to be able to do it.
Code :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PopupTest {
public static void main(String[] args) {
final JFrame MainFrame = new JFrame();
MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel MainPanel = new JPanel();
MainPanel.setLayout(new BoxLayout(MainPanel,BoxLayout.Y_AXIS));
MainFrame.add(MainPanel);
final JLabel TheLabel = new JLabel("Test");
MainPanel.add(TheLabel);
final JButton PopUpButton = new JButton("Open PopUp");
MainPanel.add(PopUpButton);
PopUpButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
JPanel PopUpPanel = new JPanel();
PopUpPanel.setLayout(new BoxLayout(PopUpPanel,BoxLayout.Y_AXIS));
PopupFactory factory = PopupFactory.getSharedInstance();
final Popup ThePopUp = factory.getPopup(MainPanel,PopUpPanel,40,40);
JTextField Text1 = new JTextField("0");
PopUpPanel.add(Text1);
ThePopUp.show();
}
});
MainFrame.pack();
MainFrame.setVisible(true);
}
}
Re: Can't edit JTextField from within a popup window
Try giving the popup panel a preferred size, e.g:
Code :
JPanel PopUpPanel = new JPanel();
PopUpPanel.setPreferredSize(new Dimension(50,20));
...
It wouldn't hurt to request focus for the text field while you're at it:
Code :
...
ThePopUp.show();
Text1.requestFocus();
Re: Can't edit JTextField from within a popup window
Quote:
Originally Posted by
dlorde
Try giving the popup panel a preferred size, e.g:
Code :
JPanel PopUpPanel = new JPanel();
PopUpPanel.setPreferredSize(new Dimension(50,20));
...
It wouldn't hurt to request focus for the text field while you're at it:
Code :
...
ThePopUp.show();
Text1.requestFocus();
I tried doing those things but it still would not work.
Re: Can't edit JTextField from within a popup window
It works for me.
I get a small window with a button, I press the button and get a white textfield with a 0. I can enter new data into the textfield.
What does it do when you execute it? What OS and version of the JDK are you using?
Re: Can't edit JTextField from within a popup window
Quote:
Originally Posted by
Norm
It works for me.
I get a small window with a button, I press the button and get a white textfield with a 0. I can enter new data into the textfield.
What does it do when you execute it? What OS and version of the JDK are you using?
I am using Windows Vista, NetBeans 6.9.1, and Java 1.6.0_23.
Re: Can't edit JTextField from within a popup window
You forgot:
What does it do when you execute it?
Re: Can't edit JTextField from within a popup window
Quote:
Originally Posted by
Norm
You forgot:
What does it do when you execute it?
I get a small window that has a button that says "Open Popup". When I click on that button, I get a text field with a "0" filled in. When I click on the "0" it moves to the left or the right slightly, but pushing backspace does not delete it and pushing additional characters does not print the new characters.
Re: Can't edit JTextField from within a popup window
I just got it to work. Before, I was setting PreferredSize to 50,50, but it works when I set it to 50,20.
Re: Can't edit JTextField from within a popup window
Quote:
Originally Posted by
One
I just got it to work. Before, I was setting PreferredSize to 50,50, but it works when I set it to 50,20.
I'm still having some similar problems with other programs. Is there a way to tell what sizes will work and what sizes will not work?
Re: Can't edit JTextField from within a popup window
After some experimenting, I think the popup must be contained within and over the component.
Try adding this:
theFrame.setLocation(400, 300); // This causes edit to fail!!! Pop MUST be within component ???
then adjust this until it works:
final Popup thePopUp = factory.getPopup(MainPanel, popupPanel, 40, 40); // location ??
Re: Can't edit JTextField from within a popup window
The popup must be contained within the frame's content pane borders otherwise it can't/won't get focus. If you resize the main frame so the popup pops up within the content pane, it will work. It's probably a good idea to give the frame a minimum size and set the popup coordinates relative to the frame X,Y position and height & width (allowing for caption & borders), rather than an absolute screen coordinate, so it will popup in a usable state wherever the frame is moved and/or sized.