monkey doodle
monkey doodle

Reputation: 700

java gui swing manipulating input by user

I'm new to GUI. I'm trying to create a gui for an already made java program I have. I want the user to enter the port number and location of file, and from there I want to use my already made program to do the rest. I'm confused about how I will get the values from the user input and implement to the program.

This is the framework of my program

public class TcpServerCompareCSV extends Frame implements ActionListener , WindowListener  {


   private Label lblPort;    // declare component Label
   private TextField tfPort; // declare component TextField    
   private int port;     // port number



   /** WindowEvent handlers */
   // Called back upon clicking close-window button
   @Override
   public void windowClosing(WindowEvent e) {
      System.exit(0);  // terminate the program
   }




   //constructor for frame
   public TcpServerCompareCSV () {
      setLayout(new FlowLayout());
         // "this" Frame sets its layout to FlowLayout, which arranges the components
         //  from left-to-right, and flow to next row from top-to-bottom.

      lblPort = new Label("Port"); // construct Label
      add(lblPort);                   // "this" Frame adds Label

      tfPort = new TextField("0", 10); // construct TextField
      tfPort.setEditable(true);       //edit text
      add(tfPort);                     // "this" Frame adds tfCount



      tfPort.addActionListener(this); // for event-handling

      setTitle("compare");  // "this" Frame sets title
      setSize(250, 100);        // "this" Frame sets initial window size
      setVisible(true);         // "this" Frame shows


      addWindowListener(this);
        // "this" Frame fires WindowEvent its registered WindowEvent listener
        // "this" Frame adds "this" object as a WindowEvent listener

   }




   /** ActionEvent handler - Called back when user clicks the button. */
   @Override
   public void actionPerformed(ActionEvent evt) {
    // Get the String entered into the TextField tfPort, convert to int
      port = Integer.parseInt(tfPort.getText());

   }





   /** The entry main() method */

public static void main(String[] args) throws IOException{

      // Invoke the constructor to setup the GUI, by allocating an instance
    TcpServerCompareCSV app = new TcpServerCompareCSV();

Upvotes: 0

Views: 176

Answers (1)

arcy
arcy

Reputation: 13123

Your code is incomplete, so I'm having to guess about a few things, esp. about what your "already-made" program looks like.

Let's say you have a command-line program in a class that starts out something like:

public class AlreadyMade
{
  public static void main(String[] arguments)
  {
    AlreadyMade am = new AlreadyMade();
    am.goToIt(arguments[0], arguments[1]);
  }

  public void goToIt(String s1, String s2)
  {
    // insert logic here for what to do with your port number and location
  }
}

In that case, you can invoke the goToIt() method from your GUI, possibly from the actionPerformed method, the same way you invoke it from main in your AlreadyMade class:

...
AlreadyMade am = new AlreadyMade();
am.goToIt(tfPort.getText(), tfFileLocation.getText());  // assume tfFileLocation, etc.
...

I'm also having to guess that this is what you wanted to know; if not, at least you know what it LOOKS like you wanted to know and can refine the question.

Upvotes: 1

Related Questions