JTable Cell Font? (Java) - java

The program I am creating should work like Microsoft Excel, except in JAVA. It should also support cell formatting (Which is my problem). I have the code for detecting which cell is clicked, and what font to use working properly - I just can not figure out how to apply the Font to the cell! Google gave me CellRenderers, but it seems that cell renderers format the cell only when a condition is true. I want it to format with the specified Font it when it is called!
Can someone please help me, I am really confused!!!
I have already looked at the Java Tutorials.
My apologies if this question has been asked before!

this is what you are looking for,, this code snippet changes the font of all columns in a jTable..
I'm sure a slight modification should get your scenario covered.
for (int i = 0; i < jTable1.getColumnCount(); i ++) {
TableColumn col = jTable1.getColumnModel().getColumn(i);
col.setCellEditor(new MyTableCellEditor());
}
public class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {
JComponent component = new JTextField();
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int vColIndex) {
((JTextField)component).setText((String)value);
((JTextField)component).setFont(new java.awt.Font("Arial Unicode MS", 0, 12));
return component;
}
}

This will change the font for all cells in the table - even when new columns or rows are added:
JTable table;
......
Object dce = table.getDefaultEditor(Object.class);
if(dce instanceof DefaultCellEditor) {
((DefaultCellEditor) dce).getComponent().setFont([your font]);
}

Related

How to change a cell in a jTable programmatically?

I'm currently writing an application where I present to the user amongst other things links to websites in a JTable. I already set up my JTable correctly to open up the corresponding website upon clicking the regarding cell. However I struggle with correctly formatting the cell so that users know they actually have the possibility of clicking the cell for instantly opening the website.
Hence what I want to achieve is to have the colour of the link being blue at least and even better also underlined. I searched through different articles on SO regarding this but couldn't quite grasp how the things explained there work together - despite I'm not entirely sure if these things would have even be what I'm actually looking for.
The way I fill my table is the following:
String[][] rowData = new String[entries.size() + 1][entries.get(0).length + 1];
rowData[0] = columnNames;
int i = 1;
Iterator<String[]> iterator = entries.iterator();
while (iterator.hasNext()) {
rowData[i] = iterator.next();
i++;
}
tblEntries = new JTable(rowData, columnNames);
entries in this case is an ArrayList that is passed over by the database handler and - as the name suggests - contains all entries for the table. After reading the ArrayList into the respective Array I initialize the table as seen in the last row. Now all the links are actually stored in all rows > 0 and the 4th column.
My first approach was doing something like this:
for (int j = 0; j < entries.size(); j++) {
for (int j2 = 0; j2 < entries.get(0).length; j2++) {
tblEntries.editCellAt(row, column, e);
}
}
where e should be an event that checks wheter the conditions for a link are satisfied or not and execute the formatting accordingly. However I don't really now what kind of event is needed to pass it to the function.
An other approach I saw in a different SO article was to use the prepareRenderer method to specify the conditions for rendering the content correctly. However apparently this seems to be only possible for own implementations of a JTable which I'd like to avoid as tblEntries.prepareRenderer() and applying a new TableCellRenderer or DefaultTableCellRenderer doesn't give me the function that I need to override according to above mentioned SO article.
So, what would be the best and most convenient way to tackle this problem down? Thanks in advance for your any adivce and help.
SOLUTION:
For anyone facing a similar problem I'll put my solution here. As suggested by #camickr the best solution is a custom DefaultTreeCellRenderer the problem in this specific scenario however is that it will also render the specific table-header (which obviously doesn't contain any links) in the link format. Hence I searched a bit further and found this website where I found a working code for customising where the renderer should be applied.
In the end I came up with this code:
String[][] rowData = new String[entries.size() + 1][entries.get(0).length + 1];
rowData[0] = columnNames;
int i = 1;
Iterator<String[]> iterator = entries.iterator();
while (iterator.hasNext()) {
rowData[i] = iterator.next();
i++;
}
tblEntries = new JTable(rowData, columnNames) {
#Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if (row > 0 && column == 4) {
c = super.prepareRenderer(new LinkRenderer(), row, column);
}
return c;
}
};
For reference for the LinkRenderer see accepted answer below.
what I want to achieve is to have the colour of the link being blue at least and even better also underlined.
This is controlled by the renderer. The default renderer for the JTable is a JLabel.
You can easily create a custom renderer to display the text in blue:
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setForeground( Color.BLUE );
table.getColumnModel().getColumn(3).setCellRenderer( renderer );
Unfortunately underlining the text will be more difficult. Underlining text in a component can be achieved by setting a property of the Font which is easy enough to do for a JLabel:
JLabel label = new JLabel("Underlined label");
Font font = label.getFont();
Map<TextAttribute, Object> map = new HashMap<TextAttribute, Object>();
map.put(TextAttribute.FONT, font);
map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
font = Font.getFont(map);
label.setFont(font);
However, you can't just set the Font for the renderer because when each cell is rendered the default renderer will reset the Font to be the Font used by the table.
So if you want to implement a custom renderer with a custom Font, you need to extend the DefaultTableCellRenderer and override the getTableCellRendererComponent(….) method. The code might be something like:
class LinkRenderer extends DefaultTableCellRenderer
{
private Font underlineFont;
public LinkRenderer()
{
super();
setForeground( Color.BLUE );
underlineFont = .getFont();
Map<TextAttribute, Object> map = new HashMap<TextAttribute, Object>();
map.put(TextAttribute.FONT, underlineFont);
map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
underLinefont = Font.getFont(map);
}
#Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setFont( underlineFont );
return this;
}
}
Read the section from the Swing tutorial on Renderers and Editors for more information.
So the other approach is to NOT use a custom renderer but instead you can add HTML to the table model. A JLabel can display simple HTML.
So the text you add to the model would be something like:
String text = "<html><u><font color=blue>the link goes here</font></ul></html>";

