LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   getText() problem in Java (https://www.linuxquestions.org/questions/programming-9/gettext-problem-in-java-171568/)

AMMullan 04-18-2004 04:47 AM

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?

Komakino 04-18-2004 01:25 PM

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!

AMMullan 04-18-2004 03:33 PM

Thanks again Komakino - didn't think to cast it :D


All times are GMT -5. The time now is 04:29 PM.