javaswing:从jlist中取消选择项

mm5n2pyu  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(263)

我正在制作一个相当简单的swing应用程序,它有经典的“添加或删除文件”面板,我正在尽可能地使其动态化。现在,我已经将remove按钮设置为仅在选中列表项时启用(通过listselectionlistener)。但是,我也希望用户能够通过单击列表项之外的任何位置来取消选择该列表项。我发现的唯一非笨拙的解决方案是使用focuslost操作,但这也会在remove按钮有机会工作之前禁用它。有没有办法:
在列表选项和删除按钮或
focuslost操作是否忽略了用户单击remove按钮?
编辑:下面是要测试的gui代码的简化版本:

public class NewJFrame extends javax.swing.JFrame {

public NewJFrame() {
    initComponents();
}

private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    jList1 = new javax.swing.JList<>();
    addButton = new javax.swing.JButton();
    removeButton = new javax.swing.JButton();
    jTextField1 = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jList1.setModel(new javax.swing.AbstractListModel<String>() {
        String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
        public int getSize() { return strings.length; }
        public String getElementAt(int i) { return strings[i]; }
    });
    jList1.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusLost(java.awt.event.FocusEvent evt) {
            jList1FocusLost(evt);
        }
    });
    jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
        public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
            jList1ValueChanged(evt);
        }
    });
    jScrollPane1.setViewportView(jList1);

    addButton.setText("Add");

    removeButton.setText("Remove");
    removeButton.setEnabled(false);

    jTextField1.setText("Placeholder text field");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jTextField1)
                .addComponent(jScrollPane1)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 242, Short.MAX_VALUE)
                    .addComponent(removeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(addButton)
                .addComponent(removeButton))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
}

private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {                                    
    removeButton.setEnabled(!jList1.isSelectionEmpty());
}                                   

private void jList1FocusLost(java.awt.event.FocusEvent evt) {                                 
    jList1.clearSelection();
}                                

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
}

private javax.swing.JButton addButton;
private javax.swing.JList<String> jList1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
private javax.swing.JButton removeButton;        
}

我使用的方法有:

private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {                                    
    removeButton.setEnabled(!jList1.isSelectionEmpty());
}                                   

private void jList1FocusLost(java.awt.event.FocusEvent evt) {                                 
    jList1.clearSelection();
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题