JTable Color Background?

Hello,
I am playing with JTables today, and I have a simple sample below (from suns tutorial web site). I would like to know if I can change the background color of the table, and how. Also, if that is doable, can the highlight color be changed, and if so, can I put in a handler to change these colors on the fly? If so, does someone have an example of this, or give me a hand.
Thanks,
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
public class SimpleTableDemo extends JFrame {
private boolean DEBUG = true;
public SimpleTableDemo() {
super("SimpleTableDemo");
Object[][] data = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Chasing toddlers", new Integer(2), new Boolean(false)},
{"Mark", "Andrews",
"Speed reading", new Integer(20), new Boolean(true)},
{"Angela", "Lih",
"Teaching high school", new Integer(4), new Boolean(false)}
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
if (DEBUG) {
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
printDebugData(table);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this window.
getContentPane().add(scrollPane, BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel();
System.out.println("Value of data: ");
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + model.getValueAt(i, j));
System.out.println();
System.out.println("--------------------------");
public static void main(String[] args) {
SimpleTableDemo frame = new SimpleTableDemo();
frame.pack();
frame.setVisible(true);

example:
table.setBackground(Color.red); // set table background
table.setSelectionBackground(Color.green); // set seleted row background

Similar Messages

  • JTable: After one row is modified, the row should have red color background

    After one row is modified(using setValueAt()),
    I want the modified row to have red color background color.
    How can I do that? I tried.
    JTableInstance.setBackground( Color.RED);But it sets all the rows not the only modified row.

    import java.awt.*;
    import java.util.ArrayList;
    import javax.swing.*;
    import javax.swing.table.*;
    public class TableChangeTest extends JFrame {
        public TableChangeTest() {
            initComponents();
            renderer = new MyTableRenderer();
            table.setDefaultRenderer(String.class, renderer);
            int row = 0;
            table.setValueAt("test1",row,0);
            rowsChanged.add( row );
            row = 2;
            table.setValueAt("test2",row,1);
            rowsChanged.add( row );
        private void initComponents() {
            panel = new JPanel();
            scrollPane = new JScrollPane();
            table = new JTable();
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setTitle("Table Change");
            panel.setLayout(new BorderLayout());
            table.setModel(new DefaultTableModel(
                    new Object [][] {
                        {"1", "a"},
                        {"2", "b"},
                        {"3", "c"},
                        {"4", "d"}
                    new String [] {
                "Title 1", "Title 2"
                Class[] types = new Class [] {
                    java.lang.String.class, java.lang.String.class
                public Class getColumnClass(int columnIndex) {
                    return types [columnIndex];
            scrollPane.setViewportView(table);
            panel.add(scrollPane, BorderLayout.CENTER);
            getContentPane().add(panel, BorderLayout.CENTER);
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((screenSize.width-357)/2, (screenSize.height-241)/2, 357, 241);
        public static void main(String args[]) {
            new TableChangeTest().setVisible(true);
        private JPanel panel;
        private JScrollPane scrollPane;
        private JTable table;
        private TableCellRenderer renderer;
        private ArrayList rowsChanged = new ArrayList();
        class MyTableRenderer extends DefaultTableCellRenderer {
            public Component getTableCellRendererComponent(
                    JTable table,
                    Object value,
                    boolean isSelected,
                    boolean hasFocus,
                    int row,
                    int column) {
                super.getTableCellRendererComponent(table,
                        value, isSelected, hasFocus, row, column);
                setBackground( Color.white );
                if( rowsChanged.contains(row) ) {
                    setBackground( Color.red );
                return this;
    }

  • How can I add more than one same spry menu (eg. collapsible menu)  with in different styles (font size, color, background, etc) on current page?

    How can I add more than one same spry menu (eg. collapsible menu)  with in different styles (font size, color, background, etc) on current page?

    Hi Nancy,
    This screenshot was only for imagination. A part of the code (not all) is below.  In the code there are some background images but they are not seem in live mode.
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <link href="css/my_site.css" rel="stylesheet" type="text/css" />
    <link href="SpryAssets/SpryTabbedPanels.css" rel="stylesheet" type="text/css"/>
    <link href="SpryAssets/SpryCollapsiblePanel.css" rel="stylesheet" type="text/css" />
    <script src="SpryAssets/SpryTabbedPanels.js" type="text/javascript"></script>
    <script src="SpryAssets/SpryCollapsiblePanel.js" type="text/javascript"></script>
    <style>
    #CollapsiblePanel1 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-color: #003366;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel1 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        line-height: 52px;
    #CollapsiblePanel1 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        line-height: 52px;
    #CollapsiblePanel2 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-image: url(images/international.jpg);
        background-repeat: no-repeat;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel2 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel2 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        background-image: url(images/TR_Gray2-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel2 .CollapsiblePanelContent {
        background-color: blue;
    #CollapsiblePanel3 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel3 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel3 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        background-image: url(images/TR_Gray2-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel4 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel4 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel4 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        background-image: url(images/TR_Gray2-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel5 .CollapsiblePanelOpen .CollapsiblePanelTab {
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        font-size: 18px;
        line-height: 52px;
        color: #FFF;
    #CollapsiblePanel5 .CollapsiblePanelTabHover .CollapsiblePanelTab {
        background-color: #003366;
        color: #FFF;
        text-shadow: 1px 1px #000;
        font-weight: bold;
        background-image: url(images/TR_Col-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    #CollapsiblePanel5 .CollapsiblePanelClosed .CollapsiblePanelTab  {
        background-color: #C3CFDF;
        border-radius: 5px 5px 0px 0px;
        color: #999
        text-shadow: 1px 1px #000;
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size: 18px;
        font-weight: bold;
        background-image: url(images/TR_Gray2-WEB.png);
        background-repeat: no-repeat;
        line-height: 52px;
    </style>

  • I need help with color pallette in iPhoto 11. I have created a book and would like to use a different color background than those given in "background." I have found the color palettes but can't seem to incorporate them into my backgrounds palette.

    I need help with color pallette in iPhoto 11. I have created a book and would like to use a different color background than those given in "background." I have found the color palettes but can't seem to incorporate them into my backgrounds palette.

    That is not a feature of iPhoto - suggest to Apple - iPhoto Menu ==> provide iPhoto feedback.
    Additionally I have no idea what you are saying here
    I have found the color palettes but can't seem to incorporate them into my backgrounds palette.
    And it always helps for you to dientify the version of iPhoto that you are using
    LN

  • When printing a PLD in 9.0 the color background does not show

    I have a PLD document (Sales Order) that I changed a field to have a "red" background which shows in my PLD document fine.  But I cannot get the color to show in my print out or in the print preview.  This issue has not been a problem for us in 8.82 but in 9.0 the same changes I make does not work.
    Has anyone had similar issues with this and if so, any suggestions on how to fix?
    PLD layout
    Preview of same layout
    Thanks

    Hi,
    Please compare and check both layouts. (8.82 & 9.0).
    Also refer thread No Color Backgrounds on print previews of PLD
    Thanks & Regards,
    Nagarajan

  • How do I select a black and white sketch from a white background and keep the filled in area of the sketch?  I want to copy it to a colored background. I amusing elements 12.

    How do I select a black and white sketch from a white background and keep the filled in area of the sketch?  I want to copy it to a colored background. I am using elements 12.

    oesrowstreasurer
    Please clarify your "Elements 12". Is that as in Photoshop Elements 12 (photo editor) or Premiere Elements 12 (video editor). And, on what computer operating system is the program running?
    Your question is pointing to Photoshop Elements 12 and that you have posted in the wrong forum. If this is a Photoshop Elements 12 question as I believe it is, please post your thread in the Adobe Premiere Elements Forum using the following link provided for your convenience
    https://forums.adobe.com/community/photoshop_elements
    The alternative is to wait for a moderator to see your thread here and move it from here to the Adobe Photoshop Elements Forum..
    We will be watching for a confirmation on what program you are using.
    ATR

  • How to change the color background in Pages for iPad?

    how to change the color background in Pages for iPad?

    You can find many themes at https://github.com/hdoria/xcode-themes  There's a download zip button on the right side of the page.
    Unzip and and copy the .dvtcolortheme files into /Users/YourUsername/Library/Developer/XCode/UserData/FontAndColorThemes Create the directory if it doesn't exist.
    If you can't find the Library directory you can get to it by using Finder's "Go to Folder..." option under "Go" or press shift + cmd + G then type the path.

  • How do I save an image (logo) transparent so that it will appear transparent on any color background such as a gradient or black?

    How do I save an image (logo) transparent so that it will appear transparent on any color background such as a gradient or black?
    I save image as "transparent" at File > New... but when I place on a gradient background it keeps appearing white. I'm trying to save transparent to be effective on any color such as a gradient or black.  How do I truly save as transparent?

    Put the logo on its own layer set the layer fill to 0 and add a layer style to the logo layer.  Here I duped one change fill to 0 added style.

  • How to create transparent text on a solid color background?

    I want to create a transparent text on a solid color backround, and I need text transparency to go all the way to through the image (i.e. do not show the solid color background). I have the transparent background layer, the solid rectangle layer that occupies part of the image, and the text layer (with text element) on the top of the solid rectangle layer. How do I make text transparency to go all the way through? Here is a similar example for Photoshop CS, only in my case, the backround layer is transparent (i.e. there is no picture): http://www.photoshopessentials.com/photo-effects/transparent-text-area/. Basically, I need to make the letters see through, so they show the backround color on which the image will be placed. Is it possible to do in Photoshop Elements? I'm using version 9 on Windows 7. Thanks.

    Thank you all for the suggestions, especially 99jon. 99jon's walktrhough is helpful, but I was hoping to find a workaround that would allow keeping transparent text that goes all the way through all other layers to transparent background as a text level, but once I flatten the image, I'd lose the text layer, so I cannot make any changes to it (like slightly change the font, size, style, face, etc). Let me clarify a bit what I'm trying to do to see if there is an even better option.
    Say, I need to have a 300x300 pixel image that (forget about photos) consists of a solid-color circle (circle can be white, black, red, blue, or any solid color, or gradient, does not matter) on a transparent background, and inside of this circle I'd like to cut out a word (say, the word "TEST" with no bevels or other decoration, just flat font) all the way to the transparent background. I just need a circle with text cut out and showing around the circle and through the word "TEST". So if a circle is white and I place the image on a blue background, the word "TEST" would be in blue (so would be the area around the circle).
    Is there a way to set it up to allow changing the color of the circle (say, from white to black, or blue, or red) and keep smooth edges of the text (and circle) blending with the background (i.e. avoid jagged edges) without redoing every color variation from scratch? Like, getting some master file which would define size and position of the elements,a s well as font, etc, and from this master file, use simple adjustments to change the color.
    Ideally (in my dream world), there would be an option for the text element to be of transparent color that would go through all layers below it. But there must be a not too complex way to do this, right?

  • Full screen images and full screen color background boxes are cropping horizontally.

    Full screen images aren't loading and full screen color background boxes are cropping horizontally. I'm forced to make my images a full screen slide show (of 1 image) to make it work, but then I lose the option to pin the image. Am I alone in this?

    Hello,
    I would request you to share the a url or few screenshot so that i can get better idea about the issue.
    To create a temporry url you can go to File > publish in muse.
    Regards
    Vivek

  • How to add a custom color background

    In iMovie 11 can you make a custom color background for text other than the stock backgrounds?
    If so how?

    imagine for a second a 'background' as a 'photo' or graphic:
    use any 'Paint'-app (Pixelmator, Photoshop/elements, abuse Keynote/iWork ... ), or download any .jpg you'd like to see as 'background' from the web.
    drag into Project, drag Title over it.

  • How to place digital photos onto a colored background?

    I have old photographs that I shot with my DSLR to digitize them.  I would like to combine two differently-sized photos onto a colored background to create one image.  I am brand-new to PS and LR (and very excited to finally have them!) and could use some help.
    Thank you!

    Try this:
    1) Decide what size you would like your final image to be and create a new PS document (File > New.)
    2) Click on your foreground color and choose the desired color for your background layer.
    3) To add the color to your background layer go to Edit > Fill.
    4) Go to File > Place (or Place Embedded) and navigate to your first photo. This will open it up as a new smart object layer. Press Enter to accept after it has opened.
    5) Repeat step 4 for your second photo.
    6) You can now scale your photos down and position them as desired by selecting a layer and going to Edit > Transform. Shift+click a corner of the image to scale, click inside to move. Press Enter when done.

  • Colors become pale when colored background is applied to logo

    Hello all,
    I am working with a vector file that was created by someone else and am unable to get a hold of that person. It is of a logo and when I applied a colored background to the logo or import it into another file, the color of some objects of the log becomes very pale and "desaturated." would anyone happen to knwo the solution to this? Below is an example of what happens. Early thansk for all your help!

    Open the Layers panel (Window > Layers) and click the "reveal" triangles to see all the layers, groups, and paths. What you see there will depend on how the artwork was constructed. In the Layers panel click in or to the right of the circle to the right of one of the orange arcs to select it. Open the Appearance panel (Window > Appearance) and again click the "reveal" triangles next to Fill and Stroke. Depending on how the arcs were made, either Fill (probably) or Stroke (possibly) will have the orange color applied to it. Directly under the color box it says "Opacity:", after which it will show either Default or a percentage (probably 100%) and the Blending Mode (probably Screen). If it says Default there, look at the Opacity line below and slightly to the left, which will show opacity percentage and any blending mode applied to the whole path, both Fill and Stroke.
    I think you'll find that both arcs have the same construction. Changing blending modes of either or both arcs will result in different color combinations where the arcs overlap each other and/or the blue rectangle.
    Apologies if this isn't clear. If you need clarification, I can try again tomorrow, or someone else can take a shot at it!
    Peter

  • Im trying to take a head shot and put it on a color background, but you can tell it has been done ho

    im trying to take a head shot and put it on a color background, but you can tell it has been done how do I blend it into background!

    Russell Brown's video shows this being done...
    http://tv.adobe.com/watch/the-russell-brown-show/masking-basics-in-photoshop-cs5/
    Also, sometimes, if you shoot an image with a radically different background than what you end up putting the subject in front of digitally, reflections in hair highlights and shading can make it seem unnatural.  In those cases you really have no choice but to alter the subject subtly to make it "blend" better into the scene - e.g., with the Burn and Dodge tools.
    People might make more specific suggestions if you'll put up an image (or even part of one) showing what you're working with.
    -Noel

  • White numbers on colored background?

    Sorry if this is the wrong forum to ask but couldn't find anywhere appropriate...
    I am searching for a way to print white numbers on a colored background. Best I can tell the average printer can not print white ink. So that makes me believe I need to print the background on white paper so that the numbers look white. Is that correct? Is there a way to do this? I don't know Photoshop at all and was hoping I could do it with MSWord or something like that. Any ideas?

    The link worked in Safari when I posted it, but they are converting content on the site, and since that date they've converted that page to a safari-unfriendly format.
    If you go to the link in Firefox, it works. I just did it. I tried to copy the info and post it here for you, but was unable to. So, I have copied (by hand UGH!) the info below:
    The color of text is a font attribute. The color of the background is a paragraph or table cell shading attribute.
    1. Select the paragraphs where you want to make the change. (Highlight all the text.)
    2. Choose Borders and Shading on the Format menu, click the Shading tab at the top of the dialogue box, and set the paragraph shading to black.
    3. Choose Font on the Format menu and set the font to white.
    Hope after all this, that (a) you have MS Word and (2) the steps set forth above work for you. Let us know how it goes.
    -- JDee

Maybe you are looking for