Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Tools / June 2005

Tip: Looking for answers? Try searching our database.

Rendering A JTable?

Thread view: 
Mkhululi - 09 Jun 2005 16:00 GMT
Hi,

I have two questions in this message:

The questions are both about

JTable

Scenario:
I want to add new information to a database and before that I want to
load everything that is already in the database and store it in a
JTable and then later add the new information after the old one.

Problem:
1. I want the colors of the rows of the old information to be different
from those of the new one. Can you please give guidance on how to code
a renderer for this. I can do and even and odd

color renderer.

2. How can I always add information on the first row of the table
instead of the last one?
I am using vectors in the modelof the table.

Thanks in advance,

Yours,
Me
Szerszu - 09 Jun 2005 18:54 GMT
Hi

sorry for my english

first, create new class

<code>

class ColorRowTable
    extends JTable {

  private int[] colorRows=new int[0];//table with numbers of our
colorful rows
  public Color rowColor=Color.red; //you can set your color
/*this method is always invoked when ui is updated*/
  public Component prepareRenderer(TableCellRenderer renderer, int
rowIndex, int vColIndex) {
    Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
    if (checkRow(rowIndex) && !isCellSelected(rowIndex, vColIndex)) {
      c.setBackground(rowColor);
    }
    else {
      c.setBackground(getBackground());
    }
    return c;
  }
/*compare index of actual row with rows to be painted*/
  private boolean checkRow(int index){
    boolean result=false;
    for(int i=0; i<this.colorRows.length && !result; i++){
      if(this.colorRows[i]==index){
        result = true;
      }
    }
    return result;
  }
/*sets rows to be painted*/
  public void setColorRows(int[] rowNumbers){
    colorRows=new int[rowNumbers.length];
    for(int i=0; i<rowNumbers.length; i++){
      colorRows[i]=rowNumbers[i];
    }
  }
  /*you can add, remove etc numbers of colorful rows*/
}

</code>
for example, in main code

<code>
ColorRowTable mytable=new ColorRowTable(); //
/*...do sth with this table...*/
/*if you have numbers of new rows*/
int[] index={/*for example*/1, 2, 5};
mytable.setColorRows(index);
mytable.rowColor=Color.....
mytable.updateUI();

</code>

sorry, if I missed sth
Signature

Szerszu
szerszu1982CUT_IT_PLEASE@CUT_IT_PLEASEo2.pl



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.