显示队列中的JList,并在单击按钮后刷新

时间:2014-03-28 23:51:06

标签: java swing jlist

我有一个接受条目然后处理数据的GUI,但我希望能够显示队列中的条目,然后将它们显示在JList中。

我有使用JList和队列的GUI代码,但我不知道如何链接它们并使JList成为队列的表示。

GUI类:

public class ServerPlayergameMain extends javax.swing.JFrame implements Runnable {
   public serverPlayerQueue serverQueue = new serverPlayerQueue();

   Thread run;

   public ServerPlayergameMain() {
        initComponents();
    }

    public void run(){
        String commandMessage = command.getText();
         serverQueue.addToQueue(commandMessage);
        command.setText(" ");
        repaint();
        validate();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        command = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        command.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                commandActionPerformed(evt);
            }
        });
        command.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                commandKeyPressed(evt);
            }
        });

        jLabel1.setText("Enter your command:");

        jButton1.setText("Go!");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        jButton2.setText("Save");

        jButton3.setText("Forfeit");

        jLabel2.setText("Command Queue:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton3)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(0, 132, Short.MAX_VALUE))
                            .addComponent(command))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane1)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(38, 38, 38))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(90, 90, 90)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)
                        .addGap(0, 46, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(command, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12)
                        .addComponent(jButton3)))
                .addContainerGap())
        );

        pack();
    }
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     run = new Thread(this);
     run.start();
}                                        
    private javax.swing.JTextField command;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
}

队列类:

public class serverPlayerQueue {
        String [] elementArray = new String [20];
        int frontPointer = 0;
        int rearPointer = -1;
        int arrayInput = 0;
        char choice;

    public void addToQueue (String command) {
        if (arrayInput == 20) {
            System.out.println("Error in queue!");
        }else{
            rearPointer = rearPointer+1;
            rearPointer = rearPointer % 20;
            elementArray[rearPointer] = command;
            arrayInput++;
        }
    }
}

你可以帮我让JList从队列中获取数据,然后显示它吗?

感谢您的时间

1 个答案:

答案 0 :(得分:1)

您有多种选择,但您选择哪一种将取决于其他因素......

你可以......

更新队列时,只需在基础ListModel中添加或删除元素即可。这将要求您提供能够进行变异的ListModel,例如DefaultListModel

这假设您正在事件调度线程的上下文中修改队列

你可以......

设计一个由队列支持的包装器/代理类,并提供所需的基本功能(添加/删除),同时提供监听器支持。

通过这种方式,您可以在将队列中的元素添加或删除给有兴趣的各方时发出通知。

这允许在单独的线程中管理队列,并确保在侦听器本身内发生UI更新的责任

这基本上是Observer Pattern

的实现

无论哪种方式,请查看How to Use Lists了解更多详情