Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 04-18-2004, 05:47 AM   #1
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Rep: Reputation: 30
getText() problem in Java

Hey all

I've got possibly a simple problem, I'm trying to get text from a selected item in a JComboBox. With a JTextField you would use getText() to get the text but i'm not sure with a JComboBox...

Here's my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class rDesktopGui extends JFrame  implements ItemListener {
  ServerConfig servers = new ServerConfig("/etc/rDesktopGui.conf"); // Starts working the ServerConfig class
  String[] listOfServers =; // Creates a local copy of the read() method from the ServerConfig class
  String externalCommand = "rdesktop ";
  // Setup components for 1st row
  private JPanel row1 = new JPanel();
  private JComboBox serverName = new JComboBox();
  private JButton goButton = new JButton("Go");
  // Setup components for 2nd row
  private JPanel row2 = new JPanel();
  private JCheckBox fullScreen = new JCheckBox("Fullscrreen", false);
  private Runnable systemCommand = new Runnable() {
    public void run() {
    /* for(int i = 0; i < listOfServers.length; i++) // This was a test to check that the conf read right
      Runtime runtime = Runtime.getRuntime(); // Creates a runtime environment for the external process
      // Spawn a shell sub-process
      Process process = runtime.exec(externalCommand); // serverName.getText() to get string from JTextBox
      BufferedReader buffer = new BufferedReader(new InputStreamReader(process.getInputStream()));
      String line;
      while((line = buffer.readLine()) != null)
      } catch(IOException ex) {
        System.out.println("Error running command - Invalid command!");
  private ActionListener buttListener = new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
  private ActionListener fullScreenListener = new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
  public void itemStateChanged(ItemEvent e) {
    int status = e.getStateChange();
    if(status == e.SELECTED)
      externalCommand = externalCommand + "-f ";
  public rDesktopGui() {
    super("rDesktopGui Connection GUI");
    setLocation(10, 10);
    // Setup graphics
    FlowLayout layout = new FlowLayout();
    Container contentPane = getContentPane();
    FlowLayout layout1 = new FlowLayout();
    for(int i = 0; i < listOfServers.length; i++)
    FlowLayout layout2 = new FlowLayout();
  private void runSystemCommand(){
    new Thread(systemCommand).start();
  public static void main(String args[]){
    rDesktopGui frame = new rDesktopGui();
Now I need to add the chosen field to the externalCommand variable... Anyone got an idea?
Old 04-18-2004, 02:25 PM   #2
Senior Member
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
A JComboBox can hold any object, not just strings (thought the object still has a string representation). You use the getSelectedItem() method to return the object and then cast that into a string to be able to use it. Hence if you wanted to set the externalCommand variable to the name of the currently selected server from the serverName JComboBox you would do:
externalCommand = (String)serverName.getSelectedItem();
Hope this helps.

p.s. Use the API reference at , you'll need to ask fewer questions like this!
Old 04-18-2004, 04:33 PM   #3
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
Thanks again Komakino - didn't think to cast it


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
php 5.0.5 and gettext ziggie216 Linux - Software 0 11-10-2005 03:26 PM
php + gettext Ateo Linux - General 0 11-09-2005 12:40 PM
glibc/gettext/multibyte-sequence problem, resolved... sundialsvcs Linux - Software 0 09-17-2005 02:02 PM
Solved Problem Second GCC-Pass(or gettext sanity check problem) hoes Linux From Scratch 0 09-04-2005 11:20 AM
no gettext support in C library nef Linux From Scratch 3 04-08-2004 07:09 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:20 AM.

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