JCombobox、编辑器和渲染器相关
作为 JCombobox ListCellRenderer,我有一个这样的类:
class ZComboBoxRenderer extends JPanel implements ListCellRenderer{
private ZGrid grid;
public ZComboBoxRenderer(ZGrid grid) {
setLayout(new BorderLayout());
this.grid = grid;
add(new JScrollPane(grid), BorderLayout.CENTER);
}
public ZGrid getGrid(){
return grid;
}
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
grid.fetchSQL();
return this;
}
}
这里的 ZGrid
扩展了 JTable
。
作为 ListCellRendererComponent
,我向 JCombobox
提供一个内部有 ZGrid
的 JPanel。问题是,在它的列表中,这个 ZGrid 正在正确绘制。但它也在 JCombobox 的编辑器内进行绘制。我上传了一张图片以更好地展示这一点。
有没有办法将编辑器与列表分开?
As a JCombobox ListCellRenderer, I have a class like this one:
class ZComboBoxRenderer extends JPanel implements ListCellRenderer{
private ZGrid grid;
public ZComboBoxRenderer(ZGrid grid) {
setLayout(new BorderLayout());
this.grid = grid;
add(new JScrollPane(grid), BorderLayout.CENTER);
}
public ZGrid getGrid(){
return grid;
}
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
grid.fetchSQL();
return this;
}
}
ZGrid
here, extends JTable
.
As a ListCellRendererComponent
, I provide a JPanel which has a ZGrid
inside, to the JCombobox
. The problem is, in its list, this ZGrid is painting properly. But it is also being painted inside the Editor of JCombobox. I have uploaded an image to show this better.
Is there a way to separate Editor from List?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我了解,您正在为
JComboBox
实现一个自定义渲染器,尽管它正确地渲染了下拉列表的内容,但它完全弄乱了组合框的当前值。我看到您可以使用两个选项:
您可以扩展
JComboBox
的 UI 组件并重写paint
方法来获取当前网格的自定义表示形式价值观。这将是一个非常快速的概念证明,但它会带来问题,因为您需要扩展您希望应用程序运行的每个 UI(金属、Windows、Mac 等)。您可以滚动自己的下拉列表,并使其看起来像
JComboBox
。作为 POC,这也不是那么困难,但这里的复杂性是处理影响组合框周围的选择和导航的不同键盘输入。From what I understand, you are implementing a custom Renderer for your
JComboBox
, and though it correctly renders the contents of your dropdown, it completely messes up the current value of the combo box.I see two options at your disposal:
you can extend the UI component for your
JComboBox
and override thepaint
method to get a custom representation of your grid for your current value view. This would be a pretty quick proof of concept, but it poses issues as you would need to extend every UI (metal, windows, mac, etc) that you expect your app to be running with.you can roll your own dropdown, and make it look like a
JComboBox
. This would not be that difficult to do as a POC as well, but the complexity here is to handle the different keyboard inputs that influence the selection and navigation around the combo box.