Reputation: 93
I am creating a JTable in swing and I have managed to create a table that is scrollable with some random data added to it. Now I ma trying to add a check box in the last column of every row.
Now this is what I am getting so far:
When you look at he table you can see that instead of getting checkboxes I am getting a column with a string value "false" which is not what I want. I want a selectable box and I want to allow multiple selections. This is the code from the class:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;
public class BiogramTable extends JFrame {
private JPanel contentPane;
private JTable table;
private JCheckBox checkbox;
private static final long serialVersionUID = 1L;
/**
* Create the frame.
*/
public BiogramTable() {
Object[] columns = {"Name", "Age" , "Gender" , "Boolean"};
Object[][] data = {{"John", "18", "Male", false},
{"Jessica", "19", "Female", false},
{"Dave", "52", "Male", false},
{"Jake", "30", "Male", false},
{"Jeremy", "14", "Male", false},
{"Jemma", "34", "Female", false},
{"Amy", "16", "Female", false},
{"Patrick", "18", "Male", false}};
DefaultTableModel model = new DefaultTableModel(data, columns);
final JCheckBox checkBox = new JCheckBox();
table = new JTable(model) {
private static final long serialVersionUID = 1L;
//@Override
//public Class getColumnClass(int column) {
//switch (column) {
//case 0:
// return String.class;
//case 1:
//return String.class;
//case 2:
//return Integer.class;
//case 3:
//return Double.class;
//default:
//return Boolean.class;
//}
//}
};
//table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
BiogramTable frame = new BiogramTable();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
});
}
}
I have done a lot of research on this but I haven't managed to find a solution using swing. I would be great if you could let me know what is wrong with my method and let me know what the correct method is as this is the first time I am creating a JTable in Java swing using Windows builder. I have seen a similar question to mine in stack overflow but my problem is different as I am getting String rather than checkboxes. So my question is why is this?
Upvotes: 1
Views: 908
Reputation: 1260
You should Override getColumnClass
method according to your data requirement. Refer to below changes.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;
class BiogramTable extends JFrame {
private JPanel contentPane;
private JTable table;
private JCheckBox checkbox;
private static final long serialVersionUID = 1L;
/**
* Create the frame.
*/
public BiogramTable() {
Object[] columns = {"Name", "Age", "Gender", "Boolean"};
Object[][] data = {{"John", "18", "Male", false},
{"Jessica", "19", "Female", false},
{"Dave", "52", "Male", false},
{"Jake", "30", "Male", false},
{"Jeremy", "14", "Male", false},
{"Jemma", "34", "Female", false},
{"Amy", "16", "Female", false},
{"Patrick", "18", "Male", false}};
DefaultTableModel model = new DefaultTableModel(data, columns);
final JCheckBox checkBox = new JCheckBox();
table = new JTable(model) {
private static final long serialVersionUID = 1L;
@Override
public Class getColumnClass(int column) {
switch (column) {
case 0:
return String.class;
case 1:
return Integer.class;
case 2:
return String.class;
case 3:
return Boolean.class;
default:
return String.class;
}
}
};
//table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
BiogramTable frame = new BiogramTable();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
});
}
}
Upvotes: 3