posted 20 years ago
I changed a lot from the previous classes.
I will paste the classes and No Database and Package. The problem is while clicking on any cell the Sorce and Description column is still in white others are default(some violet) color.I want to make sorce and description also into default.
The Interesting thing is that i am printing column without any condition on renderer class but only 0,3,4 is coming others are not printed.This 0,3,4 are String other Objects 1 int,2 Date and 5 also int.I don't know what is happening.
CustomTableCellRenderer.java
import java.awt.Component;
import java.awt.Color;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableCellEditor;
public class CustomTableCellRenderer extends DefaultTableCellRenderer {
public CustomTableCellRenderer(){
}
public Component getTableCellRendererComponent
(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent
(table, value, isSelected, hasFocus, row, column);
String strValue=value.toString().trim();
//This else if is for making the whole row in differnt color
System.out.println("Column = "+column);
if(strValue.equals("Major") && column==0){
cell.setBackground(Color.ORANGE);
}else if(strValue.equals("Minor") && column==0){
cell.setBackground(Color.YELLOW);
}else if(strValue.equals("Critical") && column==0){
cell.setBackground(Color.RED);
}else if(strValue.equals("Information") && column==0){
cell.setBackground(Color.GREEN);
}else if(strValue.equals("Warning") && column==0){
cell.setBackground(Color.CYAN);
}else {
cell.setBackground(Color.WHITE);
}
//This is for making only
/**
if(strValue.equals("Major") && column==0){
cell.setBackground(Color.CYAN);
}else if(column>0){
cell.setBackground(Color.LIGHT_GRAY);
}else if(strValue.equals("Minor") && column==0){
cell.setBackground(Color.GREEN);
}else if(column>0){
cell.setBackground(Color.LIGHT_GRAY);
}else if(strValue.equals("Critical") && column==0){
cell.setBackground(Color.RED);
}else if(column>0){
cell.setBackground(Color.LIGHT_GRAY);
}else if(strValue.equals("Information") && column==0){
cell.setBackground(Color.YELLOW);
}else if(column>0){
cell.setBackground(Color.LIGHT_GRAY);
}else if(strValue.equals("Warning") && column==0){
cell.setBackground(Color.BLUE);
}else if(column>0){
cell.setBackground(Color.LIGHT_GRAY);
}
*/
/**if( value instanceof Integer ){
Integer amount = (Integer) value;
if( amount.intValue() < 7 ){
cell.setBackground( Color.red );
// You can also customize the Font and Foreground this way
// cell.setForeground();
// cell.setFont();
}else{
cell.setBackground( Color.blue );
}
}
*/
return cell;
}//End of method getTableCellRendererComponent
}//End of Class
FaultTableModel.java
import javax.swing.JLabel;
import javax.swing.table.AbstractTableModel;
import javax.swing.event.TableModelEvent;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.awt.event.*;
import java.util.Vector;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.Date;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
class FaultTableModel extends AbstractTableModel {
static final public ColumnData columnNames[] = {
new ColumnData( "Severity", JLabel.LEFT ),
new ColumnData( "Fault ID", JLabel.LEFT ),
new ColumnData( "Date Time", JLabel.LEFT ),
new ColumnData( "Source", JLabel.LEFT ),
new ColumnData( "Description", JLabel.LEFT ),
new ColumnData( "Count", JLabel.LEFT )
};
protected Vector vectData;
protected int m_sortCol = 0;
protected boolean m_sortAsc = true;
public FaultTableModel() {
vectData = new Vector();
setFaultData();
}
public void setFaultData() {
//vectData.removeAllElements();
vectData.addElement(new FaultData("Major",1000,new Date(),"S23","desc4",254));
vectData.addElement(new FaultData("Minor",1001,new Date(),"S32","desc2",300));
vectData.addElement(new FaultData("Critical",1002,new Date(),"S33","desc1",198));
vectData.addElement(new FaultData("Warning",1003,new Date(),"S355","desc5",467));
vectData.addElement(new FaultData("Information",1004,new Date(),"3S3","desc3",598));
Collections.sort(vectData, new
StockComparator(m_sortCol, m_sortAsc));
}
public int getRowCount() {
return vectData==null ? 0 : vectData.size();
}
public int getColumnCount() {
return columnNames.length;
}
public String getColumnName(int column) {
String str = columnNames[column].m_title;
//if (column==m_sortCol)
//str += m_sortAsc ? " ďż˝" : " ďż˝";
return str;
}
public Object getValueAt(int nRow, int nCol) {
if (nRow < 0 || nRow>=getRowCount())
return "";
FaultData row = (FaultData)vectData.elementAt(nRow);
switch (nCol) {
case 0: return new String(row.gstatus);
case 1: return new Integer(row.gfaultID);
case 2: return row.gdate;
case 3: return new String(row.gsource);
case 4: return new String(row.gdesc);
case 5: return new Integer(row.gcount);
}
return "";
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
//System.out.println("Error = "+getValueAt(0, c).getClass());
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
/*if (col < 1) {
return false;
} else {
return true;
}*/
return false;
}
/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
//vectData[row][col] = value;
fireTableCellUpdated(row, col);
}
public void tableChanged(TableModelEvent e) {
fireTableChanged(e);
}
public void set(){
/*int intStatus=20;
Vector vectData=new Vector();
for(int i=30;i<41;i++){
HashMap hmValues=new HashMap();
hmValues.put("STATUS",intStatus+"");
hmValues.put("FAULTID",new Integer(i+100)+"");
hmValues.put("DATE",new Date()+"");
hmValues.put("SOURCE","Source "+i);
hmValues.put("DESC","Description "+i);
hmValues.put("COUNT",new Integer(i+200)+"");
hmValues.put("ASSIGNEDTO","Assigned To "+i);
intStatus++;
vectData.add(hmValues);
}
CreateColumns();
CreateData(vectData);
//fireTableDataChanged();
//fireTableStructureChanged();
*/
}
class ColumnListener extends MouseAdapter
{
protected JTable m_table;
public ColumnListener(JTable table) {
m_table = table;
}
public void mouseClicked(MouseEvent e) {
TableColumnModel colModel = m_table.getColumnModel();
int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex();
if (modelIndex < 0)
return;
if (m_sortCol==modelIndex)
m_sortAsc = !m_sortAsc;
else
m_sortCol = modelIndex;
for (int i=0; i < columnNames.length; i++) {
TableColumn column = colModel.getColumn(i);
column.setHeaderValue(getColumnName(column.getModelIndex()));
}
m_table.getTableHeader().repaint();
Collections.sort(vectData, new
StockComparator(modelIndex, m_sortAsc));
m_table.tableChanged(
new TableModelEvent(FaultTableModel.this));
m_table.repaint();
}
}
}
class FaultData
{
public String gstatus;
public int gfaultID;
public Date gdate;
public String gsource;
public String gdesc;
public int gcount;
public FaultData(String status,int faultID,Date date,String source,String desc,int count) {
gstatus = status;
gfaultID = faultID;
gdate = date;
gsource = source;
gdesc = desc;
gcount = count;
}
}
class ColumnData
{
public String m_title;
// public int m_width;
public int m_alignment;
public ColumnData(String title, int alignment) {
m_title = title;
//m_width = width;
m_alignment = alignment;
}
}
class StockComparator implements Comparator
{
protected int m_sortCol;
protected boolean m_sortAsc;
public StockComparator(int sortCol, boolean sortAsc) {
m_sortCol = sortCol;
m_sortAsc = sortAsc;
}
public int compare(Object o1, Object o2) {
if(!(o1 instanceof FaultData) || !(o2 instanceof FaultData)){
return 0;
}
FaultData s1 = (FaultData)o1;
FaultData s2 = (FaultData)o2;
int result = 0;
switch (m_sortCol) {
case 0: // status
result = s1.gstatus.compareTo(s2.gstatus);
break;
case 1: // fault Id
int f1=s1.gfaultID;
int f2=s2.gfaultID;
if (f1<f2)
result=-1;
else
result=1;
break;
case 2: // date
//result = s1.gstatus.compareTo(s2.gstatus);
break;
case 3: // source
result = s1.gsource.compareTo(s2.gsource);
break;
case 4: // desc
result = s1.gdesc.compareTo(s2.gdesc);
break;
case 5: // count
int c1=s1.gcount;
int c2=s2.gcount;
if (c1<c2)
result=-1;
else
result=1;
break;
}
//System.out.println("Hos"+m_sortCol);
if (!m_sortAsc)
result = -result;
return result;
}
public boolean equals(Object obj) {
System.out.println(obj);
if (obj instanceof StockComparator) {
StockComparator compObj = (StockComparator)obj;
return (compObj.m_sortCol==m_sortCol) &&
(compObj.m_sortAsc==m_sortAsc);
}
return false;
}
}
FaultMgmt.java
//import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
//import javax.swing.table.DefaultTableCellRenderer;
//import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
//import javax.swing.event.TableModelListener;
//import javax.swing.event.TableModelEvent;
//import javax.swing.event.ListSelectionListener;
//import javax.swing.event.ListSelectionEvent;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
//import java.awt.Component;
//import java.awt.Dimension;
//import java.awt.GridLayout;
//import java.sql.ResultSet;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.Date;
/**
* FaultMgmt uses a custom TableModel.
*/
public class FaultMgmt extends JPanel implements ActionListener{
private boolean DEBUG = true;
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
JTable table;
FaultTableModel modal;
public void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JButton button;
JTextField txtTotal=new JTextField(5);
JLabel lblDisplaying=new JLabel("Displaying");
JTextField txtFrom=new JTextField(5);
JLabel lblTo=new JLabel("To");
JTextField txtTo=new JTextField(5);
JLabel lblPgLength=new JLabel("Page Length");
JComboBox cmbPaging=new JComboBox();
cmbPaging.addItem("25");
cmbPaging.addItem("50");
cmbPaging.addItem("75");
cmbPaging.addItem("100");
cmbPaging.addActionListener(this);
TableCellRenderer renderer = new CustomTableCellRenderer();
modal=new FaultTableModel();
table = new JTable(modal);
//table = new JTable(new FaultTableModel());
JTableHeader head =table.getTableHeader();
//head.setForeground( Color.BLUE);
head.setReorderingAllowed(false);//Cannot Rearrange the headers
//head.setResizingAllowed(false);//Cannot Resize Headers
//head.addMouseListener(modal.new ColumnListener(table));
//Setting the width of a particular Column
TableColumn colDesc = table.getColumnModel().getColumn(4);
colDesc.setMinWidth(50);
colDesc.setMaxWidth(250);
colDesc.setPreferredWidth(200);
//table.setShowGrid(false);//No Border
table.setRowHeight(30);
table.setOpaque(true);
//table.setSelectionBackground(Color.DARK_GRAY);
//table.setCellSelectionEnabled(false);
//table.setSelectionForeground(Color.RED);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
JTable table =(JTable) me.getSource();
Point p = me.getPoint();
int row = table.rowAtPoint(p);
int col = table.columnAtPoint(p);
if(me.getButton()==3){
//System.out.println("PRESSED BUTTON = "+me.getButton());
}
TableModel tableModel = (TableModel)table.getModel();
Object strValue = tableModel.getValueAt(row,0);
HashMap hmValues=new HashMap();
hmValues.put("FAULTID",tableModel.getValueAt(row,0));
hmValues.put("DATE",tableModel.getValueAt(row,1));
hmValues.put("SOURCE",tableModel.getValueAt(row,2));
hmValues.put("DESC",tableModel.getValueAt(row,3));
hmValues.put("COUNT",tableModel.getValueAt(row,4));
hmValues.put("ASSIGNEDTO",tableModel.getValueAt(row,5));
if (me.getClickCount() == 2) {
//System.out.println("Value is = "+strValue);
FaultDetails faultDetails=new FaultDetails(strValue.toString());
//FaultDetails faultDetails=new FaultDetails(hmValues);
//faultDetails.createAndShowGUI();
}
}
});
ListSelectionModel selectionModel = table.getSelectionModel();
//selectionModel.addListSelectionListener( this );
try{
//table.setDefaultRenderer( Class.forName( "java.lang.Integer" ), renderer );
table.setDefaultRenderer(Object.class, renderer );
}catch(Exception ee){
System.out.println(ee);
}
table.setPreferredScrollableViewportSize(new Dimension(500, 450));
//table.setPreferredScrollableViewportSize(new Dimension(400, 300));
//table.setForeground(Color.RED);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//System.out.println(table.getValueAt(0,5));
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(0,5,0,5);
pane.add(txtTotal, c);
c.gridx = 1;
c.gridy = 0;
pane.add(lblDisplaying, c);
c.gridx = 2;
c.gridy = 0;
pane.add(txtFrom, c);
c.gridx = 3;
c.gridy = 0;
pane.add(lblTo, c);
c.gridx = 5;
c.gridy = 0;
pane.add(txtTo, c);
c.gridx = 6;
c.gridy = 0;
pane.add(lblPgLength, c);
c.gridx = 7;
c.gridy = 0;
pane.add(cmbPaging, c);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 8;
c.insets = new Insets(10,0,10,0); //Top 10 and Bottom 10 padding
pane.add(scrollPane, c);
/*button = new JButton("Long-Named Button ddddddddd 4");
c.ipady = 20; //make this component tall
c.weighty = 0.0;
c.gridwidth = 1;
c.insets = new Insets(0,50,0,60); //left 50 and right 60 padding
c.gridx = 1;
c.gridy = 1;
c.fill = GridBagConstraints.NONE; //Check Commenting This Line.
pane.add(button, c);
button = new JButton("5");
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,20,0); //top 10 and botton 20 padding
c.gridx = 1; //aligned with button 2
c.gridwidth = 2; //2 columns wide
c.gridy = 2; //third row
pane.add(button, c);
*/
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("NMS Fault Management Systemsss");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
//addComponentsToPane(frame.getContentPane());
frame.setResizable(false);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae){
//Color c=new Color(255);
//System.out.println("Red= "+c.getRed()+" Green = "+c.getGreen()+" Blue = "+c.getBlue());
modal.set();
}
public static void main(String[] args) {
/*//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
*/
FaultMgmt fm = new FaultMgmt();
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("NMS Fault Management Systems");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
fm.addComponentsToPane(frame.getContentPane());
frame.setResizable(false);
//Display the window.
frame.pack();
frame.setVisible(true);
}
}
Thanks.