JComboBox problem

A

Allan Mullan

Hey all :)

I've got a program i'm working on which has a JComboBox and I need to
get the results from it - the code is:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;


public class rDesktopGui extends JFrame {
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 ";
int fullTicked = 0;
String server = null;


// Setup components for 1st row
private JPanel row1 = new JPanel();
private JComboBox serverName = new JComboBox();
private JButton goButton = new JButton("Go");
private JButton exitButton = new JButton("Exit");


// 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() {
System.out.println(externalCommand + ((fullTicked == 0) ? "" : "-f
") + server);
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 goListener = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
runSystemCommand();
}
};


private ActionListener exitListener = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
};

public ItemListener fullScreenListener = new ItemListener() {
public void itemStateChanged(ItemEvent e) {
int status = e.getStateChange();
if(status == e.SELECTED)
fullTicked = 1;
else
fullTicked = 0;
}
};

public ItemListener serverNameListener = new ItemListener() {
public void itemStateChanged(ItemEvent ie) {
Object nameOfServer = ie.getItem();
server = nameOfServer.toString();
System.out.println(server);
}
};

public rDesktopGui() {
super("rDesktopGui Connection GUI");
setLocation(10, 10);
setSize(310,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Setup graphics
Container contentPane = getContentPane();
FlowLayout layout = new FlowLayout();
contentPane.setLayout(layout);

FlowLayout layout1 = new FlowLayout();
for(int i = 0; i < listOfServers.length; i++)
serverName.addItem(listOfServers);
row1.setLayout(layout1);
row1.add(serverName);
row1.add(goButton);
row1.add(exitButton);
contentPane.add(row1);

FlowLayout layout2 = new FlowLayout();
row2.setLayout(layout2);
row2.add(fullScreen);
contentPane.add(row2);

goButton.addActionListener(goListener);
exitButton.addActionListener(exitListener);
fullScreen.addItemListener(fullScreenListener);
serverName.addItemListener(serverNameListener);

setVisible(true);
}

private void runSystemCommand(){
new Thread(systemCommand).start();
}

public static void main(String args[]){
rDesktopGui frame = new rDesktopGui();
}
}


When I run this it displays the current and the next selected, i.e if
FOO is selected and you select OFF it' print both, I only want the
desktination one....

Any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top