![](/img/trans.png)
[英]JscrollPane doesn't update when changing JList model (Java swing)
[英]JList of TextFields and JScrollPane doesn't show / Java Swing
我正在尝试创建一个显示textFields动态列表的窗口,如果textfields的数量很大,那么我想添加一个滚动条。 我正在使用GridLayout。 问题是我添加了Jlist和滚动条的面板没有显示任何内容,列表和滚动条也没有。 在下面,您将找到我的代码的一部分。
//Label
JLabel numberOfTxt = new JLabel("Please enter the number in every TextField");
int n = 11; //A random number of TextFields
firstPanel.add(numberOfTxt, BorderLayout.NORTH); //Add label to panel
JList textFieldList = new JList(); //Create a list of TextFields
for (int i = 0; i < n; i++) {
//Add TextFields to list
JTextField textField = new JTextField();
textField.setBounds(0, 0, 6, 0);
textFieldList.add(textField);
System.out.println("textFieldList" + textFieldList);
}
textFieldList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
textFieldList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
textFieldList.setVisibleRowCount(8);
//Create scroller
JScrollPane listScroller = new JScrollPane(textFieldList);
listScroller.setBounds(0, 20, 600, 600);
//Create layout for panel where the textfields will be added
if (n % 2 != 0) {
n = n + 1;
}
thirdPanel.setLayout(new GridLayout(n / 2, 2, 10, 6));
thirdPanel.add(textFieldList);
thirdPanel.setVisible(true);
//ContentPane has BoxLayout
contentPane.add(firstPanel);
contentPane.add(thirdPanel);
contentPane.repaint();
window.pack();
}
window.revalidate();
}
});
JList
不能以这种方式工作。 如果您确实需要TextFields
的JList
,则应该使用ListCellRenderer
(可能不需要,请参阅第3页)。
您将textFieldList
都添加到listScroller
和thirdPanel
。 可能您应该替换thirdPanel.add(textFieldList);
通过thirdPanel.add(listScroller);
。
thirdPanel
使用GridLayout
,但仅向其中添加了一个控件。 您应该将TextField
直接添加到thirdPanel
(更简便的方法),或者让JList
管理它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.