I am writing a JTable application using Swing, and the document underlying the table view may contain numbers and text. I want numbers to appear right aligned and text to appear left aligned. I searched the net, with no luck, probably because it's something simple that everyone knows how to do. The columns are not designated as number only, or text only, it's all mixed. Any suggestions would be appreciated.
You seem to know about the javax.swing.table.TableCellRenderer already. What specific question about its use do you have? The Swing tutorial has a section on using them.
My question is that if I write a cell Renderer for numbers that right aligns numbers and another one for text, that left aligns text, how will I let the table know which one to use depending on the type of data that needs to be displayed. I'm not sure if this makes sense, but in the examples on the web, it's usually a column that has the same type of data, and the cell renderer gets added to the column...
That's why the getTableCellRendererComponent method gets passed the row and column indices of the cell to render. Using that, it needs to figure out itself which cell to draw in which way.
Or you can use different cell renderers for different columns. You can set a cell renderer for a specific column like this:
header must be a column identifier - usually the header String you used for the column. With this column you can do several things - setting minimum and/or maximum sizes for instance.
My problem is that a column contains numbers and text. If it contained just numbers, or just one type of data, it would be easier... So I'm trying to implement the public Component getTableCellRendererComponent method; so far I did not get it to work,everything is still left-aligned.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
a bit of art, as a gift, that will fit in a stocking