• 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

Centering in JTable

 
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone know to center data in a JTable? I'm using a TableModel but can't seem to figure out how to do this. I know if you add an ImageIcon to a table it is supposedly centered. Should you just try to trick the table into thinking that your string is an ImageIcon?! That seems weird and messy. Surely, there is some simple way, I just can't find it. Also, I'd LIKE to make it so the user can't modify the widths of the columns but haven't figured this one yet either.
With Respect,
Matt DeLacey
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Matt.
Look at getColumnClass method of the AbstractTableModel. Based on the return type - JTable will choose the appropriate renderer. Or a more long winded approach, try to register your own column renderer, look at getTableCellRendererComponent method of TableCellRenderer. Plus, I have seen recommendation to look at Sun's Look and Feel specs and see whether the data type you are trying to center align should be there or maybe left-justified.
Alex
 
Matt DeLacey
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Alex,
Many thanks.
With Respect,
Matt
 
reply
    Bookmark Topic Watch Topic
  • New Topic