Solved

click button to print text on a new window.

Posted on 2004-03-27
5
183 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
5 Comments
 
LVL 6

Accepted Solution

by:
dorothy2 earned 25 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 25 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 95
How to convert String matching to regex in java 4 30
hibernate insert example 13 28
how to install java on RHEL image on EC2 4 26
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

785 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