I did something similar a few months ago. For me it worked, but I have to say that I did not use a listbox. I could show you what I did, but I can’t promise anything. Of course in the following there might be classes which are created by me (ignore that, please).
The following is the ServerMain:
package server;
import com.jme3.app.SimpleApplication;
import com.jme3.math.Vector3f;
import com.jme3.network.ConnectionListener;
import com.jme3.network.HostedConnection;
import com.jme3.network.Message;
import com.jme3.network.Network;
import com.jme3.network.Server;
import com.jme3.renderer.RenderManager;
import com.jme3.system.JmeContext;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import mygame.messages.PlayerListMessage;
import server.messages.CloseConnectionMessage;
import server.messages.CommandMessage;
/**
* Main class of the server
*
* @author Domenic
*/
public class ServerMain extends SimpleApplication implements ConnectionListener {
private ServerGui serverGui;
private Server server;
private ServerManager serverManager; // you don't need this
private ServerGameAppState gameAppState; // you don't need this
public static void main(String[] args) {
Utils.initSerializers();
ServerMain app = new ServerMain();
app.start(JmeContext.Type.Headless);
}
@Override
public void simpleInitApp() {
serverGui = new ServerGui(this);
}
@Override
public void simpleUpdate(float tpf) {
}
@Override
public void simpleRender(RenderManager rm) {
}
// called from gui
public void startServer() {
// System.out.println("hier");
// String ip = serverGui.getIpAdress();
int port = serverGui.getPort();
try {
if (port > 0 && ((server == null) || (server != null && !server.isRunning()))) {
gameAppState = new ServerGameAppState();
serverManager = new ServerManager();
server = Network.createServer(port);
server.addMessageListener(new ServerListener(server, serverManager));
server.addConnectionListener(this);
server.start();
serverGui.setStatusIcon(true);
serverGui.makeShowUp("Server started");
initAppStates();
} else if (server != null && server.isRunning()) {
serverGui.makeShowUp("There is already a server running!");
} else {
serverGui.makeShowUp("No Server started");
}
} catch (IOException ex) {
Logger.getLogger(ServerMain.class.getName()).log(Level.SEVERE, null, ex);
serverGui.setStatusIcon(false);
serverGui.makeShowUp("Could not start server!");
}
}
public Server getServer() {
return server;
}
public void stopServer() {
if (server != null && server.isRunning()) {
server.close();
serverGui.setStatusIcon(false);
serverGui.makeShowUp("Server stopped");
removeAppStates();
} else {
serverGui.makeShowUp("No server available");
}
}
private void initAppStates() {
stateManager.attach(gameAppState);
stateManager.attach(serverManager);
}
private void removeAppStates() {
stateManager.detach(gameAppState);
stateManager.detach(serverManager);
}
public void sendMessage(Message m) {
server.broadcast(m);
}
@Override
public void destroy() {
if (server != null && server.isRunning()) {
server.close();
removeAppStates();
}
super.destroy();
}
@Override
public void connectionAdded(Server server, HostedConnection conn) {
}
@Override
public void connectionRemoved(Server server, HostedConnection conn) {
}
}
The following is the ServerGui:
package server;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
/**
* Builds up the gui
*
* @author Domenic
*/
public class ServerGui extends JFrame {
private JButton startButton;
private JButton stopButton;
private JTextField portField;
private JTextField ipField;
private JLabel ipLabel;
private JLabel portLabel;
private JLabel imageLabel;
private Icon onIcon;
private Icon offIcon;
private String title = "FPS Server";
private int width = 450;
private int height = 200;
private ServerMain serverMain;
public ServerGui(final ServerMain serverMain) {
this.serverMain = serverMain;
// init main frame
setTitle(title);
setSize(width, height);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(null);
setLocationRelativeTo(null);
setResizable(false);
// init buttons, labels and textFields
ipLabel = new JLabel("IP-Adresse");
ipLabel.setBounds(5, 15, 100, 25);
add(ipLabel);
portLabel = new JLabel("Port");
portLabel.setBounds(5, 50, 100, 25);
add(portLabel);
ipField = new JTextField();
ipField.setBounds(110, 15, 200, 25);
ipField.setEnabled(false);
ipField.setText("localhost");
add(ipField);
portField = new JTextField();
portField.setBounds(110, 50, 200, 25);
add(portField);
startButton = new JButton("Start");
startButton.setBounds(15, 100, 195, 40);
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
serverMain.startServer();
}
});
add(startButton);
stopButton = new JButton("Stop");
stopButton.setBounds(240, 100, 195, 40);
stopButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
serverMain.stopServer();
}
});
add(stopButton);
onIcon = new ImageIcon("assets/Interface/on.jpg");
offIcon = new ImageIcon("assets/Interface/off.jpg");
imageLabel = new JLabel();
imageLabel.setIcon(offIcon);
imageLabel.setBounds(350, 10, 80, 80);
add(imageLabel);
// set frame visible
setVisible(true);
}
/**
* Get the ipAdress
* @return ip adress as string
*/
public String getIpAdress() {
return ipField.getText();
}
/**
* Get the port from the port label
* @return an integer
*/
public int getPort() {
int port = -1;
try {
port = Integer.parseInt(portField.getText());
} catch(Exception e) {
}
return port;
}
/**
* Prints a message on the screen
* @param message
*/
public void makeShowUp(String message) {
if (message != null) {
JOptionPane.showMessageDialog(null, message);
}
}
/**
* Changes the status icon in gui
* If true, the check symbol is selected, else the X mark.
* @param status
*/
public void setStatusIcon(boolean status) {
if (status) {
imageLabel.setIcon(onIcon);
portField.setEnabled(false);
} else {
imageLabel.setIcon(offIcon);
portField.setEnabled(true);
}
}
@Override
public void dispose() {
super.dispose();
serverMain.stop();
}
}
By the way, I removed a few lines to make the hole thing look not that overwhelming, so if it doesn’t make any sense somewhere just ignore it
Gui looks like this:
It would be great if you tell me whether it helped you or not, because if not I could just remove this post again.