在GUI中向comboBox添加数据 [英] Adding data to comboBox in GUI

查看:168
本文介绍了在GUI中向comboBox添加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的服务器GUI中填充一些数据的组合框。具体来说,我想添加注册到服务器的客户端的ID。
我必须实现它错了,因为在gui的initialize()方法后的更改不适用。



我复制这里涉及的代码: / p>

在服务器中:

 服务器实现ServerInterface {

private static List< ClientInterface>客户;
static ServerGui gui;
public Server()throws RemoteException
{
super();
clients = new ArrayList< ClientInterface>();
gui = new ServerGui();
}
* ....其他方法不涉及.... *
public static void main(String [] args)
{

try {
Server statsServer = new Server();
ServerInterface stub =(ServerInterface)UnicastRemoteObject.exportObject(statsServer,1099);

注册表r = LocateRegistry.createRegistry(1099);
r.rebind(aaa,stub);
System.out.println(Server ready);
//gui.fullCombobox (clients);
gui.main(null);
}
/ *如果任何通信失败... * /
catch(RemoteException e){
System.out.println(Communication error+ e.toString ));
}
}
}

/ strong>

  import java.awt.EventQueue; 


public class ServerGui {

private JFrame frame;
public JComboBox< String>组合框;

/ **
*启动应用程序。
* /
public static void main(String [] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
try {
ServerGui window = new ServerGui();
window.frame.setVisible(true);
} catch(Exception e){
e.printStackTrace();
}
}
});
}

/ **
*创建应用程序。
* /
public ServerGui(){
initialize();
}

/ **
*初始化框架的内容。
* /

private void initialize(){
frame = new JFrame();
frame.setBounds(100,100,725,487);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane()。setLayout(null);

comboBox = new JComboBox();
ComboBox.addItem(ciao);
comboBox.addItem(miao);
comboBox.setBounds(162,113,182,38);
frame.getContentPane()。add(comboBox);

}

public void fullCombobox(List< ClientInterface> lista)throws RemoteException
{
Iterator< ClientInterface> it = lista.iterator();
while(it.hasNext())
{
ClientInterface c =(ClientInterface)it.next();
Integer i = c.getId();
comboBox.addItem(i.toString());
}
}
}

当我调用Server方法通过客户端,我在它的末端调用fullComboBox方法。我想组合框会自动重新加载,但它不会改变。



似乎所有我做后initialize()方法不工作。 / p>

我该如何解决?
我是按照正确的方式做一个服务器的gui吗?我有一些疑问,如果把一个gui对象内部的服务器(如我一样)或把一个服务器内的gui。我真的很困惑。



先感谢所有人。

解决方案

p>从查看你的代码很快,似乎你甚至没有调用方法

  public void fullCombobox(List< ClientInterface> lista)throws RemoteException {} 

您忘记在代码中显示调用或者是错误这使你认为这是一个问题的JComboBox。


I'm trying to populate a comboBox in my server GUI with some data. Specifically, i would like to add the id of the clients registered to the Server. I must have implemented it wrong, because changes after the initialize() method of the gui don't apply.

I copy here the involved code:

In server:

public class Server implements ServerInterface {

    private static List<ClientInterface> clients;
    static ServerGui gui;
public Server() throws RemoteException
    {
        super();
        clients = new ArrayList<ClientInterface>( );
        gui = new ServerGui();
    }
*.... other methods not involved ....*
public static void main(String[] args) 
    {

        try {
            Server statsServer = new Server();
            ServerInterface stub = (ServerInterface) UnicastRemoteObject.exportObject(statsServer, 1099);

            Registry r=LocateRegistry.createRegistry(1099);
            r.rebind("aaa", stub);
            System.out.println("Server ready");
            //gui.fullCombobox(clients);
            gui.main(null);
        }
        /* If any communication failures occur... */
        catch (RemoteException e) {
            System.out.println("Communication error " + e.toString());
        }
    }
}

In GUI

import java.awt.EventQueue;


public class ServerGui {

    private JFrame frame;
    public JComboBox<String> comboBox;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ServerGui window = new ServerGui();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public ServerGui() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 725, 487);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        comboBox = new JComboBox();
        comboBox.addItem("ciao");
        comboBox.addItem("miao");
        comboBox.setBounds(162, 113, 182, 38);
        frame.getContentPane().add(comboBox);

    }

    public void fullCombobox(List <ClientInterface> lista) throws RemoteException
    {
        Iterator<ClientInterface> it= lista.iterator();
        while(it.hasNext())
        {
            ClientInterface c = ( ClientInterface ) it.next();
            Integer i = c.getId();
            comboBox.addItem(i.toString());
        }
    }
}

When i invoke a Server method through Client, i invoke at the end of it the fullComboBox method too. I supposed the combobox would reload automatically, but it doesn't change at all.

It seems that all i do after the initialize() method doesn't work.

How can i resolve that? And am i following the right way to do a gui for a Server? I had some doubts if to put a gui object inside the server (as i did) or to put a server inside the gui. I'm really confused.

Thanks in advance to everyone.

解决方案

From reviewing your code shortly, it seems that you are not even calling the method

 public void fullCombobox(List<ClientInterface> lista) throws RemoteException {}

You either forgot to show the call in your code or it was the mistake that made you think that it is a problem with the JComboBox.

这篇关于在GUI中向comboBox添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