Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
850 views
in Technique[技术] by (71.8m points)

java - How to change the color of a JTable entire row having a particular column value

I have a Jtable which gets populated from an array of values. My code is like this:

  private static final String[] columnNames = {"Line Number", "Error","Fix Proposed","Percentage (%)"};
  static DefaultTableModel model = new DefaultTableModel(null,columnNames);

  public static void DisplayMyJList(List<CaptureErrors> x,String extension,
        ArrayList<Integer> l,ArrayList<Integer> p,
        ArrayList<String> e,ArrayList<String> s) throws IOException {//Method to Dynamic get values to be populated in Jtable.

    String theExtension = extension;
    if(FILE_EXTENSION.equals("java")) {
        for(CaptureErrors ex: x) {

            Vector row = new Vector();
            row.add(ex.getLinenumber());
            row.add(ex.getMyfounderror());
            row.add(ex.getMycorrection());
            row.add(ex.getMyPercentage()+"%");

            model.addRow( row );

            //model.setRowColour(1, Color.YELLOW);
        }
    }

table = new JTable(model);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);    
    table.setFillsViewportHeight(true);
    table.setShowGrid(true);
    table.setShowVerticalLines(true);
    table.setGridColor(new Color(0,128,0));
    JTableHeader header = table.getTableHeader();
    table.setBackground(new Color(255,228,225));
    table.setEnabled(true);
    header.setFont(new Font("Dialog", Font.CENTER_BASELINE, 12));
    header.setBackground(Color.black);
    header.setForeground(Color.yellow);
    JScrollPane pane4 = new JScrollPane(table); 

I can populate the Jtable from the array of values by Using a JButton. I want to have a condition where if column "percentage" ,get all values in this column >30, it highlights the rows to color.red.

I dont want to user TableCellRendererComponent .I want this action to be performed upon clicking the Jbutton.

The actual Jtable looks like this: enter image description here

Then according to what I want to get, the first 2 rows should be highlighted in red. Any help appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See the approach in Table Row Rendering for a solution without creating custom renderers.

You may also want to check out Table Format Renderers so you can format the percentage column easily.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...