Hiding an individual cell in JTable depending on model value

I have a JTable showing values from a model allowing integer values 0, 1 and 2. The values are shown in a 9 by 9 grid (like a sudoku game board).
My question is: How do I hide the zero values from the GUI?
That is, in the table cells that has a model value of zero, I dont want the GUI to show any value. The model must however contain the zero value due to calculations being done.
I have tried to use a DefaultTableCellRenderer and setting the cell component to invisible by using setVisible(true) (as in this question), but I get no result.
I have managed to use a DefaultTableCellRenderer to toggle any cell's background color depending on the model state (if a cell is considered "negative"). I do want to keep the background color visible.
I am a bit new to java, so maybe this is just an update issue?
Here is my DefaultTableCellRenderer:
private static class GameTableRenderer extends DefaultTableCellRenderer
{
private static final long serialVersionUID = 1L;
#Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
Component c = super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
GameModel model = (GameModel) table.getModel();
if(model.isNegative(row, column))
c.setBackground(Color.lightGray);
else
c.setBackground(Color.white);
if(model.getAbsoluteValueAt(row, column) == 0)
c.setVisible(false);
else
c.setVisible(true);
return c;
}
}
The "getAbsoluteValueAt" method returns an integer 0, 1 or 2. The change of background color works perfectly fine.
My model extends AbstractTableModel, and the JTable uses the renderer like this:
table.setDefaultRenderer(int.class, renderer);
I hope this information is enough to explain my problem and the efforts I so far have put in. Please ask for more information and I will provide it.
Thank you in advance. /Fredrik
Or you can use the following:
if(model.getAbsoluteValueAt(row, column) == 0)
c.setForeground(c.getBackground());
else
c.setForeground(Color.BLACK);
Instead of making the component invisible, set its text to the empty string:
if (model.getAbsoluteValueAt(row, column) == 0) {
((JLabel) c).setText("");
}

Disabling cell outline in JTable

I'm using a JTable to show some data. The user can only select entire rows of the JTable, not individual cells. Here's the code used to allow only rows selection:
jTable1.setCellSelectionEnabled(false);
jTable1.setColumnSelectionEnabled(false);
jTable1.setRowSelectionAllowed(true);
jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
But when a user selects a row the cell in that row gets outlined (first column / last row in the image below):
How Can I disable this outline?
You could simply extend the DefaultTableCellRenderer and pretend, from the UI's side, that the cell isn't "focused".
I removed the border by using the following renderer:
private static class BorderLessTableCellRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
public Component getTableCellRendererComponent(
final JTable table,
final Object value,
final boolean isSelected,
final boolean hasFocus,
final int row,
final int col) {
final boolean showFocusedCellBorder = false; // change this to see the behavior change
final Component c = super.getTableCellRendererComponent(
table,
value,
isSelected,
showFocusedCellBorder && hasFocus, // shall obviously always evaluate to false in this example
row,
col
);
return c;
}
}
You can set it on your JTable like this:
table.setDefaultRenderer( Object.class, new BorderLessTableCellRenderer() );
or, for Strings:
table.setDefaultRenderer( String.class, new BorderLessTableCellRenderer() );
It's a bit of an hack in that it's simply reusing the original renderer and pretending that the focused/selected cell isn't but it should get you started.
I just found a very simple trick to alter this behaviour. It turns out that cell focusing is an extension of the tables focus. If the table can not be focused on neither can individual cells (though row selections still show up).
All you need is one simple line of code:
jTable1.setFocusable(false);
Here is a picture from a project of mine that implements this same behaviour (Note: I am using windows LookAndFeel)
Regardless of which column you click under the entire row is selected but the cell you clicked is not focused. I hope it helps! :)

