Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Can't get value eneterd into JTextField to display.

  • 30-11-2012 4:11pm
    #1
    Registered Users, Registered Users 2 Posts: 12,692 ✭✭✭✭


    Hey, this is driving me insane. I want the user to enter their name in a JTextField, when they hit the JButton it will display their name is a showMessageDialog but I can't get the name to appear.
    This is the code for this section:
    JFrame newFrame = new JFrame("Delivery Details");

    Container pane = getContentPane();
    newFrame.setLayout(new FlowLayout());
    newFrame.setSize(new Dimension(250, 250));
    //newFrame.getContentPane().add(panel);
    newFrame.setLocation(300,300);
    newFrame.setSize(250,250);
    newFrame.setVisible(true);


    JPanel name = new JPanel();
    JLabel nameLabel = new JLabel();
    nameLabel.setText("Name :");
    JTextField nameField = new JTextField(15);
    String deliveryName = nameField.getText();
    name.add(nameLabel);
    newFrame.add(nameLabel);
    newFrame.add(nameField);
    deliveryName = nameField.getText();


    JButton submitButton = new JButton("submit");
    newFrame.add(submitButton);
    ButtonEventHandler handler = new ButtonEventHandler();
    submitButton.addActionListener(handler);





    }
    private class ButtonEventHandler implements ActionListener {


    public void actionPerformed(ActionEvent e)
    {
    String deliveryName="";



    if (e.getActionCommand().equals("submit"))
    {
    JOptionPane.showMessageDialog(null,"Your name is" + deliveryName,"Message",JOptionPane.INFORMATION_MESSAGE);
    }
    else
    JOptionPane.showMessageDialog(null,"Nothing");
    }}


Comments

  • Registered Users, Registered Users 2 Posts: 377 ✭✭CarefulNow


    nameField needs to be a Global Variable to be seen in the ButtonHandler class.
    Also the deliveryName string in the actionPerformed method has to get the text from the nameField at that point.


  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    Or you could pass the variables you need via the button handler constructor.


  • Moderators, Technology & Internet Moderators Posts: 1,336 Mod ✭✭✭✭croo


    private class ButtonEventHandler implements ActionListener {
    
    		public void actionPerformed(ActionEvent e) {
    			[B]String deliveryName = "";[/B]
    
    			if (e.getActionCommand().equals("submit")) {
    				JOptionPane.showMessageDialog(null, "Your name is"
    						+ deliveryName, "Message",
    						JOptionPane.INFORMATION_MESSAGE);
    			} else
    				JOptionPane.showMessageDialog(null, "Nothing");
    		}
     b	}
    

    Surely this ensures the name displayed will always be blank!?


  • Registered Users, Registered Users 2 Posts: 12,692 ✭✭✭✭siblers


    croo wrote: »
    private class ButtonEventHandler implements ActionListener {
    
    		public void actionPerformed(ActionEvent e) {
    			[B]String deliveryName = "";[/B]
    
    			if (e.getActionCommand().equals("submit")) {
    				JOptionPane.showMessageDialog(null, "Your name is"
    						+ deliveryName, "Message",
    						JOptionPane.INFORMATION_MESSAGE);
    			} else
    				JOptionPane.showMessageDialog(null, "Nothing");
    		}
     b	}
    

    Surely this ensures the name displayed will always be blank!?

    I had been trying it a few different ways and none were working, that was just my latest attempt.


  • Moderators, Technology & Internet Moderators Posts: 1,336 Mod ✭✭✭✭croo


    The simplest solution is to do as Carefulnow suggests.

    Because you create the JTextField in an actual method it is only visible and usable in the method it is defined in.
    If you were to define the JTextField variable outside of any method it would be a class variable and then available to any method of the class.

    If you wanted the variable to be available to other classes in the package ... and SUBCLASSES which ButtonEventHandler is... then you define it as protected (still a class variable outside of any method).

    I should point out that a key goal of object orientation is the concept of encapsulation. So you could define all your variables in a class as public class variables and have no problems with variable "scoping" but this would be very bad in terms of OO.

    Also note what CarefulNow mentions re the JTextField method getText .. to use the value entered in your popup dialog you could use nameField.getText()

    ps. it might be an idea to review the "modifiers" - public, protected & private.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 12,692 ✭✭✭✭siblers


    Thanks for the help, will hopefully get it sorted.


  • Moderators, Technology & Internet Moderators Posts: 1,336 Mod ✭✭✭✭croo


    Just looking at your code again and I see you were already using the nameField.getText().
    But I think I understand where your confusion is coming from...

    When you learn to code first, everything is procedural... 1 step after another. But when you start with GUI coding that all changes. The flow is no longer straight forward and what executes all depends on the "events" that are happening in the GUI.
    So in your original code, you had
    newFrame.add(nameField);
    [B]deliveryName = nameField.getText();[/B]
    
    But at this point the UI is just being built, so nameField has no text to get!
    You need to move that line so that it is executed at the point in time when the event occurs ... so to
    public void actionPerformed(ActionEvent e)
    {
    String deliveryName=nameField.getText(); <-HERE
    ...
    
    Which I'm guessing you probably had at one time - BUT because the nameField was a method variable it wasn't available and you were getting errors or defining it again in that method and getting a blank!?
    The other ealier suggestion to make nameField a class variable by moving its definition outside of the method will resolve that.


  • Registered Users, Registered Users 2 Posts: 12,692 ✭✭✭✭siblers


    Got it working, thanks so much! Really appreciate it.


  • Registered Users, Registered Users 2 Posts: 12,692 ✭✭✭✭siblers


    Is there any way for me to format the JTextFields so they align tidly? I've tried GridLayOut but it didn't really same to make any difference, I now I can mess around with the size of the JFrame so they'll fit that way but it doesnt offer much flexibilty.


  • Registered Users, Registered Users 2 Posts: 8,324 ✭✭✭chrislad


    siblers wrote: »
    Is there any way for me to format the JTextFields so they align tidly? I've tried GridLayOut but it didn't really same to make any difference, I now I can mess around with the size of the JFrame so they'll fit that way but it doesnt offer much flexibilty.

    I'm new enough to GUIs, but you can just use a null layout and then use .setBounds(int x, y, height, width) to set. That's what I did.


  • Advertisement
Advertisement