JTable - 使用PopupMenu切换列(如Windows资源管理器) [英] JTable - Toggle Columns with PopupMenu (like Windows Explorer)

查看:174
本文介绍了JTable - 使用PopupMenu切换列(如Windows资源管理器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Swing中创建一个JTable,允许用户使用简单的下拉菜单切换列,就像在Windows资源管理器文件夹中一样?我的表有12列,屏幕无法同时显示屏幕上的所有信息。由于用户可能不需要看到许多这些列,这似乎是最好的选择。



我假设它将使用 JPopupMenu 当用户右键单击表头时出现,每列的 JCheckBoxMenuItem



<这是我正在尝试复制的Window的列切换器的屏幕截图:

解决方案


如何在Swing中创建允许用户$ b $的JTable b用简单的下拉菜单切换列,比如Windows
资源管理器文件夹?


也许你可以尝试 JXTable (可在 SwingX 库中找到)从 JTable 和提供这样的功能。此代码snipet完成所有操作:

  DefaultTableModel model = new DefaultTableModel(new Object [] {Column#1,Column #2,第3列,第4列},10); 

JXTable table = new JXTable(model);
table.setColumnControlVisible(true); //这添加了JXTable



屏幕截图



注意负责显示菜单的右上角按钮:




How would I go about creating a JTable in Swing that allows the user to toggle columns with a simple dropdown meun, like in Windows Explorer folders? My table has 12 columns, and the screen cannot fit all the information on the screen at once. Since the user may not need to see many of these columns, this seems like the best option.

I assume it would use a JPopupMenu that appears when the user right-clicks the table header, with a JCheckBoxMenuItem for each column.

Here's a screenshot of Window's column toggler that I'm trying to replicate:

解决方案

How would I go about creating a JTable in Swing that allows the user to toggle columns with a simple dropdown meun, like in Windows Explorer folders?

Maybe you can try JXTable (available in SwingX library) which extends from JTable and provides such feature. This code snipet does it all:

DefaultTableModel model = new DefaultTableModel(new Object[]{"Column # 1", "Column # 2", "Column # 3","Column # 4"}, 10);

JXTable table = new JXTable(model);
table.setColumnControlVisible(true); // this added with JXTable

Screenshot

Pay attention to the top-right corner button which is responsible to display the menu:

这篇关于JTable - 使用PopupMenu切换列(如Windows资源管理器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