JTable trouble changing colors for first column

i try to change the color of fields in a JTable according to their value. I don't want to change any color of the first column but it changes anyway in a buggy way(some fileds are not correctly filed like University and Possible_Reviewer):
My code is as following:
table.setDefaultRenderer(Object.class, new CustomRenderer());
private class CustomRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,boolean hasFocus, int row, int col){
Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
try {
Double val = Double.parseDouble(value.toString());
if(col == 0){
comp.setBackground(Color.white);
} else {
comp.setBackground(changeColor(val));
}
} catch (NumberFormatException e){}
return( comp );
}
private Color changeColor(Double val) {
//returns a Color between red and green depending on val
}
}
The weird thing is that when i use "col == 2" it turns the second column white but the first remains strangely colored.
Anyone an idea?
You should extend JTable class and override this method:
public TableCellRenderer getCellRenderer(int row, int column){}
Otherwise JTable will use the same renderer for each cell in the same column.
EDIT:
Like #Mark Bramnik pointed out, it's better to not instantiate a new TableCellRenderer object for every getCellRenderer call. You could implement a method like the following:
setCellRenderer(int row, int col, TableCellRenderer render)
and store the renderer in the extended JTable itself.
How to Use Tables: Using Custom Renderers mentions this alternative approach: "To specify that cells in a particular column should use a renderer, you use the TableColumn method setCellRenderer()."
Addendum: A benefit of this approach is that the renderer "sticks" to the column if the user drags it to a different position. In this example, replace setDefaultRenderer() with setCellRenderer().
table.getColumnModel().getColumn(DATE_COL).setCellRenderer(new DateRenderer());

JTable with multi-line cell renderer prints very weird

