JTable - Toggle Columns with PopupMenu (like Windows Explorer)

十年热恋 提交于 2019-12-04 17:29:54

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!