简体   繁体   English

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

[英]display JList from a queue and refreshes after button click

i have a GUI that accepts an entry and then processes the data, but i want to be able to display the entries in a queue and then display them in a JList. 我有一个接受条目然后处理数据的GUI,但是我希望能够在队列中显示条目,然后在JList中显示它们。

i have the GUI code with the JList and the queue but i dont know how to link them and make the JList a representation of the queue. 我有JList和队列的GUI代码,但我不知道如何链接它们并使JList表示队列。

GUI Class: 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;
}

queue class: 队列类:

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++;
        }
    }
}

could you please help me make the JList get the data from the queue and then display it? 您能帮我让JList从队列中获取数据然后显示它吗?

Thanks for your time 谢谢你的时间

You have several choices, but which one you choose will depend on other factors... 您有多种选择,但是选择哪种取决于其他因素...

You Could... 你可以...

Simply add or remove an element to the underlying ListModel when you update the queue. 在更新队列时,只需向基础ListModel添加或删除元素。 This would require you to provide a ListModel which was capable of been mutated, such as a DefaultListModel . 这将要求您提供一个能够被突变的ListModel ,例如DefaultListModel

This assumes that you are modifying the queue within the context of the Event Dispatching Thread 这假定您正在事件调度线程的上下文中修改队列。

You Could... 你可以...

Devise a wrapper/proxy class that was backed by the queue and provided the basic functionality you need (add/remove) but also provide listener support. 设计一个由队列支持的包装器/代理类,该类提供了您需要(添加/删除)的基本功能,还提供了侦听器支持。

In this way, you could notifications when elements were added or removed from the queue to those parties that are interested. 这样,您可以向队列中感兴趣的各方通知何时将元素添加到队列中或从队列中删除。

This allows the queue to be managed in a separate thread and places the responsibility of ensure that updates to the UI occur within the listener itself 这允许在单独的线程中管理队列,并负责确保UI的更新发生在侦听器本身内

This is basically a implementation of a Observer Pattern 这基本上是观察者模式的实现

Either way, take a look at How to Use Lists for more details 无论哪种方式,请查看如何使用列表以了解更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM