Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to change font to bold/italic/plain depends on the row

843806Nov 29 2007 — edited Nov 29 2007
Hi guys,

I found code and read a bunch on the renderer stuff, but I still can't figure out how to change font.
This is what i have so far:
This changes the color depending on if the item on the row has severity A, B, C, D, etc.... How can I change whether to make a row bold dependinds on severity as well?

I have tried adding this adaptee.setFont(boldFont); to all the cases in the the switch statement, but nothing gets switched. Can someone help me?

Thanks,
Monkey2007



package corp.ray.kernel.log.act;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.util.*;

public class TextAreaRenderer extends JTextArea implements TableCellRenderer
{
static final long serialVersionUID = 123123123;
private final DefaultTableCellRenderer adaptee = new DefaultTableCellRenderer();
/** map from table to map of rows to map of column heights */
private final Map cellSizes = new HashMap();


public TextAreaRenderer(boolean inv)
{

setLineWrap(true);
setWrapStyleWord(true);

}

public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected,
boolean hasFocus, int row, int column)
{
// set the colours, etc. using the standard for that platform
adaptee.getTableCellRendererComponent(table, obj,
isSelected, hasFocus, row, column);

setForeground(adaptee.getForeground());
setBackground(adaptee.getBackground());
setBorder(adaptee.getBorder());
setFont(adaptee.getFont());
setText(adaptee.getText());


// This line was very important to get it working with JDK1.4
TableColumnModel columnModel = table.getColumnModel();
setSize(columnModel.getColumn(column).getWidth(), 100000);
int height_wanted = (int) getPreferredSize().getHeight();
addSize(table, row, column, height_wanted);
height_wanted = findTotalMaximumRowSize(table, row);

if (height_wanted != table.getRowHeight(row))
{
if(height_wanted > table.getRowHeight(row))
{
table.setRowHeight(row, height_wanted);
}
}

ArrayList sevs = new ArrayList();
sevs.add("-A-");
sevs.add("-B-");
sevs.add("-C-");
sevs.add("-D-");
sevs.add("-E-");
sevs.add("-F-");
sevs.add("-G-");
sevs.add("-H-");


Color color;
final Font boldFont = adaptee.getFont().deriveFont(Font.BOLD);

for(int i=0; i<table.getRowCount(); i++)
{
try
{
//Integer.parseInt(myTable.getValueAt(i,1).toString())
switch(sevs.indexOf(table.getValueAt(i,(table.getColumnModel().getColumnIndex("Severity"))).toString()))
{
case 5 :
color = Color.black;
adaptee.setFont(boldFont);
break;

case 0 :
color = Color.blue;
break;

case 1 :
color = Color.green.darker().darker();
break;

case 2 :
color = new Color(238,118,0);
break;

case 3 :
color = Color.magenta;
break;

case 4 :
color = Color.red;
break;
case 6 :
color = Color.cyan;
//System.out.println(table.getCellRenderer));
//font = new Font("Serif", Font.BOLD, 20);
//int cur_row = table.getEditingRow();
//System.out.println(cur_row);
//table.setBackground( Color.green );
System.out.println(row + " "+ column);
//table.getCellRenderer(row, column).getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column).setFont(new Font("serif", Font.BOLD, 12));
break;
case 7 :
color = Color.blue.darker().darker();
break;

default :
color = Color.pink;
break;
}


if(row == i)
{
this.setForeground(color);
}
}
catch(NumberFormatException e)
{

}
}

if(isSelected)
{
this.setForeground(Color.white);
}

return this;
}

private void addSize(JTable table, int row, int column, int height)
{
Map rows = (Map) cellSizes.get(table);
if (rows == null)
{
cellSizes.put(table, rows = new HashMap());
}
Map rowheights = (Map) rows.get(new Integer(row));
if (rowheights == null) {
rows.put(new Integer(row), rowheights = new HashMap());
}
rowheights.put(new Integer(column), new Integer(height));
}

/**
* Look through all columns and get the renderer. If it is
* also a TextAreaRenderer, we look at the maximum height in
* its hash table for this row.
*/
private int findTotalMaximumRowSize(JTable table, int row) {
int maximum_height = 0;
Enumeration columns = table.getColumnModel().getColumns();
while (columns.hasMoreElements()) {
TableColumn tc = (TableColumn) columns.nextElement();
TableCellRenderer cellRenderer = tc.getCellRenderer();
if (cellRenderer instanceof TextAreaRenderer) {
TextAreaRenderer tar = (TextAreaRenderer) cellRenderer;
maximum_height = Math.max(maximum_height,
tar.findMaximumRowSize(table, row));
}
}
return maximum_height;
}

private int findMaximumRowSize(JTable table, int row) {
Map rows = (Map) cellSizes.get(table);
if (rows == null) return 0;
Map rowheights = (Map) rows.get(new Integer(row));
if (rowheights == null) return 0;
int maximum_height = 0;
for (Iterator it = rowheights.entrySet().iterator();
it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
int cellHeight = ((Integer) entry.getValue()).intValue();
maximum_height = Math.max(maximum_height, cellHeight);
}
return maximum_height;
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 27 2007
Added on Nov 29 2007
1 comment
292 views