Solved

click button to print text on a new window.

Posted on 2004-03-27
5
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:

695 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