【问题标题】:How can I change the size of a JPanel to fit a dynamically generated JBList?如何更改 JPanel 的大小以适应动态生成的 JBList?
【发布时间】:2016-01-24 18:18:08
【问题描述】:

我正在尝试为 IntelliJ 创建一个弹出插件。弹出窗口包含一个 JPanel(parentPanel),其中包含一个 JList(我使用 JBList,它只是 Intellij 的一个子类)和 JTextField。列表的内容是动态构建的,取决于用户按下 TAB 时 JTextField 的内容。

这是我目前正在尝试的。结果是使用适当的元素构造了一个新的 JList,但 JPanel 保持完全相同的大小,因此大部分 JList 都被阻塞了。如何展开面板(和整个弹出窗口)以使整个列表可见?

是我在 JPanel 上做错了什么,还是弹出窗口本身的大小受到限制?

parentPanel 从这一行接收 GridLayout

this.setLayout(new GridLayout(2,1));

这是修改 JList 的代码。

DefaultListModel listModel = (DefaultListModel) parentPanel.getFileList().getModel();
listModel.removeAllElements();
VirtualFile[] children = currentPathFile.getChildren();

for(int i = 0; i < children.length; ++i) {
   listModel.addElement(children[i].getPath());
}
parentPanel.remove(1); // The list is the panel's second child
parentPanel.add(new JBList(listModel));
parentPanel.revalidate();
parentPanel.repaint();

即使我给 GridLayout 10 行,JList 也只占一行。

【问题讨论】:

    标签: java swing intellij-idea jpanel intellij-plugin


    【解决方案1】:

    如果parentPanelJPanel,则其默认布局为FlowLayout,它保留初始首选大小,通常由原始内容的首选大小定义。尝试将其更改为GridLayout,这将允许列表随着封闭面板大小的调整而增长和缩小。

    这也不起作用。已添加GridLayout 代码。

    因为JBList扩展了JList,你或许可以使用setVisibleRowCount(),如图here

    【讨论】:

    • 那也行不通。添加了网格布局代码。
    • 我不确定您的方法在哪里失败。可以发minimal reproducible example吗?我引用了上面的一个例子供参考。
    猜你喜欢
    • 2013-12-17
    • 2020-09-04
    • 2013-12-18
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 2021-07-19
    • 2017-04-13
    • 2021-03-29
    相关资源
    最近更新 更多