我正在尝试从其单元格渲染器getListCellRendererComponent()方法中获取JList的宽度。
我得到的疏远行为是,只要它被放大,我得到正确的宽度,但是对于小于原始宽度的尺寸,我总是得到它在第一次绘制时的宽度。
这适用于list.getWidth(),list.getSize(),list.getBounds()和list.getCellBounds()。 将包含ScrollPanel的列表的宽度与parent.getxxxxx()一起使用可以正常工作。
顺便说一下,这个JList上没有设置min / max / preferredSize。
此时我不知所措..
修改
因此,似乎在调整大小时不会重新验证包含的ScrollPanel。不应该自动调用revalidate()吗?请注意,这不是以编程方式完成的任何操作,而只是用户执行的鼠标框架调整大小。
编辑2:
是的,好吧,JList的几何有一些复杂的内在性,在一天结束时与实际屏幕真实状态完全无关。任何对后者感兴趣的人都可以更好地获取包含ScrollPane的ViewPort并查询(getWidth,getHaight,getSize,getBounds)。
编辑3
这是根据要求的SSCCE。虽然不是很短暂,但我想保留Netbeans的布局代码,以防它对这个问题产生影响。有了这个,我得到一个667的JList宽度,这似乎与最长项目的长度有些相关(在667它开始显示末尾的省略号)。
只要视口的宽度为667,jlists宽度就会开始跟踪视口的宽度。缩小时,此跟踪在667处停止并保持不变。
package com.dafquest.operator.mvc;
import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JLabel;
import javax.swing.JList;
public class JListSSCCE extends javax.swing.JFrame {
public JListSSCCE() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setName("Form");
jPanel1.setName("jPanel1");
jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane1.setName("jScrollPane1");
jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "This is this Jlist's Item 1", "A shorter Item 2", "For some reason Item 3 seems to be longer", "Certainly issue whith item 3 must be a mirage, Item 4 *is* a really long item", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jList1.setCellRenderer(new MyCellRenderer());
jList1.setName("jList1");
jScrollPane1.setViewportView(jList1);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JListSSCCE.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new JListSSCCE().setVisible(true);
}
});
}
private javax.swing.JList jList1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private class MyCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel comp = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
String widths = "(SPViewPort: " + list.getParent().getWidth()
+ ", Jlist: " + list.getWidth()
+ ", Cell: " + comp.getWidth()
+ ", CellPref: " + comp.getPreferredSize().width + ") ";
comp.setText(widths + comp.getText());
// Lets see after adding text, might be that JList width is equal
// tho longest item's preferred width?
widths = "(SPViewPort: " + list.getParent().getWidth()
+ ", Jlist: " + list.getWidth()
+ ", Cell: " + comp.getWidth()
+ ", CellPref: " + comp.getPreferredSize().width + ") ";
System.out.println(widths);
return comp;
}
}
}
答案 0 :(得分:1)
问题在于您有效地更改了列表的大小要求而未通知它: - )
以下是您的渲染器已修改以演示行为:
somefixedChars + 2
somefixedChars + 2* digitsOfSize
解决方案取决于您的实际用例:您需要找到一种方法来强制ui使其大小缓存无效。 SwingX' JXList有一个方法,如:
xList.invalidateCellSizeCache();
对于核心,您可以通过重新设置固定大小来破解:
list.setFixedCellWidth(somearbitraryValue);
list.setFixedCellWidth(-1);
要使用的渲染器:
private class MyCellRenderer extends DefaultListCellRenderer {
private int zeroPref;
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel comp = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
String widths = "(SPViewPort: " + list.getParent().getWidth()
+ ", Jlist: " + list.getWidth()
+ ", CellPref: " + comp.getPreferredSize().width + ") ";
comp.setText(widths + comp.getText());
if (list.getWidth() == 0) {
// keep track of the pref for a zero sized list/viewport
zeroPref = Math.max(comp.getPreferredSize().width, zeroPref);
}
//list is sized to its initial pref
widths = //"index: " + index
"initial: " + zeroPref
+ "(SPViewPort: " + list.getParent().getWidth()
+ ", Jlist: " + list.getWidth()
+ ", CellPref: " + comp.getPreferredSize().width + ") ";
System.out.println(widths);
return comp;
}
}