Set the color of a row in a JTable

Hi all
I need to set the color of a line (or simply a cell) in a JTable to a specific color,
I read these pages:

http://StackOverflow.com/questions/1057710/how-to-set-the-colour-of-my-1st-row-in-my-JTable-to-yellowor-any-colour
http://www.roseindia.NET/Java/example/Java/Swing/SadingRows.shtml
http://StackOverflow.com/questions/3875607/change-the-background-color-of-a-row-in-a-JTable
http://www.codeguru.com/Forum/archive/index.php/t-34478.html
http://www.thatsjava.com/Java-Swing/16549/

and lots and lots of links like these... all of them are too complicated and HARD...
What I need, it's too simple: pick a line and then set the color red for example...
Can you help me please...

I need the simplest code possible

Thank you very much

{/ / Here a Question: this is the beginning of the block what?} Method? class? where is the name?

It's what we call a "class internal annonymous. Its extension JTable and similar to the override of the prepareRenderer() method if you do not need a separate source file.

This method returns a component... while I was expecting, it does the job and returns nothing (void)...

The code is substituting a JTable method defined in the API.

It seems that these codes CREATE a JTable with a custom look, whereas what I need is to EDIT and CHANGE a JTable that is ALREADY CREATED

Custom made is a design time decision. That is, you need to create and implement an API at design which allows to dynamically change the rendering at runtime. The default API does not support line-level rendering which is why you need to create your own API to do this.

I need to set the color of a line (or simply a cell) in a JTable to a specific color,

Converters of work by making the data contained in a table cell. If you want a rendering engine, a cell or a row of a table a different color, so you need to store this information somewhere so that the rendering engine knows what to do.

If you feared strictly for color lines, then approach you could do is create acard. When you want a row to be a specific color, then you add and entry to the map. The best way to use this information would override the method prepareRenderer() to check the card each time to see if a custom color is for the line to be renderered.

Here is a simple example of a simple API added to the JTable which lets you row rendered level:

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

public class RowTable extends JTable
{
     Map rowColor = new HashMap();

     public RowTable(TableModel model)
     {
          super(model);
     }

     @Override
     public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
     {
          Component c = super.prepareRenderer(renderer, row, column);

          if (!isRowSelected(row))
          {
               Color color = rowColor.get( row );
               c.setBackground(color == null ? getBackground() : color);
          }

          return c;
     }

     public void setRowColor(int row, Color color)
     {
          rowColor.put(row, color);
     }

     public static void main(String[] args)
     {
          DefaultTableModel model = new DefaultTableModel(10, 4);
          RowTable table = new RowTable( model );
          table.setPreferredScrollableViewportSize(table.getPreferredSize());

          table.setRowColor(1, Color.YELLOW);
          table.setRowColor(4, Color.RED);
          table.setRowColor(7, Color.ORANGE);

          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
          frame.add( new JScrollPane( table ) );
          frame.pack();
          frame.setLocationRelativeTo( null );
          frame.setVisible(true);
     }
}

Tags: Java

Similar Questions

  • Set the color to a table Cluster

    Is it possible to set the color, if the table, fonts, cluster, etc. on a specific column and row within a table Cluster? Looking at the properties and methods, it does not seem so. I have no other options that rethink my VI?

    Here's an example of how my data are implemented with the sample data provided.

    I would appreciate any advice.

    Interesting that you should raise this issue.  Coincidentally, a similar question was answered just in time:

    https://forums.NI.com/T5/LabVIEW/change-enum-colour-in-a-cluster-that-is-in-an-array/m-p/2918506#U29...

    Check out Jeff's response and see if maybe you can adapt to your problem.

  • How to set the color for a particular column in the table in advance?

    How can we set the color for a particular column in advance table based on a few feteched of vo in process request query parameter?

    Hello

    Reference http://oraclearea51.com/blog/dynamically-color-the-rows-in-an-oa-framework-advanced-table.html

    and prev thread. Can color us the lines in the column of a table

    It will be useful.

    Kind regards
    GYAN

  • How to set the color depth

    try to set the color of amd's message color depth less than 24. How to set the highest color depth?

    If your computer allows you to change this setting, it will be located in the "Control Panel > display > resolution > Advanced Settings > Monitor tab > screen colors.

  • Set the color for emphasis on the interactive whiteboard to HorizontalFieldManager

    I have an array of horizontal fields that contains a bitmap and a labelfield each. The entire line should be clickable who worked until now, but how do I set the color to accent properly? At the moment the onFocus and onUnfocus functions are completely ignored.

    It's the definition of my table:

    for (int i = 0; i < listSize; i++) {
            logInDetailManager[i] = new HorizontalFieldManager(
                    Manager.USE_ALL_WIDTH | Field.FOCUSABLE) {
    
                protected void onFocus(int direction) {
                    super.onFocus(direction);
                    background_color = Color.RED;
                    invalidate();
                }
    
                protected void onUnfocus() {
                    invalidate();
                    background_color = Color.GREEN;
                }
    

    And that's how I add my horizontal fields:

    logInDetailManager[i].setChangeListener(this);
    logInDetailManager[i].add(dummyIcon[i]);
    logInDetailManager[i].add(new LabelField("hello"));
    logInDetailManager[i].add(new NullField(Field.FOCUSABLE));
    add(logInDetailManager[i]);
    

    Thanks for your help!

    AAH it got already...

    If someone has trouble with this too: I removed onFocus() and onUnfocus and set the background color in the paint so the color of the entire line method is modified when:

    protected void paint(Graphics graphics) {
    
        graphics.setBackgroundColor(isFocus() ? Color.RED : Color.GREEN);
        graphics.clear();
        invalidate();
        super.paint(graphics);
    }
    
  • How to set the color of foreground to EditField

    Hi all

    I added EditField to display new EditField("About:","",1024,EditField.FILTER_DEFAULT).

    Now I want this editfield foreground color value. But there is not setForegroundColor() as setBackgroundColor() in the EditField class.

    Can someone tell me how to set the color of foreground to EditField?

    I think so

    You can change the foreground color by replacing your method of painting:

    That is to say

    editField = new EditField()
    {
        public void paint(Graphics g)
        {
          g.setColor(Color.GRAY);
          super.paint(g);
        }
    };
    
  • I need something to smooth out grainy photos and set the color.  I have several photos that I need to use which has a blue cast on them.  Photoshop will remove the color blue?  I need an answer immediately.

    I need something to smooth out grainy photos and set the color.  I have several photos that I need to use which has a blue cast on them.  Photoshop will remove the color blue?  I need an answer immediately.

    Yes, photoshop can remove noise and remove color casts

  • How to set the color for background ChoiceBox point to ChoiceBox with css?

    How to set the color for background ChoiceBox point to ChoiceBox with css?

    I need to change the backgound color list items.

    I tried with the following code, but it's not help to:

    {.context-menu .choice-box

    -fx-background-color: Red;

    }

    .choice-box .menu-item {}

    -fx-background-color: red;

    }

    In Java 8, either of your selectors should work.

    In Java 7 (JavaFX 2.2), you need of the following workaround:

    #choice-box-menu-item {
         -fx-background-color: red ;
    }
    
  • Set the color of the horizontal rule

    How to set the color of a horizontal rule? The properties box allows you to change the length, width, location & shading - but not color. Is it possible to change the color in the code? If the color change is NOT possible, is there another option to 'draw the objects' in Dreamweaver? Note: I use Dreamweaver 8.0.

    Thanks for your time!

    You can go to Code view and add the color = "whatever" to the HR tag, but I think it only works on IE. The best way is to add a property of CSS color for the hr tag.

  • setting the color of the font in a text flash Member

    Hi all

    I'm trying to set the color of my text in a text flash member using the script below, but I get an error. No idea why? Thank you very much

    on startmovie
    Sprite (7) .fontSize = 20
    Sprite (7).font.color = rgb (153,153,51)
    end

    If you use the flashComponents, you should consult the
    the documentation that comes with Flash for formatting information. TO
    to make changes, you need to set the style of the element. Of
    Manager, you can change the color of the text for a TextInput component as
    This (assuming that your wickedness is in channel 1):

    Sprite (1). SetStyle ("Color", "0xFF0000")

    That will change the color red.

    Other style options (for flash TextInput component) are:

    backgroundColor
    borderStyle
    marginLeft
    marginRight
    disabledColor
    IncorporerPolices
    fontFamily
    fontSize
    fontStyle
    fontWeight
    textAlign
    textIndent
    textDecoration

    You can find out what are the current values using getStyle()

    put sprite (1) .getStyle ("color")
    -"0xff0000".

  • Setting the color of a DataGrid row

    Hello
    I have a DataGrid and according to one of the values in each row, I need to highlight the line. So say for example if the Datagrid represents a list of customers and if the balance of the account is less than 0, I need to display this line in red (preferably in red text).
    I am able to do this using an itemRenderer on each cell, but because my dataProvider is important enough makes the slow page, especially when the user tries to scroll through the data grid.

    Is there any other way to do this without using itemRenderers for each cell?

    Thank you
    Orla

    Tutorial of Peter Ent has helped me TONS

    http://weblogs.Macromedia.com/pent/archives/2007/02/coloring_the_ba.cfm

  • you will need to set the color of the labelfield.

    Hi all

    Can someone suggest me, how can I set labelfield color?

    I can able to define the fonts but I m not able to get how will also set the font color.

    Ajay

    LabelField objLabel = new LabelField ("test") {public void paint (Graphics g) {g.setColor (Color.RED); super.paint (g) ;}};}

    Only try this let me know.

  • How to set the color palette for the system colors for the visible record of all controls on the web page?

    The color scheme by default in Windows 7 (x 36, Premium family edition) offers a combination of a white background for the application windows and black text for information that are there.   But being a visually handicapped person, I find the white background rather irritating to the eyes, and I'd rather have a black background, with white text, as this combination gives me a comfortable environment to work on the computer for long periods.  When I set these colors according to my convenience, using the link ' Customize > window color > color and appearance of windows "and by changing only the colours of the just 'Windows' choice in the dialog box, I think the colors of several other controls, in particular the"Drop Down Lists","Framed"and"Buttons"are also changed, often to be the same thus making the information unreadable such controls.  As these color changes seem to be rather random and apparently 'unpredictable', I cannot set the application window, according to my convenience.  I'm not able to get any useful information from the link "Help and Support" of the STARTUP dialog, about the dependencies of the combinations of colors of controls, or the link dialog box "Customize > window color > color and appearance of windows ' lists all controls that are normally used in any typical web page.  After spending a lot of time and effort - and therefore actually "lose" my time at the computer - through several tests, with all giving them negative results, I am at a loss to get this problem is solved.  Furthermore, I can't go back to the default of origin either.  So my computer is idle for a very large part of the day.

    I would be really grateful to all users or personal assistance MS, who take the time to provide me with an answer to this problem and help me use the computer in a fruitful way.  I offer my sincerest thanks in advance to anyone who offers me a complete solution.

    Hello

    I suggest that you go through the Microsoft article and check if it helps.
  • LR 6.3 - How to set the color temperature in units of 50?

    Recently updated to 6.3 LR LR 5. In LR 5, I could click on the color temp box and drag left and right to adjust the temperature of color 50 increments of unit. Now, it will sometimes start 50 unit increments, but then if I drag further, it starts to change the setting in random numbers. I know that this is normal behavior for the actual slider, but I would get the 50 backstroke of function setting unit when clicking / sliding on the box color temp. Thanks for any help.

    If you use the upper and down arrows instead of the mouse, the increment is 50.

    If you do the more wide Panel, you get a finer granularity with the mouse, movements, but which removes the space in the picture display.

  • Set the color of the text for &amp; quot; Topics &amp; quot; DTC?

    I want to use a related topics text control at Design time. The fonts options to set the name of the font, bold, etc. and highlight, but not the color of the font. Is there a way to change the font color? To use a style as Hotspots in expansion? Or can I manually add something in the code?

    Hi, Margaret.

    If it's any help, the color is the fourth attribute that you can set for the property are the order of related topics. For example:

    There is more information here:

    http://msdn.Microsoft.com/en-us/library/ms644677 (vs.85) .aspx
    http://msdn.Microsoft.com/en-us/library/ms644681 (vs.85) .aspx

    Pete

Maybe you are looking for

  • My iPhone was fried

    my iPhone has been fried due to water damage how much will it cost? If anyone knows could you please tell me thanks

  • Battery - Vista is unable to get it back the power of the options for the battery

    Hello Vista retrieves me the error message is not able to retrieve the power settings.I have to install a softawre to allow me access to these settings? Thanks for you comments. Rgds, F,

  • Portege Z830 - 10K. Abaout need 20% battery in Mode offline!

    If I change my Z830 - 10K off (Off komplete!) must be aporx 20% of the capacity of the battery in 24 hours. If after 5 days, the battery is empty. Already, I turn off the parameter 'quick start input... ". "the BIOS? What can I do? Thanks in advance

  • Mail crashes during import of mailbox

    When I open the Mail, I get: Importing mail messages This process hangs at 6408 17029 messages Box import mailbox ' Inbox - xxAccount. I tried the following: Reinstalled OS 10.11 Off managers Do all inactive e-mail accounts, including me.com iCloud S

  • HP mini 110-3601TU unable to connect to the internet

    I'm under the netbook with Windows7 32-bit with Service Pack 1 Starter Pack. The PC connects to the internet eventhough bluetooth and Wi - Fi indicate that they are not cancelled.  Try to connect with HP Quickweb is possible, however.  So, there's so