LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-19-2008, 06:46 PM   #1
Infasoft
Member
 
Registered: Aug 2008
Location: Salem, Oregon
Distribution: Windows 7 home premium X86_64 | Linux
Posts: 63

Rep: Reputation: 15
java swing receiving input (strings) from a jtextfield


hello!
I am trying to make a Java swing application that executes commands. My question is that how do you receive a string of characters (a-z, or 1-9) when the user puts the command on the jtextfield?. like if the user types in the jtextfield:
kdesudo adept
how would i get those characters to execute them? Thank you so much!
 
Old 10-19-2008, 07:18 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

1. Basically, you just:
a) Create an "ActionListener" (event handler)
b) Create your JTextField object
c) Assign the action listener to the object

2. Here are a couple of good examples:
http://www.javabeginner.com/jtextfield.htm

http://java.sun.com/docs/books/tutor...textfield.html

3. If you're using an IDE (Netbeans is an excellent choice; Eclipse and JBuilder are two other options), then you would typically:

a) Create a form
b) Drag and drop your JTextField object onto the form
c) Double-click on the JTextField object to define your ActionListener

4. Finally, you can call "myJTextField.getText ()" any time to read the current contents of the text field.

'Hope that helps .. PSM
 
Old 10-20-2008, 06:54 AM   #3
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
And to execute operating system processes, you would use a ProcessBuilder.

I suggest that you visit Sun and download their java tutorial, it has all the information that you need to get started.
 
Old 10-20-2008, 08:29 AM   #4
Infasoft
Member
 
Registered: Aug 2008
Location: Salem, Oregon
Distribution: Windows 7 home premium X86_64 | Linux
Posts: 63

Original Poster
Rep: Reputation: 15
Ok thanks guys! But im confused. I Use netbeans and i drag the jbutton onto the GUI. then i side click the button and do the ActionPerformed event. But when ever i try doing
Runtime.getRuntime().exec(cmd)
The IDE tells me:
"catch" without "finnaly"
i know that i must import java.io.IOException and for the top of the source code do:
public static void main(String[] Args)Extends IOException
But i wanted the button to execute the command. i would have to execute the command in the same bracket as where i Extend IOException. How do i fix this? thank you!
 
Old 10-20-2008, 10:25 AM   #5
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
throws IOException

If you need to separate the process from the event handler, you can create a separate method in your class that also declares that it throws IOException. You can then send the input from the event handler to that method to have it processed.

Last edited by jay73; 10-20-2008 at 10:32 AM.
 
Old 10-20-2008, 11:46 AM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Uh, no Infasoft -

Getting a string from JTextEdit is one thing: it has nothing to do with IOException.

Executing a shell command is another, different thing. If you use "Runtime.getRuntime().exec(cmd)", then you'll probably want to catch IOException:

http://en.wikibooks.org/wiki/Java_Pr...ing_Exceptions

JTextEdit.getString() tells you *what* command to run. You've probably also got a JButton somewhere to trigger *when* you run the command. The JButton, of course, is yet a third, completely different thing. With its own, separate, action listener.

STRONG SUGGESTION:
1. Create a new project in your IDE

2. Cut and paste (or type in) one of the tutorial examples jay73 and I cited above.
Do *not* use the IDE to create any GUI elements for you - do everything with code.

3. See how it works, how it all fits together.

Sound like a plan?
 
Old 10-20-2008, 03:59 PM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Here's a crude, simple example that might help:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

/**
 * Minimal JTextField example
 */
public class HelloJTextField extends javax.swing.JFrame implements ActionListener
{

  public static void main(String[] args)
  {
    System.out.println ("Starting GUI...");
    HelloJTextField frame = new HelloJTextField ();
  }
	
  /*
   * We'll use the constructor to initialize our GUI
   */
  public HelloJTextField ()
  {
    // Create form; use null layout and fixed size
    Container container = getContentPane();
    container.setLayout(null);
    setSize(140, 120);
    setLocation (100, 100);
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    // Add text field: don't bother with a special listener
    jtextfield1 = new JTextField ();
    jtextfield1.setSize(100, 25);
    jtextfield1.setLocation(20, 10);
    add (jtextfield1);
    
    // Add a pushbutton.  Add a custom event handler (action listener).
    jbutton1 = new JButton ("run command");
    jbutton1.setSize(100, 30);
    jbutton1.setLocation(20, 50);
    jbutton1.addActionListener(this);
    add (jbutton1);
    
    // Display and run the GUI
    setVisible(true);
  }
	
  /*
   * This will be the event handler for our pushbutton
   */
  public void actionPerformed(ActionEvent e) 
  {
    String cmd = jtextfield1.getText ();
    System.out.println ("button pushed, cmd= " + cmd + "...");
    Runtime rt = null;
    Process proc = null;
    try
    {
      rt = Runtime.getRuntime ();
      proc = rt.exec(cmd);
      System.out.println ("If we got here, we executed (" + cmd + ")...");      
    }
    catch (Exception ex)
    {
      System.out.println ("WARNING: " + ex.getMessage () + "...");
    }
  }	

  /*
   *  Member data
   */
  private javax.swing.JTextField jtextfield1;
  private javax.swing.JButton jbutton1;
  private static final long serialVersionUID = 1L;
}
'Hope that helps .. PSM

Last edited by paulsm4; 10-20-2008 at 04:05 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java 1.6.0u6 JTextField: Cannot enter text make Linux - Software 2 05-14-2013 03:19 AM
a java-swing problem... shosh Programming 2 06-26-2009 05:38 AM
Need Java library to create Swing input components from XML cygnus-x1 Programming 3 09-09-2008 07:14 AM
Making input - masked JTextField MRMadhav Programming 2 06-02-2006 07:49 AM
Strings and input marales314 Programming 6 01-24-2005 12:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration