This may be a vague query, so please pardon me.
Customized JTable (I've modified the query and will discuss based on the SSCCE provided). I've to create a JTable to provide authorization based on selected checkboxes in JTable
Purpose of this JTable is to show users with all of the menu options of the application. This JTable have three columns: First column: class Bollean (checkbox) Second column: class String (main menu items) Third column: class String (sub-menu items)
To provide authorization user shall select checkbox corresponding to sub-menu items and finally select "Authorize" button (I haven't included authorize button in this as my authorize functionality is working fine)
Now the UI requirement is that in the first column of JTable, I should display checkboxes corresponding to sub-menu items only instead of displaying checkbox in each cell of first column (in other words it should not display checkbox corresponding to main menu menu item)
Pic below is expected output (although I'm getting all cells in first column with checkbox)
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();
}
});
}
}
I tried various things with cell renderer and googled about JTable and custom cells but couldn't figured it out. Any help will be greatly appreciated
Override the
getCellRenderer()
method to return an appropriate renderer. Something like:You would also need to change the data in the model:
Finally you would also override the
isCellEditable()
method to make the empty cell non editable.Then you can just use the default Boolean renderer/editor for the other rows in the column.
Basically, you're going to have supply you're cell renderer and editor.
In this case, I changed the first columns value/type to
int
. This allows me to supply additional meaning beyondboolean
.If the column value is
0
then the cell is not "selectable",1
is unchecked,2
is checked.I also altered the
isCellEditable
method of theTableModel
to only allow the "active" cells to be editable.I've not done the editor, but the basic concept is the same...
You can use the
Boolean
wrapper class instead of the raw type and write your ownTableCellRenderer
and override thegetTableCellRendererComponent(..)
method.Then just simply set
infos.setDefaultRenderer(Boolean.class, new CustomTableCellRenderer());
and replace your raw types in the array withnew Boolean(true)
. Wherever you don't want to have aJCeckBox
you put anull
instead of aBoolean
and an emptyJPanel
will appear.