Win a copy of AWS Security this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Centering in JTable

 
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Hey Alex,
Many thanks.
With Respect,
Matt
 
I will suppress my every urge. But not this shameless plug:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic