Brian
Brian

Reputation: 2051

Java - JTable selecting first column by default

I have this piece of code:

listTable.addMouseListener(new MouseAdapter() {
                 public void mouseClicked(MouseEvent e) {

                  if (e.getClickCount() == 1) {
                  JTable target = (JTable)e.getSource();

                  int row = target.getSelectedRow();
                  int column = target.getSelectedColumn();

                  String title = listTable.getValueAt(row, column).toString();
                  String duration = listTable.getValueAt(row, column+1).toString();
                  String genre = listTable.getValueAt(row, column+2).toString();
                  String actor = listTable.getValueAt(row, column+3).toString();
                  String director = listTable.getValueAt(row, column+4).toString();
                  //String rentable = listTable.getValueAt(row, column+5).toString();
                  //String synopsis = listTable.getValueAt(row, column+6).toString();

                  txtTitle.setText(title);
                  txtDuration.setText(duration);
                  txtGenre.setText(genre);


                  }
                });

Which enables me:

However, when I don't click the first column by default, the selection gets garbled up. For example, if I don't click on the "Title" column first and click on the "Duration" column, it puts the duration in txtTitle and the others are also mixed up.

How do I add a piece of code that when a row is selected, it default-selects the first column ?

This is a screenshot of what happens:

enter image description here

Thanks, Brian

Upvotes: 0

Views: 4053

Answers (4)

Priyanshu Jha
Priyanshu Jha

Reputation: 575

Its obvious, as When you click on Duration Column

int column = target.getSelectedColumn();

gets the column value of Duration and thus

String title = listTable.getValueAt(row, column).toString();

gets the value from the duration column.. the best way to tackle this is hard code column no. Suppose your Table is in Following Order Title : column 0 Duration : Column 1 Genre : Column 2 and so on

simply put String title = listTable.getValueAt(row, 0).toString();

String duration = listTable.getValueAt(row,1).toString(); 

and so on..!!

Hope it helps

Upvotes: 0

Ashwinee K Jha
Ashwinee K Jha

Reputation: 9317

Rather than forcing selection of column why not always extract values based on known columns?

For example you can replace:

String title = listTable.getValueAt(row, column).toString();

with:

String title = listTable.getValueAt(row, 0).toString();

If you provide ability to reorder columns then you can instead try table.getModel().getValueAt(convertRowIndextoModel(row), 0);

Upvotes: 1

mKorbel
mKorbel

Reputation: 109823

Everything depends how is set ListSelectionModel, but works for me

myTable.changeSelection(row, column, false, false);

Upvotes: 3

Attila
Attila

Reputation: 28802

Change this

int column = target.getSelectedColumn(); 

to

int column = 0; 

Your version gets the column index from where the user clicked, you need the first one (index 0)

Upvotes: 3

Related Questions