-1

I want to extend the DefaultTableModel class with a function that let's me set the color of a specific row. So lets say I have a table with 30 rows and want to color row 12 in red.

I want to invoke a function like:

JTable table = new JTable();
DefaultTableModel model = new DefaultTableModel();
table.setModel(model);

[...]

model.setRowColor(12, Color.RED);

[...]

I know that there are many similar questions regarding the topic "color a JTable row" but I was not able to implement any of these solutions. Maybe somebode could help me to understand custom renderers and how to implement my custom method.

Flatron
  • 1,375
  • 2
  • 12
  • 33
  • 1
    there are two ways 1. prepareRenderer (easiest of ways and methods implemented in APIs) in the case that decorations is depends of value stored in DefaultTableModel, 2. store arrays of Colors in the model as part of in the case that value came as parameter from rest of GUI – mKorbel Mar 13 '15 at 06:27
  • agree with down_voter, here is many codes in SSCCE/MCVE form, I'm think, sure that link to thread protect your question, shows your effort – mKorbel Mar 13 '15 at 06:31
  • `TableModel` is not responsible for performing this action, get the renderers to do it...or use `JXTable` from SwingLabs SwingX library which provides highlighting functionality – MadProgrammer Mar 13 '15 at 06:38
  • What decisions are you making to make a give row a different color? – MadProgrammer Mar 13 '15 at 06:38
  • One decision is if a cell containing a boolean is true (checked) then the corresponding row should be colored green. – Flatron Mar 13 '15 at 06:41
  • boolean is true (checked) then the corresponding row should be colored green. == prepareRenderer, many times here with an SSCCE/MCVE – mKorbel Mar 13 '15 at 06:59

1 Answers1

0

if a cell containing a boolean is true (checked) then the corresponding row should be colored green.

Table Row Rendering shows how you can color a row based on a value in the row.

camickr
  • 321,443
  • 19
  • 166
  • 288