LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 04-18-2004, 04:47 AM   #1
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 437

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:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
                                                                                                                                
public class rDesktopGui extends JFrame  implements ItemListener {
  ServerConfig servers = new ServerConfig("/etc/rDesktopGui.conf"); // Starts working the ServerConfig class
  String[] listOfServers = servers.read(); // 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
          System.out.println(listOfServers);*/
                                                                                                                                
    System.out.println(externalCommand);
    try{
      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)
        System.out.println(line);
      } catch(IOException ex) {
        System.out.println("Error running command - Invalid command!");
      }
    }
  };
                                                                                                                                
  private ActionListener buttListener = new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
      runSystemCommand();
    }
  };
                                                                                                                                
  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);
    setSize(300,100);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
    // Setup graphics
    FlowLayout layout = new FlowLayout();
    Container contentPane = getContentPane();
    contentPane.setLayout(layout);
 
    FlowLayout layout1 = new FlowLayout();
    for(int i = 0; i < listOfServers.length; i++)
      serverName.addItem(listOfServers[i]);
    row1.add(serverName);
    row1.add(goButton);
    contentPane.add(row1);
 
    FlowLayout layout2 = new FlowLayout();
    row2.add(fullScreen);
    contentPane.add(row2);
 
    goButton.addActionListener(buttListener);
    fullScreen.addItemListener(this);
 
    setVisible(true);
  }
 
  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, 01:25 PM   #2
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 53
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:
Code:
externalCommand = (String)serverName.getSelectedItem();
Hope this helps.

p.s. Use the API reference at http://java.sun.com/j2se/1.5.0/docs/api/ , you'll need to ask fewer questions like this!
 
Old 04-18-2004, 03:33 PM   #3
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 437

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


Reply


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


All times are GMT -5. The time now is 11:55 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration