Hi All,
I am trying to display some chinese text in one of the three columns in a JTable. The first two columns will contain english text and the third column will contain chinese. The data comes from a server, in XML format.
In the client side I have verified that the data from the server is coming correctly. For this I wrote the data in a flat file and opened it using EditPlus (with MS Hei as default font). The chinese text is appearing correctly.
However, when I am trying to display the same in the jtable, the text is coming as junk. The table cell renderer i have used is as follows:
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.Locale;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
/**
* Class : OperationalMessageTableCellRenderer
* Desc : This class is used to render the cell in the table in the Operation
* messages Edit panel. The main purpose of this class is to render the
* table so that it displays the chinese column in the table with a
* chinese font.
*
* @author Debopam */
public class OperationalMessageTableCellRenderer extends JLabel
implements TableCellRenderer
{
//~ Instance variables =====================================================
/** Chinese Font */
private Font font_Chinese = new Font("MS Hei", Font.PLAIN, 11);
/** English Font */
private Font font_English = new Font("Arial Unicode", Font.PLAIN, 11);
/**
* Creates a new OperationalMessageTableCellRenderer object.
*/
public OperationalMessageTableCellRenderer()
{
}
/** Method : getTableCellRendererComponent
* Desc : Implementation method for the TableCellRenderer interface.
* If the column is 2 (chinese) then the font is changed to chinese.
* @param table JTable
* @param value Object
* @param bIsSelected booleab
* @param bHasFocus boolean
* @param iRow row index
* @param iColumn colun index
* @return Component
*/
public Component getTableCellRendererComponent(JTable table, Object value,
boolean bIsSelected, boolean bHasFocus,
int iRow, int iColumn)
{
if(iColumn != 2)
{
setFont(font_English);
}
else
{
setFont(font_Chinese);
}
this.setText(value.toString());
if(bIsSelected)
{
setForeground(Color.BLUE);
}
else
{
setForeground(Color.BLACK);
}
return this;
}
} //class