Disable Button in the TableColumnGroup

This is the forum for JIDE Common Layer which is open sourced at https://github.com/jidesoft/jide-oss. Please note, JIDE technical support doesn't monitor this forum as often as other forums. Please consider subscribe for technical support for JIDE Common Layer so that you can use customer only forum to get a timely response.

Moderator: JIDE Support

Forum rules
Community driven forum for open source JIDE Common Layer. JIDE technical support doesn't monitor this forum as often as other forums. If you only use JIDE Common Layer, please consider subscribing for technical support for JIDE Common Layer so that you can use customer only forum to get a timely response.

Disable Button in the TableColumnGroup

Postby gaolifeng » Wed Sep 17, 2014 4:32 pm

Hi
I am trying to implement a Button Renderer in the TableColumnGroup, and disable the Buttons at a certain time.
Below is a sample code of disabling the Buttons in a table cell and table header.
Before I am using the TableColumnGroup to group the columns, the code is working, both buttons in the table cell and table header can be disabled.
However, Once I added the table columns into the TableColumnGroup, neither the button in the table header nor the button in the TableColumnGroup can be disabled.

The code highlighted in blue are the new code, without them disabling the table header is still working.

Does anyone have any suggestions on how I can make the buttons in TableColumnGroup disabled?



Thanks!

--------------------------------------------------------------------------------


public class TableButton implements ActionListener {
JTable table;
TableColumnGroup tcg;
public void actionPerformed(ActionEvent e) {
JButton editorButton = (JButton)e.getSource();
String ac = editorButton.getActionCommand();
System.out.println("ac = " + ac);
}

private JScrollPane getContent() {
table = new JTable(getModel());
table.setCellSelectionEnabled(true);
int height = (new JButton(" ")).getPreferredSize().height;
table.setRowHeight(height);
NestedTableHeader header = new NestedTableHeader(table);
table.setTableHeader(header);

TableColumnModel colModel = table.getColumnModel();

colModel.getColumn(0).setCellRenderer( new ButtonRenderer());
colModel.getColumn(0).setHeaderRenderer( new ButtonRenderer());

tcg = new TableColumnGroup("A and B");
tcg.add(colModel.getColumn(0));
tcg.add(colModel.getColumn(1));
tcg.setHeaderRenderer( new ButtonRenderer());
header.addColumnGroup(tcg);


return new JScrollPane(table);
}

private AbstractTableModel getModel() {
return new AbstractTableModel() {
public int getColumnCount() { return 3; }
public int getRowCount() { return 6;}
public boolean isCellEditable(int row, int col) {
return col == 0;
}
public Object getValueAt(int row, int col) {
if(col == 0)
return "Button " + (row + 1) + (col + 1);
return String.valueOf(row + 1) + (col + 1);
}
};
}

private JPanel getLastPanel() {
JToggleButton toggle = new JToggleButton("Disable Buttons");
toggle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JToggleButton tb = (JToggleButton)e.getSource();
TableColumn col= table.getColumnModel().getColumn(0);
ButtonRenderer renderer = (ButtonRenderer)col.getCellRenderer();
ButtonRenderer headerrenderer = (ButtonRenderer)col.getHeaderRenderer();
ButtonRenderer colgrouprenderer = (ButtonRenderer)tcg.getHeaderRenderer();
boolean enable;
if(tb.isSelected()) {
enable = false;
tb.setText("Enable Buttons");
} else {
enable = true;
tb.setText("Disable Buttons");
}
renderer.setEnabled(enable);
headerrenderer.setEnabled(enable);
colgrouprenderer.setEnabled(enable);

table.getTableHeader().repaint();
table.repaint();
}
});
JPanel panel = new JPanel();
panel.add(toggle);
return panel;
}

public static void main(String[] args) {
TableButton test = new TableButton();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test.getContent());
f.getContentPane().add(test.getLastPanel(), "Last");
f.setSize(340,300);
f.setLocation(200,200);
f.setVisible(true);
}
}

class ButtonRenderer implements TableCellRenderer {
JButton button;
boolean enabled = true;

public ButtonRenderer() {
button = new JButton();
}

public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row, int column) {
String text = (String)value;
button.setText(text);
button.setEnabled(enabled);
return button;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
gaolifeng
 
Posts: 7
Joined: Tue May 22, 2012 7:33 am

Re: Disable Button in the TableColumnGroup

Postby JIDE Support » Thu Sep 18, 2014 7:47 am

I'll fix it for the next release. What happened is we set the header renderer component to enabled again by syncing it with header.isEnabled(). I guess my fix would be if the renderer component is already disabled but the header is enabled, I shouldn't change it.
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 36107
Joined: Sun Sep 14, 2003 10:49 am

Re: Disable Button in the TableColumnGroup

Postby gaolifeng » Thu Sep 18, 2014 8:32 am

Thanks you very much! If that is the case, I think I found a workaround, I can disable the table header based on the enable value.
Just tried to call table.getTableHeader().setEnabled(enable) before renderer.setEnabled(enable), it works then.

thanks again!
gaolifeng
 
Posts: 7
Joined: Tue May 22, 2012 7:33 am

Re: Disable Button in the TableColumnGroup

Postby JIDE Support » Thu Sep 18, 2014 9:01 am

I don't think so. If you disable the header, all cell renderer components will be disabled in the existing code.
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 36107
Joined: Sun Sep 14, 2003 10:49 am

Re: Disable Button in the TableColumnGroup

Postby gaolifeng » Thu Sep 18, 2014 9:18 am

That is right! I can see all the column headers are disabled as well.
So, please do fix this so that only the header renderer is disabled while headers are still enabled. Thanks!
gaolifeng
 
Posts: 7
Joined: Tue May 22, 2012 7:33 am

Re: Disable Button in the TableColumnGroup

Postby JIDE Support » Tue Oct 28, 2014 8:36 pm

Just so you know, 3.6.3 is released with this bug fixed.
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 36107
Joined: Sun Sep 14, 2003 10:49 am


Return to JIDE Common Layer Open Source Project Discussion (Community Driven)

Who is online

Users browsing this forum: No registered users and 1 guest