【问题标题】:Java Dialog with jList and option buttons带有 jList 和选项按钮的 Java 对话框
【发布时间】:2016-06-04 10:18:05
【问题描述】:

我想要一个包含供用户选择的 JList 的对话框。虽然以下将执行此操作,但我还想要一条消息和一个“取消”按钮。

list = new JList(LstArray1.toArray());
JOptionPane.showMessageDialog(
 null, list, "Title", JOptionPane.INFORMATION_MESSAGE);

更像这样,但将组合框更改为 JList。

String input = (String) JOptionPane.showInputDialog (null, "Choose from list", "title", JOptionPane.INFORMATION_MESSAGE, null, LstArray2.toArray(), LstArray2.get(0));

我查看了以下内容,但似乎找不到我需要的内容。 http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

【问题讨论】:

    标签: java dialog jlist netbeans-8


    【解决方案1】:

    这可能不是您正在寻找的,但希望它能为您提供所需的基础或激发您自己的替代方法:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class ListDialog {
        private JList list;
        private JLabel label;
        private JOptionPane optionPane;
        private JButton okButton, cancelButton;
        private ActionListener okEvent, cancelEvent;
        private JDialog dialog;
    
        public ListDialog(String message, JList listToDisplay){
            list = listToDisplay;
            label = new JLabel(message);
            createAndDisplayOptionPane();
        }
    
        public ListDialog(String title, String message, JList listToDisplay){
            this(message, listToDisplay);
            dialog.setTitle(title);
        }
    
        private void createAndDisplayOptionPane(){
            setupButtons();
            JPanel pane = layoutComponents();
            optionPane = new JOptionPane(pane);
            optionPane.setOptions(new Object[]{okButton, cancelButton});
            dialog = optionPane.createDialog("Select option");
        }
    
        private void setupButtons(){
            okButton = new JButton("Ok");
            okButton.addActionListener(e -> handleOkButtonClick(e));
    
            cancelButton = new JButton("Cancel");
            cancelButton.addActionListener(e -> handleCancelButtonClick(e));
        }
    
        private JPanel layoutComponents(){
            centerListElements();
            JPanel panel = new JPanel(new BorderLayout(5,5));
            panel.add(label, BorderLayout.NORTH);
            panel.add(list, BorderLayout.CENTER);
            return panel;
        }
    
        private void centerListElements(){
            DefaultListCellRenderer renderer = (DefaultListCellRenderer) list.getCellRenderer();
            renderer.setHorizontalAlignment(SwingConstants.CENTER);
        }
    
        public void setOnOk(ActionListener event){ okEvent = event; }
    
        public void setOnClose(ActionListener event){
            cancelEvent  = event;
        }
    
        private void handleOkButtonClick(ActionEvent e){
            if(okEvent != null){ okEvent.actionPerformed(e); }
            hide();
        }
    
        private void handleCancelButtonClick(ActionEvent e){
            if(cancelEvent != null){ cancelEvent.actionPerformed(e);}
            hide();
        }
    
        public void show(){ dialog.setVisible(true); }
    
        private void hide(){ dialog.setVisible(false); }
    
        public Object getSelectedItem(){ return list.getSelectedValue(); }
    }
    

    示例用法:

    JList list = new JList(new String[] {"foo", "bar", "foobar"});
            ListDialog dialog = new ListDialog("Please select an item in the list: ", list);
            dialog.setOnOk(e -> System.out.println("Chosen item: " + dialog.getSelectedItem()));
            dialog.show();
    

    随意使用/修改上述内容,如果您有任何问题,请在下方提问

    【讨论】:

    • 几乎是我所需要的,并且通过一些调整(如文本大小)就可以完成这项工作。谢谢。
    • 刚刚发现错误;我如何选择取消选项。类似于 dialog.setOnClose(e -> goto end) 在 dialog.setOnOk 和 dialog.show 之间移动。一旦用户从列表中选择了一个项目,在一个地方就会有进一步的代码。还是我必须将值放入字符串并检查是否为空。
    • setOnOk/setOnClose 调用决定了当这些按钮被点击时应该发生什么。他们不会立即执行。只要在显示对话框之前设置它们就可以了,因为在按钮具有任何功能之前不会有机会单击按钮
    • ta,可以从那里排序。
    猜你喜欢
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 2015-02-22
    • 2015-09-10
    相关资源
    最近更新 更多