The Artima Developer Community
Sponsored Link

Java Answers Forum
listen for button event

1 reply on 1 page. Most recent reply: Mar 11, 2005 12:33 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Geoff Tait

Posts: 1
Nickname: dogbody
Registered: Mar, 2005

listen for button event Posted: Mar 10, 2005 8:57 PM
Reply to this message Reply
Advertisement
I'm trying to get information entered in a gui to be available from the parent class used to show the gui.

I've made up a bare bones example. I have one parent class and one child gui class. The gui has one textbox and one button.

I want the line System.out.println(gc.getTextValue()); to be executed in SomeClass when the button on the gui is pressed.

package test;
public class SomeClass{

public static void main(String[] args) {
GuiClass1 gc = new GuiClass1();
gc.setVisible(true);
System.out.println(gc.getTextValue());
}
}


Here's the code for the gui

package test;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;

public class GuiClass1 extends JFrame {

private javax.swing.JPanel jContentPane = null;
private JTextField jTextField = null;
private JButton jButton = null;
private String value = "";

public String getTextValue() {
return value;
}

private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(71, 54, 126, 23);
}
return jTextField;
}

private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(86, 91, 88, 17);
jButton.setText("ok");

jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
value = jTextField.getText();
}
});
}
return jButton;
}

public GuiClass1() {
initialize();
}

private void initialize() {
this.setSize(300,200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}

private javax.swing.JPanel getJContentPane() {
if(jContentPane == null) {
jContentPane = new javax.swing.JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}
}


Thanks for your help
GT


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: listen for button event Posted: Mar 11, 2005 12:33 AM
Reply to this message Reply
What's the problem?

Just add the line to the actionlistener.

			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					value = jTextField.getText();
					System.out.println(this.getTextValue()); //or System.out.println(value);
				}
			});

Flat View: This topic has 1 reply on 1 page
Topic: BufferedReader Previous Topic   Next Topic Topic: how to post message to server in an applet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use