aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes changing color of  one  row dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "changing color of  one  row dynamically" Watch "changing color of  one  row dynamically" New topic
Author

changing color of one row dynamically

wip siva
Ranch Hand

Joined: Nov 03, 2000
Posts: 36
hi friends
i want to change the color of a particular row dynamically.right now i can able to change all the row.how to capture a particular row and change the color dynamically.
plz help me with some example
bye
Julie Stella
Greenhorn

Joined: Jan 12, 2001
Posts: 5
This is an older post...did you get it resolved? If not, you will want to create a custom TableCellRenderer that assigns color to a specific row before painting. Just put your code in getTableCellRendererComponent.
I have examples but not with me today. If you reply to this post, I will post them for you.
If you've already solved your problem, good for you!
Stella
wip siva
Ranch Hand

Joined: Nov 03, 2000
Posts: 36
hi stella thanx a lot.
i can able to change the color of one cell.not the entire row.
badly i need it.i have a demo on monday.plz give me the sample
code .
i just put my code .
and i have one more major bug.whenever the table gets updated by
new data its not repainting properly throwing some nullpointer
exception.
i trying it if u have any idea.plz let me know.
my id is .
123knock@usa.net
thanx in advance.

This is the exception i am getting while updating the table.
Exception occurred during event dispatching:
java.lang.NullPointerException
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java, Comp
iled Code)
at javax.swing.plaf.basic.BasicTableUI.paintRow(BasicTableUI.java, Compi
led Code)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java, Compiled
Code)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java, Compiled Code)
at javax.swing.JComponent.paintComponent(JComponent.java, Compiled Code)
at javax.swing.JComponent.paint(JComponent.java, Compiled Code)
at javax.swing.JComponent.paintChildren(JComponent.java, Compiled Code)
at javax.swing.JComponent.paint(JComponent.java, Compiled Code)
at javax.swing.JViewport.paintViaBackingStore(JViewport.java, Compiled C
ode)
at javax.swing.JViewport.paint(JViewport.java, Compiled Code)
at javax.swing.JComponent.paintWithBuffer(JComponent.java, Compiled Code
)
at javax.swing.JComponent._paintImmediately(JComponent.java, Compiled Co
de)
at javax.swing.JComponent.paintImmediately(JComponent.java, Compiled Cod
e)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java, Com
piled Code)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(System
EventQueueUtilities.java, Compiled Code)
at javax.swing.SystemEventQueueUtilities.processRunnableEvent(SystemEven
tQueueUtilities.java, Compiled Code)
at javax.swing.SystemEventQueueUtilities$RunnableTarget.processEvent(Sys
temEventQueueUtilities.java, Compiled Code)
at java.awt.Component.dispatchEventImpl(Component.java, Compiled Code)
at java.awt.Component.dispatchEvent(Component.java, Compiled Code)
at java.awt.EventQueue.dispatchEvent(EventQueue.java, Compiled Code)
at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java, C
ompiled Code)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:92)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:83)
This is my File
***************
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.LinkedList;
import java.util.Collections;
import java.util.Enumeration;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.ComponentUI.*;
import java.awt.Component.*;
import com.wipro.osssmart.multicast.*;

