Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to make first row of JList unselectable like JTable

 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am using a JList to show a number of data in tabular format. I can't use JTable(that's my first constraint).

Here is the code I have used. Everything is working fine except the first row is selectable yet. Can anyone share an idea how to make first row un-selectable(e.g. like a header in JTable).



Thanks>
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur Sharma wrote:I can't use JTable(that's my first constraint).

Why not?

Can anyone share an idea how to make first row un-selectable(e.g. like a header in JTable).

With JTable, the header is actually a separate component, of type JTableHeader.

If you add your JTable to a simple JPanel, you'll notice that the table header is absent - you have to add it manually. JTable has special support for JScrollPane for adding the header automatically, but JScrollPane is the only one.
 
Brian Cole
Author
Ranch Hand
Posts: 906
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur Sharma wrote:Hi I am using a JList to show a number of data in tabular format.

... Can anyone share an idea how to make first row un-selectable(e.g. like a header in JTable).



It seems like a strange thing to want to do, but try this:You should probably check the ListSelectionModel API to make sure I haven't missed a method.

The other thing you could do is for the first line of your getListCellRendererComponent() method do if (index == 0) isSelected = false;
That wouldn't change if the top element could be selected or not, but it would be drawn as unselected even when selected, which is almost as good. It's not as clean, but it may suit your needs.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest adding a JLabel as the scroll pane's header view component.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic