Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java jtable allow row selection

Tags:

java

swing

jtable

I am trying to make a jtable which displays a list of users. The table should allow users to select an entire row but not allow editing of the cells.

So far i have this, it stops them from editing cells but how to i allow them to select the rows instead of cells?

DefaultTableModel userTableModel = new DefaultTableModel(new Object[]{"Customer ID", "First Name", "Last Name"}, 0) {
    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }
};

And this i show i am populating the table:

public void refreshCustomersList() throws SQLException, ClassNotFoundException {

    UserBeanList userList = dbConnector.getUserData();

    for (int i = 0; i < userList.size(); i++) {
        UserBean userBean = userList.getUserBeanAt(i);

        String[] data = new String[3];

        data[0] = userBean.getCustomerID();
        data[1] = userBean.getFirstName();
        data[2] = userBean.getLastName();

        userTableModel.addRow(data);

    }
    tableCustomers.setModel(userTableModel);
}

As i said i have disabled cell editing but how do i only allow row selection.

I have seen posts from other people saying i should put this but i not sure were to put it.

selectionTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

Any help would be great.

like image 906
user667430 Avatar asked Aug 19 '26 15:08

user667430


1 Answers

Have you tried setRowSelectionAllowed(true) on your JTable instance?

I would suggest trying to look at the javadocs http://docs.oracle.com/javase/6/docs/api/javax/swing/JTable.html#setRowSelectionAllowed(boolean)

and read the tutorial linked from the javadocs: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

They are pretty thorough :)

like image 121
JustDanyul Avatar answered Aug 22 '26 05:08

JustDanyul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!