• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

How to make first row of JList unselectable like JTable

 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest adding a JLabel as the scroll pane's header view component.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic