JList调整大小问题?
我在调整 JList 大小时遇到问题... 下面是我的代码...我的要求是将具有固定大小的 JList 添加到面板中,当我最大化/最小化框架时,JList 应相应增加/减少。请帮助我..如何实现这一点?
以下是设计:
嗨卡尔, 它适用于简单的情况,但不适用于上述情况。 所以我现在解释上面的代码..
我有一个名为“TestListControl”的框架,具有 GB 布局。 添加具有 GB 布局的 selectDevicesPanel。 在 selectDevicesPanel 中: 添加带有 GB 布局的 selectNamesInnerPanel 在 selectNamesInnerPanel 中: 添加GB布局的selectNameAddPanel 添加带有 Flow 布局的 selectTeamPanel 在 selectNameAddPanel 中: 选择设备滚动窗格 单击添加删除面板 选定的名称滚动窗格 在 selectTeamPanel 中: 选择团队标签 选择团队组合框
public class TestListControl extends javax.swing.JFrame {
public TestListControl() {
initComponents();
}
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
selectDevicesPanel = new javax.swing.JPanel();
selectNamesInnerPanel = new javax.swing.JPanel();
selectNameAddPanel = new javax.swing.JPanel();
chooseDeviceScrollPane = new javax.swing.JScrollPane();
chooseNamesList = new javax.swing.JList();
clickToAddDeletePanel = new javax.swing.JPanel();
clickToAddButton = new javax.swing.JButton();
clickToRemoveButton = new javax.swing.JButton();
selectedNameScrollPane = new javax.swing.JScrollPane();
selectedNamesList = new javax.swing.JList();
selectNameLabel = new javax.swing.JLabel();
selectTeamPanel = new javax.swing.JPanel();
selectTeamLabel = new javax.swing.JLabel();
selectTeamComboBox = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
selectDevicesPanel.setBorder(javax.swing.BorderFactory
.createTitledBorder("Member Selection"));
selectDevicesPanel.setLayout(new java.awt.GridBagLayout());
selectNamesInnerPanel.setLayout(new java.awt.GridBagLayout());
selectNameAddPanel.setLayout(new java.awt.GridBagLayout());
chooseNamesList.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "XXXX", "YYYY", "ZZZZ" };
public int getSize() {
return strings.length;
}
public Object getElementAt(int i) {
return strings[i];
}
});
chooseNamesList.setMaximumSize(new java.awt.Dimension(46, 88));
chooseNamesList.setMinimumSize(new java.awt.Dimension(94, 98));
chooseDeviceScrollPane.setViewportView(chooseNamesList);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 12, 7, 0);
selectNameAddPanel.add(chooseDeviceScrollPane, gridBagConstraints);
clickToAddDeletePanel.setLayout(new java.awt.GridBagLayout());
clickToAddButton.setText(">>");
clickToAddDeletePanel.add(clickToAddButton,
new java.awt.GridBagConstraints());
clickToRemoveButton.setText("<<");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
clickToAddDeletePanel.add(clickToRemoveButton, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 2;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
selectNameAddPanel.add(clickToAddDeletePanel, gridBagConstraints);
selectedNamesList.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "XXXX", "YYYY" };
public int getSize() {
return strings.length;
}
public Object getElementAt(int i) {
return strings[i];
}
});
selectedNamesList.setMinimumSize(new java.awt.Dimension(94, 98));
selectedNameScrollPane.setViewportView(selectedNamesList);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
selectNameAddPanel.add(selectedNameScrollPane, gridBagConstraints);
selectNameLabel.setText("Select Name");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 8, 0);
selectNameAddPanel.add(selectNameLabel, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
selectNamesInnerPanel.add(selectNameAddPanel, gridBagConstraints);
selectTeamLabel.setText("Select Team");
selectTeamPanel.add(selectTeamLabel);
selectTeamComboBox.setModel(new javax.swing.DefaultComboBoxModel(
new String[] { "TEAM X", "TEAM Y", "TEAM Z" }));
selectTeamComboBox
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
selectTeamComboBoxActionPerformed(evt);
}
});
selectTeamPanel.add(selectTeamComboBox);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
selectNamesInnerPanel.add(selectTeamPanel, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
selectDevicesPanel.add(selectNamesInnerPanel, gridBagConstraints);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(
org.jdesktop.layout.GroupLayout.LEADING).add(0, 400,
Short.MAX_VALUE).add(
layout.createParallelGroup(
org.jdesktop.layout.GroupLayout.LEADING).add(
layout.createSequentialGroup().add(0, 107,
Short.MAX_VALUE).add(selectDevicesPanel,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
185,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(0, 108, Short.MAX_VALUE))));
layout.setVerticalGroup(layout.createParallelGroup(
org.jdesktop.layout.GroupLayout.LEADING).add(0, 300,
Short.MAX_VALUE).add(
layout.createParallelGroup(
org.jdesktop.layout.GroupLayout.LEADING).add(
layout.createSequentialGroup().add(0, 39,
Short.MAX_VALUE).add(selectDevicesPanel,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
221,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(0, 40, Short.MAX_VALUE))));
pack();
}
private void selectTeamComboBoxActionPerformed(
java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestListControl().setVisible(true);
}
});
}
private javax.swing.JScrollPane chooseDeviceScrollPane;
private javax.swing.JList chooseNamesList;
private javax.swing.JButton clickToAddButton;
private javax.swing.JPanel clickToAddDeletePanel;
private javax.swing.JButton clickToRemoveButton;
private javax.swing.JPanel selectDevicesPanel;
private javax.swing.JPanel selectNameAddPanel;
private javax.swing.JLabel selectNameLabel;
private javax.swing.JPanel selectNamesInnerPanel;
private javax.swing.JComboBox selectTeamComboBox;
private javax.swing.JLabel selectTeamLabel;
private javax.swing.JPanel selectTeamPanel;
private javax.swing.JScrollPane selectedNameScrollPane;
private javax.swing.JList selectedNamesList;
}
i am having resizing problem with JList ...
Below is my code ...my requirement is to add a JList with some fixed size into a Panel and when i maximize/minimize the Frame the JList should increase/decrease accordingly .please help me ..how to achieve this?
Following is the design:
Hi Carl ,
It works in simple cases but not in the above case.
So i am explaning the above code now ..
i have a frame called "TestListControl" with GB layout.
Add selectDevicesPanel with GB layout.
In selectDevicesPanel :
Add selectNamesInnerPanel with GB layout
In selectNamesInnerPanel :
Add selectNameAddPanel with GB layout
Add selectTeamPanel with Flow layout
In selectNameAddPanel:
chooseDeviceScrollPane
clickToAddDeletePanel
selectedNameScrollPane
In selectTeamPanel :
selectTeamLabel
selectTeamComboBox
public class TestListControl extends javax.swing.JFrame {
public TestListControl() {
initComponents();
}
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
selectDevicesPanel = new javax.swing.JPanel();
selectNamesInnerPanel = new javax.swing.JPanel();
selectNameAddPanel = new javax.swing.JPanel();
chooseDeviceScrollPane = new javax.swing.JScrollPane();
chooseNamesList = new javax.swing.JList();
clickToAddDeletePanel = new javax.swing.JPanel();
clickToAddButton = new javax.swing.JButton();
clickToRemoveButton = new javax.swing.JButton();
selectedNameScrollPane = new javax.swing.JScrollPane();
selectedNamesList = new javax.swing.JList();
selectNameLabel = new javax.swing.JLabel();
selectTeamPanel = new javax.swing.JPanel();
selectTeamLabel = new javax.swing.JLabel();
selectTeamComboBox = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
selectDevicesPanel.setBorder(javax.swing.BorderFactory
.createTitledBorder("Member Selection"));
selectDevicesPanel.setLayout(new java.awt.GridBagLayout());
selectNamesInnerPanel.setLayout(new java.awt.GridBagLayout());
selectNameAddPanel.setLayout(new java.awt.GridBagLayout());
chooseNamesList.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "XXXX", "YYYY", "ZZZZ" };
public int getSize() {
return strings.length;
}
public Object getElementAt(int i) {
return strings[i];
}
});
chooseNamesList.setMaximumSize(new java.awt.Dimension(46, 88));
chooseNamesList.setMinimumSize(new java.awt.Dimension(94, 98));
chooseDeviceScrollPane.setViewportView(chooseNamesList);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 12, 7, 0);
selectNameAddPanel.add(chooseDeviceScrollPane, gridBagConstraints);
clickToAddDeletePanel.setLayout(new java.awt.GridBagLayout());
clickToAddButton.setText(">>");
clickToAddDeletePanel.add(clickToAddButton,
new java.awt.GridBagConstraints());
clickToRemoveButton.setText("<<");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
clickToAddDeletePanel.add(clickToRemoveButton, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 2;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
selectNameAddPanel.add(clickToAddDeletePanel, gridBagConstraints);
selectedNamesList.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "XXXX", "YYYY" };
public int getSize() {
return strings.length;
}
public Object getElementAt(int i) {
return strings[i];
}
});
selectedNamesList.setMinimumSize(new java.awt.Dimension(94, 98));
selectedNameScrollPane.setViewportView(selectedNamesList);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
selectNameAddPanel.add(selectedNameScrollPane, gridBagConstraints);
selectNameLabel.setText("Select Name");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 8, 0);
selectNameAddPanel.add(selectNameLabel, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
selectNamesInnerPanel.add(selectNameAddPanel, gridBagConstraints);
selectTeamLabel.setText("Select Team");
selectTeamPanel.add(selectTeamLabel);
selectTeamComboBox.setModel(new javax.swing.DefaultComboBoxModel(
new String[] { "TEAM X", "TEAM Y", "TEAM Z" }));
selectTeamComboBox
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
selectTeamComboBoxActionPerformed(evt);
}
});
selectTeamPanel.add(selectTeamComboBox);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
selectNamesInnerPanel.add(selectTeamPanel, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
selectDevicesPanel.add(selectNamesInnerPanel, gridBagConstraints);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(
org.jdesktop.layout.GroupLayout.LEADING).add(0, 400,
Short.MAX_VALUE).add(
layout.createParallelGroup(
org.jdesktop.layout.GroupLayout.LEADING).add(
layout.createSequentialGroup().add(0, 107,
Short.MAX_VALUE).add(selectDevicesPanel,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
185,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(0, 108, Short.MAX_VALUE))));
layout.setVerticalGroup(layout.createParallelGroup(
org.jdesktop.layout.GroupLayout.LEADING).add(0, 300,
Short.MAX_VALUE).add(
layout.createParallelGroup(
org.jdesktop.layout.GroupLayout.LEADING).add(
layout.createSequentialGroup().add(0, 39,
Short.MAX_VALUE).add(selectDevicesPanel,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
221,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(0, 40, Short.MAX_VALUE))));
pack();
}
private void selectTeamComboBoxActionPerformed(
java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestListControl().setVisible(true);
}
});
}
private javax.swing.JScrollPane chooseDeviceScrollPane;
private javax.swing.JList chooseNamesList;
private javax.swing.JButton clickToAddButton;
private javax.swing.JPanel clickToAddDeletePanel;
private javax.swing.JButton clickToRemoveButton;
private javax.swing.JPanel selectDevicesPanel;
private javax.swing.JPanel selectNameAddPanel;
private javax.swing.JLabel selectNameLabel;
private javax.swing.JPanel selectNamesInnerPanel;
private javax.swing.JComboBox selectTeamComboBox;
private javax.swing.JLabel selectTeamLabel;
private javax.swing.JPanel selectTeamPanel;
private javax.swing.JScrollPane selectedNameScrollPane;
private javax.swing.JList selectedNamesList;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您这样做,您的列表不会变得更大:
chooseNamesList.setMaximumSize(new java.awt.Dimension(46, 88));
Your list won't get much bigger if you do this:
chooseNamesList.setMaximumSize(new java.awt.Dimension(46, 88));
伙计们,我自己解决了这个问题..感谢你们的协调..实际上,我犯了一个错误,将weightx和weighty赋予了父面板,该面板同时包含Jlist和JEditor窗格...简单地说,我们需要设置包含要调整大小的任何组件的所有父容器的权重参数。
guys i solved this problem myself .. thnks for ur co-ordination ..actually i did the mistake of giving weightx and weighty to the parent panel which is holding the both the Jlist and for the JEditor pane ... briefly we need to set weight parameters for all the parent containers which contains any component which you want to resize..
如果设置原型单元格值,则数据更改时 JList 将不会调整大小。
请参阅 setPrototypeCellValue(Object) 方法的 JList Javadoc 页面。
If you set a prototype cell value, the JList will not resize when the data is changed.
See The JList Javadoc page for the setPrototypeCellValue(Object) method.