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?
|