public class MonitorTablePanel extends OssPanel implements OssEventListener,/*Runnable,*/ActionListener,TableModelListener
{
private boolean test = false;
private OssTable ossTable;
private OssTableModel tableModel;
private DefaultTableModel model;
private DefaultTableColumnModel colModel;
private Vector msgVector,errorVector,appVector;
private thread1 thread;
private thread2 listThread;
private boolean orcondition = false;
private boolean andcondition = false;
private OssPanel btnPanel,lblPanel,tblPanel;
private LinkedList list ;
private JScrollBar scrollBar;
private OssButton btnClear,btnFilter;
private OssLabel lblMonitor;
private OssMainFrame frame;
private Message msg;
private ColoredTableCellRenderer renderer;
// private Object[][] data=null;

//Array of columns and rows
String[] names1 ={"Date", "OssName", "ApplicationName", "ServiceName", "MessageType","Result","MessageText"};
// Object[][] data ={{"","","","","","",""},
//};
Vector data=null;
Vector names=null;
//Constructor for MonitorTablePanel
public MonitorTablePanel(OssMainFrame frame)
{
this.frame=frame;
//Setting BorderLayout for this panel
setLayout(new BorderLayout());
msgVector = new Vector();
errorVector = new Vector();
appVector=new Vector();
data=new Vector();
names=new Vector();
for(int i =0; i <names1.length;i++)>
names.addElement(names1[i]);
System.out.println("vector"+names);
//Instanstiating OssTable
tableModel = new OssTableModel(data,names);
tableModel.addTableModelListener(this);
tableModel.setCellEditable(false);
ossTable = new OssTable(tableModel);
model=(DefaultTableModel)ossTable.getModel();
sizeColumn();
list = new LinkedList();
//Creating a Thread for this panel
thread = new thread1();
listThread = new thread2();
JScrollPane scrollPane = new JScrollPane(ossTable);
scrollBar = scrollPane.getVerticalScrollBar();
add("Center", scrollPane);
btnPanel=new OssPanel();
//setting layout for button panel
btnPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 100,10));
btnClear=new OssButton("Clear","Click to Clear");
btnPanel.add(btnClear);
// add("South", btnPanel);
btnClear.addActionListener(this);
btnFilter=new OssButton("Filter","Click to Filter");
btnPanel.add(btnFilter);
add("South", btnPanel);
btnFilter.addActionListener(this);

lblPanel=new OssPanel();
lblMonitor=new OssLabel("Monitor Application");
lblMonitor.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblMonitor.setFont(new Font("Dialog",Font.BOLD|Font.ITALIC,18));
add("Center",lblMonitor);
lblPanel.setLayout(new BorderLayout());
lblPanel.setBackground(Color.yellow);
lblPanel.add("North",lblMonitor);
add("North",lblPanel);
tblPanel=new OssPanel();
tblPanel.setLayout(new BorderLayout());
tblPanel.add("Center",scrollPane);
renderer = new ColoredTableCellRenderer();
colModel=new DefaultTableColumnModel();
(ossTable.getColumnModel()).getColumn(5).setCellRenderer(renderer);
add("Center",tblPanel);

}//End of COnstructor

/**
* This Method used to Clear all the rows on the Table
* by clicking clear button.
**/

public void actionPerformed(ActionEvent e)
{
try
{
if(e.getSource()==btnClear)
{
model.getDataVector().removeAllElements();
ossTable.updateUI();
}
if(e.getSource()==btnFilter)
{
frame.filterAction(e);
System.out.println("Filter clicked");
}

}catch(Exception ce)
{
ce.printStackTrace();
System.out.println("Clear Table"+ce.getMessage());
}
}

/** This Method to upDate the data on the table
* This will be called when any one the Application is selected from left Panel.
**/
public void updateData(OssEvent event)
{
try
{
if(event.getActionType()== OssEvent.APPLICATION)
{
appVector.removeAllElements();
appVector = event.getData();

}
else if(event.getActionType() == OssEvent.FILTEROR)
{
msgVector = event.getData();
errorVector = event.getErrorVector();
orcondition = true;
andcondition = false;
}
else if (event.getActionType()== OssEvent.FILTERAND)
{
msgVector = event.getData();
errorVector = event.getErrorVector();
andcondition = true;
orcondition = false;
}
else if(event.getActionType()==OssEvent.STOP)
{
orcondition = false;
andcondition = false;
}
//Then it calls the start method
start();
}catch(Exception e)
{
e.printStackTrace();
System.out.println("Exception in updateData:"+e.getMessage());
}
}
/** This method gets the Messages as parameter and adds to the vector
* Then the vector added to the TableMOdel by using addRow method.
**/
public void addRow(Message msg)
{
try
{
Vector vector = new Vector();
vector.addElement((String)msg.applicationName);
vector.addElement((String)msg.ossName);
vector.addElement((String)msg.clusterName);
vector.addElement((String)msg.serviceName);
vector.addElement((String)msg.messageType);
vector.addElement((String)msg.errorType);
vector.addElement((String)msg.messageText);
// if(SwingUtilities.isEventDispatchThread())
model.addRow(vector);
ossTable.updateUI();
//ossTable.validate();
}catch(NullPointerException e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}

/**
* This Method to set the preferedsize of the table column
**/
public void sizeColumn()
{
for(int i=0; i< ossTable.getColumnCount() ;i++)
{
TableColumn objLeftEndColumn = ossTable.getColumnModel().getColumn( i );
objLeftEndColumn.setPreferredWidth(objLeftEndColumn.getWidth() + 50 );
}
}
/** This Method to start the Thread
**/
public void start()
{
test=false;
if(thread.isAlive())
{
try
{
thread.interrupt();
}
catch(Exception e)
{ }
}
if(listThread.isAlive())
{
try
{
listThread.interrupt();
}
catch(Exception e)
{ }
}
if(list.size() != 0)
{
list.clear() ;
}
if(!thread.isAlive())
thread.start();
}
/** This class Start of thread1
* This thread used to receive the data
* from the hub.
**/
private class thread1 extends Thread
{
private Message preMessage = null;
private Message msg1 = null;
public thread1()
{
}
public void run()
{
while(true)
{
Reciever cl = new Reciever();
//Reading data from OssHub and storing it in a String object
msg1= cl.read();
if(preMessage == null )
{
list.addLast(msg1);
preMessage = msg1;
}
else if( msg1.applicationName.trim().compareTo(preMessage.applicationName.trim()) != 0 )
{
list.addLast(msg1);
preMessage = msg1;
}
else
{
System.out.println("Adding thread1" );
}
try
{
this.sleep(400);
}
catch(Exception e)
{ }
if(!test)
{
if(list.size() != 0)
{
if(!listThread.isAlive())
listThread.start();
test = true;
}
}
}
}
}// End of class thread2
/** This class Start of thread2
* This thread used to put the data
* On the table.
**/
private class thread2 extends Thread
{
//private Message msg;
public thread2()
{
msg = new Message();
}
//Overrided run method from Thread class
public void run()
{
try
{
while( true )
{
try
{
msg = (Message)list.removeFirst();
}
catch(Exception e)
{
msg=null;
}
if(msg != null)
{
if ((msg.applicationName).equalsIgnoreCase(("System").trim()))
{
addRow(msg);
//model.fireTableDataChanged();
}
else
{
if(appVector.indexOf((msg.applicationName).trim()) != -1)
{
if (andcondition == true)
{
if(errorVector.indexOf(msg.errorType.trim()) != -1 && msgVector.indexOf(msg.messageText.trim()) != -1 )
{
addRow(msg);
//model.fireTableDataChanged();
}
else if(errorVector.size() == 0 && msgVector.indexOf(msg.messageText.trim()) != -1 )
{
addRow(msg);
//model.fireTableDataChanged();
}
else if(errorVector.indexOf(msg.errorType.trim()) != -1 && msgVector.size() == 0)
{
addRow(msg);
//model.fireTableDataChanged();
}
}
else if (orcondition == true)
{
if(msgVector.indexOf(msg.messageText.trim()) != -1 )
{
addRow(msg);
//model.fireTableDataChanged();
}
else if(errorVector.indexOf(msg.errorType.trim()) != -1)
{
addRow(msg);
//model.fireTableDataChanged();
}
}
else
{
addRow(msg);
//model.fireTableDataChanged();
}
}
}
}
scrollBar.setValue(model.getRowCount()* ossTable.getRowHeight()) ;
try
{
this.sleep(100);
}
catch(Exception e)
{ }
}
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Exception in Thread"+e.getMessage());
}
}
}// End of class thread2
//scrollBar.updateUI();
public void tableChanged(TableModelEvent e)
{
try
{
if((msg.errorType).equalsIgnoreCase(("Error Type2").trim()))
{
//ossTable.setForeground(Color.red);
}
else
{
ossTable.setForeground(Color.blue);
}
}catch(Exception a)
{
//a.printStackTrace();
}
}
class ColoredTableCellRenderer extends DefaultTableCellRenderer
{
public void setValue(Object value)
{
setText((String)value);
if(((String)value).equals("Error Type2"))
{

setForeground(Color.red);
}
else
{
setForeground(Color.blue);
}
}
}
}//End of MonitorTablePanel class
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: changing color of one row dynamically
 
Similar Threads
dynmicall adding row
To set color for rows in JTable
Quetion about how to change the color of a row in a JTable
XSLT and HTML
How to create border or change color in a JTable cell?