Solved

click button to print text on a new window.

Posted on 2004-03-27
5
163 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:mayankeagle
mayankeagle 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:mayankeagle
ID: 10857792
Please proceed with that recommendation.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
noX challenge 17 76
where is session ID cache stored 1 38
word0 challenge 4 52
wordmultiple challenge 12 89
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now