Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing to a file HELP HELP using actionlistener

Status
Not open for further replies.

tig12

Programmer
Feb 27, 2007
20
0
0
GB
Can anyone help me and tell me how to write to a field after someone has pressed the update button in the frame.

I Have the texfield and the button but dont know how to use actionlistener and send info to file.

Thank you

here is my code:

import javax.swing.*;

import java.awt.event.*;
import java.awt.*;
import javax.swing.JComponent.*;


public class update extends JFrame
{
// instance variables - replace the example below with your own
private JTextField text;

private JButton button1;

/**
* Constructor for objects of class update
*/
public update()
{
// initialise instance variables

GridBagLayout layout = new GridBagLayout();
GridBagConstraints layoutConstraints = new GridBagConstraints();
getContentPane().setLayout(layout);



JTextField text = new JTextField ("");
layoutConstraints.gridx = 2; layoutConstraints.gridy = 1;
layoutConstraints.gridwidth =1; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.ipadx = 2;
layoutConstraints.ipady = 0;
layoutConstraints.insets = new Insets(150, 50, 250, 50);
layoutConstraints.anchor = GridBagConstraints.NORTH;
layoutConstraints.weightx = 1.0; layoutConstraints.weighty = 1.0;
layout.setConstraints(text, layoutConstraints);
getContentPane().add(text);

button1 = new JButton("Update");
layoutConstraints.gridx = 2; layoutConstraints.gridy = 1;
layoutConstraints.gridwidth =1; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.HORIZONTAL;
layoutConstraints.ipadx = 2;
layoutConstraints.ipady = 0;
layoutConstraints.insets = new Insets(530, 180, 3, 50);
layoutConstraints.anchor = GridBagConstraints.NORTH;
layoutConstraints.weightx = 1.0; layoutConstraints.weighty = 1.0;
layout.setConstraints(button1, layoutConstraints);
getContentPane().add(button1);




}

//This makes the GUI
public static void main( String args[] )
{
// Create an instance of the test application
update mainFrame = new update();
mainFrame.setSize(600,800);
mainFrame.setVisible( true );
}

}
 
Something like this placed after button is created.

Code:
button1.addActionListener(new ActionListener(){

  public void actionPerformed(ActionEvent e){
    update.this.text.setText("Boo");
  }
});

Tim
 
Also, why do you put HELP HELP in the middle of all your thread title lines? It doesn't add anything - almost everyone here who starts a thread wants help in some form or another, unless they are posting a FAQ...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Yeah, your post titles are quite good apart from that needy 'HELP HELP' stuff.

By the way, in my example above you won't need the update.this. bit.

Code:
button1.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
    text.setText("Boo");
  }
});

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top