?
Solved

click button to print text on a new window.

Posted on 2004-03-27
5
Medium Priority
?
218 Views
Last Modified: 2010-03-31
Hi,

I'm not quite sure how write java code to print text on a new window / JOptionPane.. by clicking a button.

Thanks

Lee
0
Comment
Question by:LeeMY
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
dorothy2 earned 100 total points
ID: 10694486
iimport java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonExample extends JFrame {

   public ButtonExample() {
     // Add controls to JFrame
     Container container = this.getContentPane();
     JButton button = new JButton("Press to Continue");
     container.add(button);
     JTextField txtField = new JTextField();
     container.add(txtField);

    ButtonHandler handler = new ButtonHandler();
    listener.setTextField(txtField);
    button.addActionListener(handler);

    setSize(200, 100);
    setVisible(true);
   } // end of constructor
} // end of class

// put in same source code file
private class ButtonHandler implements ActionListener {
  private JTextField txtField;

 // pass textfield to ActionListener
  public void setTextField(JTextField field) {
      this.txtField = field;
  }

  // handle button press
  public void actionPerformed(ActionEvent event) {
     txtfield.addText("You pressed the button.");
  }

} // end of class

There are some variations. Some developers have the class containing the JFrame implement ActionListener, and put the actionPerformed method there. It's also possible to create the ActionListener class as an inner class within the JFrame subclass. This saves having to add a method to pass the TextField reference to the ActionListener.
   
Dorothy    
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 100 total points
ID: 10701417
>> how write java code to print text on a new window / JOptionPane

In the actionPerformed () method, you can use the showMessageDialog () method of JOptionPane, like:

JOptionPane.showMessageDialog ( <Parent-Frame>, <Message>, <TITLE>, JOptionPane.INFORMATION_MESSAGE ) ; // show an information message - you can use ERROR_MESSAGE, WARNING_MESSAGE, etc
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10857792
Please proceed with that recommendation.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This video teaches viewers about errors in exception handling.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question