JTable Boolean.class
Asked Answered
O

3

1
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

class ColorTableModel extends AbstractTableModel {

  Object rowData[][] = { { "value1", Boolean.FALSE },
      { "value1", Boolean.FALSE }, { "value1", Boolean.FALSE },
      { "value1", Boolean.FALSE}, { "value1", Boolean.FALSE }, };

  String columnNames[] = { "English", "Boolean" };

  public int getColumnCount() {
    return columnNames.length;
  }

  public String getColumnName(int column) {
    return columnNames[column];
  }

  public int getRowCount() {
    return rowData.length;
  }

  public Object getValueAt(int row, int column) {
    return rowData[row][column];
  }

  public Class getColumnClass(int column) {
    return (getValueAt(0, column).getClass());
  }

  public void setValueAt(Object value, int row, int column) {
    rowData[row][column] = value;
  }

  public boolean isCellEditable(int row, int column) {
    return (column != 0);
  }
}

public class EditableColorColumn {

  public static void main(String args[]) {
    JFrame frame = new JFrame("Editable Color Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    TableModel model = new ColorTableModel();
    JTable table = new JTable(model);
    // TableColumn column = table.getColumnModel().getColumn(3);
    // column.setCellRenderer(renderer);
    // column.setCellEditor(editor);

    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }

I would like to get the value of column one if I ticked the corresponding row. I have read a lot about this, but I can't just put it to code. Can you code a some one?

Sample scenario, when I ticked row1 checkbox, it will system.out.println() with a result of value1.

Offcolor answered 21/11, 2012 at 16:6 Comment(0)
T
4

Your implementation of setValueAt() in your AbstractTableModel fails to fire the event that would notify listeners of the change:

@Override
public void setValueAt(Object value, int row, int column) {
    rowData[row][column] = value;
    fireTableCellUpdated(row, column);
}

Once that is corrected, a TableModelListener will see each change. Try commenting out the fireTableCellUpdated() line to see the difference.

In addition:

Code:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 * @see https://mcmap.net/q/911305/-jtable-boolean-class/230513
 */
public class EditableColorColumn {

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new EditableColorColumn().display();
            }
        });
    }

    private void display() {
        JFrame frame = new JFrame("Editable Color Table");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final TableModel model = new ColorTableModel();
        JTable table = new JTable(model);
        table.setPreferredScrollableViewportSize(new Dimension(400, 150));
        table.getModel().addTableModelListener(new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {
                System.out.println(model.getValueAt(e.getFirstRow(), 0)
                    + " " + model.getValueAt(e.getFirstRow(), 1));
            }
        });

        frame.add(new JScrollPane(table), BorderLayout.CENTER);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static class ColorTableModel extends AbstractTableModel {

        String columnNames[] = {"English", "Boolean"};
        Object rowData[][] = {
            {"value1", Boolean.FALSE},
            {"value2", Boolean.TRUE},
            {"value3", Boolean.FALSE},
            {"value4", Boolean.TRUE},
            {"value5", Boolean.FALSE},};

        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }

        @Override
        public int getRowCount() {
            return rowData.length;
        }

        @Override
        public Object getValueAt(int row, int column) {
            return rowData[row][column];
        }

        @Override
        public Class getColumnClass(int column) {
            return (getValueAt(0, column).getClass());
        }

        @Override
        public void setValueAt(Object value, int row, int column) {
            rowData[row][column] = value;
            fireTableCellUpdated(row, column);
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return (column != 0);
        }
    }
}
Township answered 21/11, 2012 at 19:44 Comment(0)
B
0

You can use getSelectedRow() to get row which was selected. Then use getValueAt(row, column) where row is the value which you got from getSelectedRow() and column is the column which you want. Try following :-

int row=table.getSelectedRow();

String value=table.getValueAt(row, 0).toString();

where 0 means it will return the value of first column.

Blen answered 21/11, 2012 at 16:15 Comment(7)
int row=table.getSelectedRow(); String value=table.getValueAt(row, 0).toString();Blen
You should first define MouseListner function for jtable. Then only you will be able to capture clicks on your jtable.Blen
even when I click the non checkbox cell, it system.out.println the value of the cell. How can I get rid of that? It must be only the column with checkbox.Offcolor
You can use getSelectedColumn() to know which column was clicked. And then you can use simple if loop to do something only if particular column was clicked.Blen
But the checkbox function is nonfunctional if thats the case, I can get the value of the row, even if i deselect the checkbox which it should be not. I wanted to get the value if the checkbox is ticked.Offcolor
Its very simple. If you want value only when checkbox is clicked just add logic to make sure code is executed only if value of checkbox is true .Blen
Thank you Addict. So true, just simple logic. :( Ive been working on this, this past days and I dig too deep due to a lot of codes I've seen. But didn't think about what you suggested. Thanks.Offcolor
P
0

If only you need to get value of the selected column, try this

table.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            int row = table.getSelectedRow();
            System.out.println("Selected Row ; " + row );
            System.out.println("Velue : " + model.getValueAt(row, 0));
        }
});
Pruett answered 21/11, 2012 at 17:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.