codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Hiii!!!Problem with JTable Coloring  haripriya amaresan at 08:52 on Wednesday, November 01, 2006
 

Hii,

I have a problem in JTable.I have a Jtable and a button named validate.My JTable first displays a set of rows and colums.if we name the colns as a,b and c. when i click the validate button i will take the values of a1 and b1 and do some validations. If it is in correct position then the row remains as it is else i will change the value of b1 to a1 position and vice versa. This happens in a loop till the end and the data vector of the table gets changed accordingly.Now the problem is suppose if there are 10 rows and the value changes in row 1,5,7 thn i need that rows alone to be colored while the button ends its action.Finally the JTable should be viewed as row 1,5,7 colored and others in their default color....

Please help me...

Thanks.

  Re: Hiii!!!Problem with JTable Coloring  crwood at 22:41 on Saturday, November 04, 2006
 


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

public class Highlight {
int[] rows = new int[0];

JTable table = new JTable() {
public Component prepareRenderer(TableCellRenderer renderer,
int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if(isChanged(row))
c.setBackground(Color.yellow);
else if(isCellSelected(row, column))
c.setBackground(getSelectionBackground());
else
c.setBackground(getBackground());
return c;
}
};

private boolean isChanged(int row) {
for(int j = 0; j < rows.length; j++)
if(rows[j] == row)
return true;
return false;
}

public static void main(String[] args) {
Highlight test = new Highlight();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test.getTable());
f.getContentPane().add(test.getLastPanel(), "Last");
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}

private JScrollPane getTable() {
int rows = 10, cols = 2;
String[] colIds = new String[2];
Object[][] data = new Object[rows][cols];
for(int j = 0; j < rows; j++)
for(int k = 0; k < cols; k++)
data[j][k] = "item " + (j*cols + k+1);
DefaultTableModel model = new DefaultTableModel(data, colIds);
table.setModel(model);
return new JScrollPane(table);
}

private JPanel getLastPanel() {
JButton validate = new JButton("validate");
validate.addActionListener(new ActionListener() {
Random seed = new Random();

public void actionPerformed(ActionEvent e) {
// simulate some rows whose values were swapped
int rowCount = table.getRowCount();
int numRows = 1 + seed.nextInt(3);
// initialize elements to value outside domain
rows = new int[numRows];
for(int j = 0; j < rows.length; j++)
rows[j] = -1;
// select some random rows
int count = 0;
while(count < numRows) {
int row = seed.nextInt(rowCount);
if(notFoundIn(rows, row)) // no repetition
rows[count++] = row;
}
table.repaint();
}
});
JPanel panel = new JPanel();
panel.add(validate);
return panel;
}

private boolean notFoundIn(int[] array, int n) {
for(int j = 0; j < array.length; j++)
if(array[j] == n)
return false;
return true;
}
}


  Re: Hiii!!!Problem with JTable Coloring  haripriya amaresan at 06:25 on Monday, November 06, 2006
 

>>Thank you very much crwood. I got it solved now.Very happy.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Re: Problem with concatenation
•  how to genrates the crystal report by sending a id at runtime
•  help me
•  pls help me with this..
•  Re: Security - Code verify
•  Job @ EarlySail
•  Job @ EarlySail (perl)
•  IPC problem
•  Re: import contacts of msn/yahoo


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007