- I am trying to add separated column for button where every row will have have button and on click of the button color of the row should change
- I am also trying to add checkbox column for every row
- And at last a submit button, below table which on click will show the pop of selected row via checkbox
I can't think of logic of the problem.
My code is below
public class Main {
private static final JFrame jFrame = new JFrame();
public static void main(String[] args) {
JPanel panel = new JPanel();
jFrame.setSize(350,200);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.add(panel);
panel.setLayout(new FlowLayout());
JComboBox<String> cb = new JComboBox<>(new String[] {"","Show","Hide"});
panel.add(cb);
JButton button = new JButton("change");
button.setBounds(10,80,80,25);
panel.add(button);
String[][] rec = {
{ "1", "Steve", "AUS" },
{ "2", "Virat", "IND" },
{ "3", "Kane", "NZ" },
{ "4", "David", "AUS" },
{ "5", "Ben", "ENG" },
{ "6", "Eion", "ENG" },
};
String[] header = { "Rank", "Player", "Country" };
JTable table = new JTable(rec, header);
panel.add(new JScrollPane(table));
table.setVisible(false);
cb.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
// YourType varName = (YourType)comboBox.getSelectedItem();`
String value = cb.getSelectedItem().toString();
if ("Show".equalsIgnoreCase(value)) {
table.setVisible(true);
}
else {
table.setVisible(false);
}
}
}
});
jFrame.setVisible(true);
}
}
I am done implementing checkBox in the table but can't think of How to add button in this
public class MyTable extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
//initialize Jframe
MyTable form = new MyTable();
form.setVisible(true);
}
});
}
public MyTable(){
//the form
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(200,200,800,300);
setTitle("Form");
getContentPane().setLayout(null);
//add scroll pane
JScrollPane pane = new JScrollPane();
pane.setBounds(70,80,600,200);
getContentPane().add(pane);
//the table
final JTable table = new JTable();
pane.setViewportView(table);
//the model of table
DefaultTableModel model = new DefaultTableModel(){
public Class<?> getColumnClass(int column){
switch (column){
case 0:
return String.class;
case 1:
return String.class;
case 2:
return String.class;
case 3:
return String.class;
case 4:
return Boolean.class;
default:
return String.class;
}
}
};
// setting model of the table
table.setModel(model);
model.addColumn("Points");
model.addColumn("Position");
model.addColumn("Team");
model.addColumn("Manager");
model.addColumn("Select");
for(int i = 0; i < 7; i++){
model.addRow(new Object[0]);
model.setValueAt("Column 3",i,0);
model.setValueAt("Our Row"+(i+1),i,1);
model.setValueAt("Column 2",i,2);
model.setValueAt("Column 4",i,3);
model.setValueAt(false,i,4);
}
//obtain selected
JButton button = new JButton("Submit");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//get selected item
for(int i = 0; i < table.getRowCount(); i++){
Boolean checked = Boolean.valueOf(table.getValueAt(i,4).toString());
String col = table.getValueAt(i,1).toString();
//Display
if(checked){
JOptionPane.showMessageDialog(null,col);
}
}
}
});
// Add button to form
button.setBounds(20,30,130,30);
getContentPane().add(button);
}
}
Thanks! I am stuck in this for few days, does anyone know how to implement this.
The easy way to place a JCheckBox in a JTable is a TableModel that has a boolean value in one of the columns (see TableModel.getColumnClass())
To get both a JCheckBox and a JButton into one cell create your custom TableCellRenderer. See also Swing JTable with custom TableCellRenderer