简体   繁体   English

如何更改JPanel的大小以适合动态生成的JBList?

[英]How can I change the size of a JPanel to fit a dynamically generated JBList?

I'm trying to create a popup plugin for IntelliJ. 我正在尝试为IntelliJ创建一个弹出插件。 The popup contains a JPanel (parentPanel), which contains a JList (I use JBList which is just a sublcass for Intellij) and JTextField. 弹出窗口包含一个JPanel(parentPanel),其中包含一个JList(我使用JBList,它只是Intellij的子类)和JTextField。 The contents of the list are built dynamically, depending on the contents of the JTextField when the user presses TAB. 列表的内容是动态构建的,具体取决于用户按下TAB时JTextField的内容。

This is what I am currently trying. 这就是我目前正在尝试的。 The result is that a new JList is constructed with the proper elements, but the JPanel stays the exact same size, so most of the JList is obstructed. 结果是使用适当的元素构造了一个新的JList,但是JPanel保持完全相同的大小,因此大部分JList被阻塞了。 How can I expand the panel (and the whole popup) so that the entire list is visible? 如何展开面板(和整个弹出窗口),以便可以看到整个列表?

Am I doing something wrong with JPanel, or is the size constrained by the popup itself? 我是否对JPanel做错了,还是大小受弹出窗口本身的限制?

parentPanel receives GridLayout from this line parentPanel从此行接收GridLayout

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

Here is the code which modifies the JList. 这是修改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();

Even if I give the GridLayout 10 rows, the JList only occupies one row. 即使给GridLayout 10行,JList也只占据一行。 在此处输入图片说明

If parentPanel is a JPanel , its default layout is FlowLayout , which retains the initial preferred size, typically defined by the preferred size of the original content. 如果parentPanelJPanel ,则其默认布局为FlowLayout ,它保留初始的首选大小,通常由原始内容的首选大小定义。 Try changing it to GridLayout , which will allow the list to grow and shrink as the enclosing panel is resized. 尝试将其更改为GridLayout ,这将使列表随着封闭面板的调整大小而增加和缩小。

That doesn't work either. 那也不行。 GridLayout code has been added. GridLayout代码已添加。

Because JBList extends JList , you may be able to use setVisibleRowCount() , as shown here . 由于JBList扩展JList ,你可以使用setVisibleRowCount()如图所示这里

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

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