【问题标题】:Is there a way to create a JList in a modal dialog?有没有办法在模式对话框中创建 JList?
【发布时间】:2019-08-20 15:13:13
【问题描述】:

我有一个 JFrame 已经可见。用户可以加载保存的会话。

这个想法是创建一个JList,这样用户就可以加载选择的会话并且可以更新框架。

下面的代码获取一个字符串列表并将它们添加到列表中。

DefaultListModel model = new DefaultListModel();
JList list=new JList(model);
JScrollPane pane = new JScrollPane(list);
try {
    for (String  part : Utils.getSessions()) {
        model.addElement(part);
    }
} catch (IOException e1) {
    e1.printStackTrace();
}

下一步:显示步骤。

我发现了什么:将窗格添加到当前框架

我的希望:在模式对话框中显示列表

有没有办法在模态对话框中创建JList

【问题讨论】:

  • here。在此示例中,JLabel 被添加到弹出窗口中,但您可以添加 JList 的滚动条。
  • 您是在寻找一个弹出窗口(如弹出菜单)还是在寻找一个小的模态对话框,例如JOptionPane
  • 我正在寻找模态对话框,所以当 JList 可见时我可以看到 JFrame 的内容。使用 JOptionPane,我只能得到一个字符串。
  • @SergiyMedvynskyy Huh .. 我想知道(来源)是否会以某种方式成为弹出窗口中列表/滚动窗格中鼠标单击的问题。没有。与列表一起工作就好了。
  • “我正在寻找模态对话框” 请注意,节点对话框不仅很有意义,而且与中建议的“弹出窗口”完全不同这个问题。请edit 更改该信息的问题。还有一个提示:添加@TadHarrison(或重要的@)以通知此人有新评论。

标签: java swing jlist jdialog


【解决方案1】:

事实证明JOptionPane 已经建立了列表选择,无需使用您自己的JList

这里是使用的调用:JOptionPane.showInputDialog

这是一个非常简单的例子:Displaying a dialog with a list of choices

这是一个完整的示例,可让您选择字体名称(使用 Andrew 在 cmets 中提供的方便的字体列表 sn-p)。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class ListChooserDemo extends JFrame {
  JTextPane textPane = new JTextPane();
  String lastChoice = null;

  public ListChooserDemo() {
    setTitle("List Chooser Demo");
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setSize(new Dimension(500, 500));
    add(new JScrollPane(textPane), BorderLayout.CENTER);
    JPanel buttonPanel = new JPanel(new FlowLayout());
    add(buttonPanel, BorderLayout.SOUTH);
    JButton b = new JButton("Choose it!");
    textPane.setText("Click the button...");
    b.addActionListener(this::doChooseFont);
    buttonPanel.add(b);
  }

  public void doChooseFont(ActionEvent e) {
    // a handy way to get a nontrivial list of choices for a demo
    String[] choices = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

    // Show a list of options with no effort on our part.
    String input = (String) JOptionPane.showInputDialog(
            this,                         // optional reference to frame/window or null
            "Choose a font...",           // prompt displayed over list
            "Font Chooser",               // title
            JOptionPane.QUESTION_MESSAGE, // message style
            null,                         // Use default icon for message style
            choices,                      // array of choices
            lastChoice);                  // initial choice or null
    if (input == null) {
      // Handle case when user canceled, didn't select anything, or hit escape
      textPane.setText(textPane.getText() + "\r\nCanceled!");
    } else {
      // Do stuff that happens when a selection was made
      textPane.setText(textPane.getText() + "\r\nSelected " + input);
      lastChoice = input;
    }
  }

  public static final void main(String[] args) {
    // Run in GUI thread
    SwingUtilities.invokeLater(() -> {
      ListChooserDemo frame = new ListChooserDemo();
      // Center in screen and show
      Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
      frame.setLocation(dim.width / 2 - frame.getSize().width / 2, dim.height / 2 - frame.getSize().height / 2);
      frame.setVisible(true);
    });
  }
}

【讨论】:

    【解决方案2】:

    您可以使用JOptionPane 的能力来显示任何组件。使用

    JOptionPane.showMessageDialog(frame, list);
    

    获得一个显示您的JList 的模式对话框。您可以通过添加更多参数来进一步自定义此对话框,请参阅here 了解更多信息。

    一个完整的例子:

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    
    String[] options = new String[] {"a", "b", "c"};
    JList<String> list = new JList<>(options);
    
    // Shows the dialog
    JOptionPane.showMessageDialog(frame, list);
    
    // Do whatever you want with the selection, for example
    frame.add(new JLabel(list.getSelectedValue()));
    frame.pack();
    

    【讨论】:

    • String[] options = new String[] {"a", "b", "c"}; DefaultListModel&lt;String&gt; model = new DefaultListModel&lt;&gt;(); for (String opt : options) model.addElement(opt); JList&lt;String&gt; list = new JList&lt;&gt;(model); 可以缩短为String[] options = new String[] {"a", "b", "c"}; JList&lt;String&gt; list = new JList&lt;&gt;(options);
    猜你喜欢
    • 2020-05-11
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2012-01-16
    • 2018-03-19
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多