Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

click button to print text on a new window.

Posted on 2004-03-27
5
Medium Priority
?
215 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

647 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