Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Can't edit JTextField from within a popup window

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

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


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Can't edit JTextField from within a popup window

    Try giving the popup panel a preferred size, e.g:
    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:
    ...
    ThePopUp.show();
    Text1.requestFocus();
    Last edited by dlorde; June 6th, 2011 at 11:11 AM.

  3. The Following User Says Thank You to dlorde For This Useful Post:

    One (June 6th, 2011)

  4. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Can't edit JTextField from within a popup window

    Quote Originally Posted by dlorde View Post
    Try giving the popup panel a preferred size, e.g:
    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:
    ...
    ThePopUp.show();
    Text1.requestFocus();
    I tried doing those things but it still would not work.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?

  6. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Can't edit JTextField from within a popup window

    Quote Originally Posted by Norm View Post
    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.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Can't edit JTextField from within a popup window

    You forgot:
    What does it do when you execute it?

  8. #7
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Can't edit JTextField from within a popup window

    Quote Originally Posted by Norm View Post
    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.

  9. #8
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  10. #9
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Can't edit JTextField from within a popup window

    Quote Originally Posted by One View Post
    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?

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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 ??

  12. The Following User Says Thank You to Norm For This Useful Post:

    One (June 7th, 2011)

  13. #11
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default 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.
    Last edited by dlorde; June 7th, 2011 at 04:35 AM.

Similar Threads

  1. jTable popup box
    By _lithium_ in forum AWT / Java Swing
    Replies: 1
    Last Post: March 13th, 2011, 12:05 PM
  2. Craating non active window ,and inputing to the non active window.
    By java-beginner in forum Java Theory & Questions
    Replies: 0
    Last Post: February 25th, 2011, 10:13 PM
  3. jsp popup
    By hussain4hunt in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: February 1st, 2011, 09:27 AM
  4. popup menu
    By antonio69 in forum AWT / Java Swing
    Replies: 1
    Last Post: May 20th, 2010, 04:24 AM
  5. Replies: 1
    Last Post: October 23rd, 2009, 03:17 PM