Solved

Show data in a textArea

Posted on 2003-11-17
5
285 Views
Last Modified: 2010-03-31
Hi All,
 I have a demo  program below that runs exactly as I want it to with a
 System in and out, and now in the second program below I want to move up appearance
 wise to show textFields and textArea within a frame so that
 the user can type the needed input data in a TextField and the
 program show the output in the TextArea.
 
I think that maybe I have the necessary textfields to get user to enter the name and age but I cannot get the name and age to show into the text area.

I appreciate your help in this matter. This is not homework.

Delphi3

package systeminouttotxtfieldinout;

import java.io.*;

public class textSystemInOut {
  public textSystemInOut() {
  }

  public static void main(String[] args) {

    InputStreamReader keyboard = new InputStreamReader(System.in);
    BufferedReader input = new BufferedReader(keyboard);
    try {
      System.out.println("Enter your name  ");
      System.out.print(" ");
      String name = input.readLine();
      System.out.println("Your Age   ");
      String age = input.readLine();
      int iage = (int) Integer.parseInt(age);
      System.out.println("My Name is " + name + " and twice my Age Is  " +
                         iage * 2);
      System.out.println("Thank you");

    }
    catch (Exception e) {
      System.out.println("error getting data");
    }
  }
}

package systeminouttotxtfieldinout;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;

public class framedTextFieldInOut
    extends JPanel {
  String name;
  String age;
  int iage;
  private BarPanel barPanel;
  public static JTextField nameField = null;
  public static JTextField ageField = null;
  public static JTextArea ShowInArea = null;

  private Action startAction = new AbstractAction("Process") {
    public void actionPerformed(ActionEvent displayData) {
    }
  };

  public framedTextFieldInOut() {
    setLayout(new BorderLayout());
    JPanel dataPane = new JPanel();
    barPanel = new BarPanel();
    barPanel.setBorder(
        BorderFactory.createLineBorder(getForeground(), 2));
    add(barPanel, BorderLayout.CENTER);
    JPanel buttonPanel = new JPanel();
    add(buttonPanel, BorderLayout.SOUTH);
    buttonPanel.add(new JButton(startAction));

    add(dataPane, BorderLayout.CENTER);
    dataPane.add(new JLabel("Name : "));

    nameField = new JTextField(10);
    dataPane.add(nameField);
    nameField.setText(name);
    nameField.setEnabled(false);
    nameField.addFocusListener(new FocusAdapter() {
      public void focusLost(FocusEvent e) {
        nameField.selectAll();
        name = nameField.getText();
      }
    });

    dataPane.add(new JLabel("Age :  "));
    ageField = new JTextField(10);
    dataPane.add(ageField);
    ageField.setText(age);
    ageField.setEnabled(false);
    ageField.addFocusListener(new FocusAdapter() {
      public void focusLost(FocusEvent e) {
        ageField.selectAll();
        age = ageField.getText();
        int iage = (int) Integer.parseInt(age);
      }
    });

    dataPane.add(new JLabel("Summary Report :"));
    dataPane.add(new JTextArea(10, 30));

    // Show ??????

  }

  public static void main(String[] args) {

    javax.swing.JFrame frame = new JFrame();
    frame.setTitle("Conversion To Frame");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(200, 200, 350, 400);
    frame.getContentPane().add(new framedTextFieldInOut());
    frame.validate();
    frame.show();
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    new framedTextFieldInOut();
  }

}

class BarPanel
    extends JPanel {

}
0
Comment
Question by:delphi3
  • 3
  • 2
5 Comments
 
LVL 15

Expert Comment

by:Javatm
ID: 9769314
Hello friend;

You just need to create a JButton and set the action listener like :

JButton b1 = new JButton("Display");

 b1.addActionListener(new ActionListener()  {
 
  public void actionPerformed(ActionEvent e)
  {
     String s1 = nameField.getText();
     String s2 = ageField.getText();

     ShowInArea.setText(""+s1+"\n\n"+s2);
   
     // If you want to append you can use this :
    // ShowInArea.append(""+s1+"\n\n"+s2);
  }
});

Hope it helps . . .
JAVATM
0
 
LVL 4

Author Comment

by:delphi3
ID: 9769429
JAVATM,
I tried to place it in several locations within the program.
And it had no benefit.

Can we assume there is already a button ("Process") that is on the above form and it sits there with no action. Can you please include it into your gift and then locate it within the program or is that not possible?

Delphi3


0
 
LVL 15

Accepted Solution

by:
Javatm earned 50 total points
ID: 9770735
>> Can you please include it into your gift

     I'm not sure about your problem but here is a way on how you can add an image or
     .gif picture on a button.

    ImageIcon c1 = new ImageIcon("c:\yourfolder\your.gif");
    JButton b1 = new JButton();
    b1.setIcon(c1);

// This codes will do the trick for your program :

/**
 * Created by :
 * JAVATM
 * Copyright : 2003
 */

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Message extends JFrame {

    private JTextField f1, f2;
    private JTextArea t1;
    private JButton b1;

    public Message() {

        super("Text Sample . . .");

        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();

        JLabel l1 = new JLabel("Name : ");
        JLabel l2 = new JLabel("Age : ");

        f1 = new JTextField();
        f2 = new JTextField();

        t1 = new JTextArea();
        t1.setSize(300,300);
        b1 = new JButton("Click to Test");

        p1.setLayout(new GridLayout(2,2));
        p1.add(l1); p1.add(f1);
        p1.add(l2); p1.add(f2);

        p2.add(b1);

        JScrollPane p3 = new JScrollPane(t1);

        this.getContentPane().add(p1, BorderLayout.NORTH);
        this.getContentPane().add(p3, BorderLayout.CENTER);
        this.getContentPane().add(p2, BorderLayout.SOUTH);
        setSize(250,250);
        show();

        b1.addActionListener(new ActionListener()  {

        public void actionPerformed(ActionEvent h)
         {
         String x = f1.getText();
         String y = f2.getText();

         t1.setText("Name : "+x+"\nAge : "+y);
         JOptionPane.showMessageDialog(null,"Test Finished !",
         "Message . . .",JOptionPane.WARNING_MESSAGE);
         }
       });
    }

    public static void main(String args[]) {

        Message app = new Message();

        app.addWindowListener(
         new WindowAdapter() {
            public void windowClosing( WindowEvent e )
            {
               System.exit(0);
            }
         }
      );
   }
}
0
 
LVL 4

Author Comment

by:delphi3
ID: 9771801
Javatm,
Thanks so much. Great job!!!

Your gift solves my problem for this part of sloshing through the details. These details are trying to convert a program that makes a tcp  server and a tcp client that is constructed in a format using String name = input.readLine();and System.out.println(name); to a new framed version.

Thanks again.

Delphi3
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9774037
:) Glad to help . . .
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
eclipse apache tomcat admin console 52 147
login form jsp example 2 49
Delphi Firemonkey: if the Sms contain special characters it won't send it 3 62
Java array 10 55
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

740 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