且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在GUI中向comboBox添加数据

更新时间:2023-12-01 09:17:52

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.