• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 22791
131
Eclipse IDE Spring Chrome 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic