Hi all,
i don't manage to set a jCheckBox in a jTable...
Can anyone tell me what's wrong with my code.
Thanks
//******************************************************************
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.lang.*;
import javax.swing.JTable;
public class Sans_titre1 extends JPanel implements TableModelListener {
private final String[] columnNames = { "Rayon", "Produit", "Quantit?",
"Achet?"};
private JButton buttonSave;
private JTable table;
private DefaultTableModel tableModel;
//******************************************************************
public Sans_titre1() {
tableModel = new DefaultTableModel(columnNames, 0);
tableModel.addTableModelListener(this);
Object[] data = { new String("Frais"), new String("Pommes"), new
Integer(5), new Boolean(true)};
tableModel.addRow(data);
table = new JTable(tableModel);
javax.swing.table.TableColumn var_col;
var_col = table.getColumnModel().getColumn(3);
JCheckBox check = new JCheckBox();
var_col.setCellEditor(new DefaultCellEditor(check));
JScrollPane scrollPane = new JScrollPane(table);
setLayout(new BorderLayout());
setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
add(BorderLayout.NORTH, new JLabel("Mon panier", JLabel.CENTER));
add(BorderLayout.CENTER, scrollPane);
JButton buttonAdd = new JButton("Ajouter");
buttonAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
Object[] data = {
new String("Test"), new String("Test"), new Integer(0),
new Boolean(false)};
tableModel.addRow(data);
}
});
buttonSave = new JButton("Sauver");
buttonSave.setEnabled(false);
buttonSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
buttonSave.setEnabled(false);
}
});
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(buttonAdd);
buttonsPanel.add(buttonSave);
add(BorderLayout.SOUTH, buttonsPanel);
setPreferredSize(new Dimension(200, 250));
}
public void tableChanged(TableModelEvent e) {
if (e.getType() == TableModelEvent.UPDATE) {
buttonSave.setEnabled(true);
}
}
//******************************************************************
public static void main(String[] args) {
JFrame frame = new JFrame("Mon Panier");
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add("Center", new Sans_titre1());
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
frame.addWindowListener(wndCloser);
frame.pack();
frame.show();
}
}
//******************************************************************
Thomas Fritsch - 16 Dec 2003 12:48 GMT
>Hi all,
>i don't manage to set a jCheckBox in a jTable...
When running your code, I saw in column "Acheté", that this column is
rendered as a "true"/"false" String.
Only when clicking into it I saw a JCheckBox (with the check mark on/off)=2E
I suppose this is the problem you mean.
>Can anyone tell me what's wrong with my code.
>Thanks
The reason is, that you have managed to get a JCheckBox as the
TableCellEditor of column 3,
but still have the default JLabel as its TableCellRenderer.
>...
>public class Sans_titre1 extends JPanel implements TableModelListener {
> private final String[] columnNames = { "Rayon", "Produit", "Quantité", "Acheté"};
private final Class[] columnClasses { String.class, String.class, Integer.class, Boolean.class };
> private JButton buttonSave;
> private JTable table;
[quoted text clipped - 3 lines]
> public Sans_titre1() {
> tableModel = new DefaultTableModel(columnNames, 0)
{
// Override getColumnClass (the superclass always returned
Object.class).
// The table will then select an appropriate CallEditor and(!)
CellRenderer
// according to the returned column class.
public Class getColumnClass(int column)
{
return columnClasses[column];
}
}
>;
>
[quoted text clipped - 5 lines]
> table = new JTable(tableModel);
>
// You don't need the following 4 lines anymore, because of the
getColumnClass overriding above.
> // javax.swing.table.TableColumn var_col;
> // var_col = table.getColumnModel().getColumn(3);
[quoted text clipped - 6 lines]
>
>...
Greetings
Thomas
Todd Corley - 16 Dec 2003 13:50 GMT
change
var_col.setCellEditor(new DefaultCellEditor(check));
to
var_col.setCellEditor( table.getDefaultEditor( Boolean.class ) );
var_col.setCellRenderer( table.getDefaultRenderer( Boolean.class ) );