这可能是一个模糊的查询,所以请原谅我。
定制的 JTable(我已经修改了查询,将根据提供的 SSCCE 进行讨论)。我必须创建一个 JTable 以根据 JTable 中选定的复选框提供授权
此 JTable 的目的是向用户展示应用程序的所有菜单选项。这个 JTable 有三列: 第一列:Bollean 类(复选框) 第二列:String 类(主菜单项) 第三列:String 类(子菜单项)
要提供授权,用户应选择与子菜单项对应的复选框,最后选择“授权”按钮(我没有在其中包含授权按钮,因为我的授权功能工作正常)
现在的 UI 要求是在 JTable 的第一列中,我应该只显示与子菜单项对应的复选框,而不是在第一列的每个单元格中显示复选框(换句话说,它不应该显示与主菜单菜单项对应的复选框)
下面的图片是预期的输出(尽管我在第一列中使用复选框获取所有单元格)
public class SwingSolution extends JPanel {
public SwingSolution() {
super(new GridLayout(1,0));
String[] columnNames = {"", "Main Menu", "Sub Menu"};
Object[][] data = {
{false, "File", ""},
{false, "", "New"},
{false, "", "Save"},
{false, "", "Close"},
{false, "Edit", ""},
{false, "", "Delete"},
{false, "", "Format"},
{false, "Project", ""},
{false, "", "Create New"},
{false, "", "Delete"},
{false, "", "Build"},
{false, "", "Properties"},
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
final JTable table = new JTable(model) {
private static final long serialVersionUID = 1L;
@Override
public Class getColumnClass(int column) {
switch (column) {
case 0:
return Boolean.class;
case 1:
return String.class;
case 2:
return String.class;
default:
return Boolean.class;
}
}
};
table.getColumnModel().getColumn(0).setMaxWidth(30);
table.getColumnModel().getColumn(1).setMaxWidth(100);
table.getColumnModel().getColumn(2).setMaxWidth(120);
table.setPreferredScrollableViewportSize(new Dimension(250, 195));
table.setFillsViewportHeight(true);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
SwingSolution newContentPane = new SwingSolution();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
我用单元格渲染器尝试了各种事情,并在谷歌上搜索了 JTable 和自定义单元格,但无法弄清楚。任何帮助将不胜感激