I have a JTable with a custom Cell Renderer for multi-line cells. Everything is ok, the JTable is painted ok in the screen and I am very happy with it, but ast night when I tried to simply print it, I came up with a very strange issue. Using:
table.print(PrintMode.FIT_WIDTH, new MessageFormat("..."), new MessageFormat("..."));
I saw that the table did not print entirely. Then using another class made from a colleague for printing JTables I had the same result:
The table (with multi-line cells) needed 22 pages to print. The printed document (which I only viewed in xps format since I do not own a printer) had also 22 pages. But up to page 16 everything was printed as expected and after that only the borders and the column headers of the table were printed.
Strangely (to me) enough, when I tried to print the table using another cell renderer that does not allow for multi line cells, the table needed exactly 16 pages and was printed entirely, albeit the cropping in the lengthy cell values.
I searched all over the net but I had no luck. Does anybody know why could this be happening? Is there a solution?
Update:
My cell renderer is the following:
public class MultiLineTableCellRenderer extends JTextPane implements TableCellRenderer {
private List<List<Integer>> rowColHeight = new ArrayList<List<Integer>>();
public MultiLineTableCellRenderer() {
setOpaque(true);
}
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
String s = (String)value;
if (s.equals("<περιοδάριθμος>")) {
setForeground(Color.blue);
}
else if(s.equals("<παραγραφάριθμος>")) {
setForeground(Color.red);
}
else {
setForeground(Color.black);
}
setBackground(new Color(224, 255, 255));
if (isSelected) {
setBackground(Color.GREEN);
}
setFont(table.getFont());
setFont(new Font("Tahoma", Font.PLAIN, 10));
if (hasFocus) {
setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
if (table.isCellEditable(row, column)) {
setForeground(UIManager.getColor("Table.focusCellForeground"));
setBackground(UIManager.getColor("Table.focusCellBackground"));
}
} else {
setBorder(new EmptyBorder(1, 2, 1, 2));
}
if (value != null) {
setText(value.toString());
} else {
setText("");
}
adjustRowHeight(table, row, column);
SimpleAttributeSet bSet = new SimpleAttributeSet();
StyleConstants.setAlignment(bSet, StyleConstants.ALIGN_CENTER);
StyleConstants.setFontFamily(bSet, "Tahoma");
StyleConstants.setFontSize(bSet, 11);
StyledDocument doc = getStyledDocument();
doc.setParagraphAttributes(0, 100, bSet, true);
return this;
}
private void adjustRowHeight(JTable table, int row, int column) {
int cWidth = table.getTableHeader().getColumnModel().getColumn(column).getWidth();
setSize(new Dimension(cWidth, 1000));
int prefH = getPreferredSize().height;
while (rowColHeight.size() <= row) {
rowColHeight.add(new ArrayList<Integer>(column));
}
List<Integer> colHeights = rowColHeight.get(row);
while (colHeights.size() <= column) {
colHeights.add(0);
}
colHeights.set(column, prefH);
int maxH = prefH;
for (Integer colHeight : colHeights) {
if (colHeight > maxH) {
maxH = colHeight;
}
}
if (table.getRowHeight(row) != maxH) {
table.setRowHeight(row, maxH);
}
}
}
Furthermore, if you test the following very simple example you will notice that something is terribly wrong with the printing, but I really can't find what!
public static void main(String[] args) throws PrinterException {
DefaultTableModel model = new DefaultTableModel();
model.addColumn("col1");
model.addColumn("col2");
model.addColumn("col3");
int i = 0;
for (i = 1; i <= 400; i++) {
String a = "" + i;
model.addRow(new Object[]{a, "2", "3"});
}
JTable tab = new JTable(model);
tab.print();
}
I believe you are having the same problem that I had when I asked this question:
Truncated JTable print output
I found a solution to my problem, and I believe it may help you as well.
The answer is here:
Truncated JTable print output
To summarize my answer:
If your TableCellRenderer is the only place in your code where you are setting rows to their correct height, then you are going to run into trouble caused by an optimization inside JTable: JTable only invokes TableCellRenderers for cells that have been (or are about to be) displayed.
If not all of your cells have been displayed on-screen, then not all of your renderers have been invoked, and so not all of your rows have been set to the desired height. With your rows not being their correct height, your JTable overall height is incorrect. After all, part of determining the overall JTable height is accounting for the height of each of that table's rows. If the JTable overall height isn't correct, this causes the print to truncate, since the JTable overall height is a parameter that is considered in the print layout logic.
An easy (but perhaps not squeaky clean) way to fix this is to visit all of your cell renderers manually before printing. See my linked answer for an example of doing this. I actually chose to do the renderer visitation immediately after populating my table with data, because this fixes some buggy behavior with the JTable's scrollbar extents (in addition to fixing the printing.)
The reason the table looks and works OK on-screen even when printing is broken, is because as you scroll around in the table, the various renderers are invoked as new cells come on screen, and the renderers set the appropriate row height for the newly visible rows, and various dimensions are then are recalculated on the fly, and everything works out OK in the end as you interact with the table. (Although you may notice that the scrollbar "extent" changes as you scroll around, which it really shouldn't normally do.)
Strange thing is that behavior is not deterministic.
Such behavior always makes me suspect incorrect synchronization.
It's not clear how your TableCellRenderer works, but you might try HTML, which is supported in many Swing components.
Another useful exercise is to prepare an sscce that reproduces the problem in minature. A small, complete example might expose the problem. It would also allow others to test your approach on different platforms.
This answer is probably too late for the one who asked this question, but for everybody with a similar problem, here is my solution;
I had exactly the same problem, I have my own TableCellRenderer to handle multi-line Strings which works flawless for showing the table but makes the printing of the table unreliable.
My solutions consists of 2 parts;
Part 1: I have created my own TableModel, in the getValueAt() I 'copied' a part of the StringCellRenderer logic, I make it recalculate and set the height of the table row in case af a multi-line String AND return the String as HTML with 'breaks' instead of line-separators.
Part 2: Before invoking the table.print() I call the getValueAt() for all cells (a for-loop over the columns with an inner for loop over the rows invoking the getValueAt()), this has to be done 'manually' because the print functionality doesn't invoke all getValueAt's (I have found reasons on different fora regarding this issue regarding the execution of the TableCellRenderers).
This way the clipping of the table is done like it is supposed to, only complete rows are printed per page and it devides the rows over severall pages if required with a table header at each page.

Categories

Resources