Page 1 of 1

Netbeans ( java coding )

Posted: Mon May 02, 2011 1:14 am
by kinggenton
Ok guys i am creating a GUI with two buttons and a textfield
what i need help in is how when i press a button
it uses the info in my textfield to generate my answer!
this is what i am looking for

so lets say my program asked my name
i enter Darnelle in the field and one of my buttons i need it to use that name!
and for output give me Hi Darnelle thank you for coming

and i know how to import! JOptionPane its not part of this exercise!
i just need some code to help use the info from the textfield thanks!

Re: Netbeans ( java coding )

Posted: Mon May 02, 2011 5:06 pm
by davidFashion
Hi.

Here is a complete program that shows how, when a button is pressed, that the information is read from the text field and displayed in a JOptionPane.

Fundamentally, a Java ActionListener object is added to the button. At the place in the code where the ActionListener is added to the button, the ActionListerer object has access to the TextField object. Hence, the ActionListerer object can read the text that exists in the TextField object.

Code: Select all

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Window {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                final JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);

                JPanel panel = new JPanel(new GridLayout(2, 1));
                frame.add(panel);
                
                JButton button = new JButton("Press Me");
                final JTextField textfield = new JTextField(25);

                button.addActionListener(new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        JOptionPane.showMessageDialog(frame, textfield.getText(), "Message", JOptionPane.INFORMATION_MESSAGE);
                    }
                });
                
                panel.add(button);                
                panel.add(textfield);
                
                frame.pack();
            }
        });
    }

}


Re: Netbeans ( java coding )

Posted: Tue Oct 25, 2011 11:02 am
by chorinne
How to disable the Java Runtime error messages saying to debug? On one of the Google sites my volunteer group uses, I constantly get about 7 Java Runtime error messages asking me if I want to debug. It is extremely annoying, and I'd like to know how to disable this feature.
___________________________
keyword research ~ keyword tool ~ keyword tracking ~ affiliate elite