Chess UI Code Posted... Problems setting the background of JLabel.

import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
class chess
     JLabel l[];
     JPanel p;
     public chess()
          l = new JLabel[65];
          p= new JPanel();
          p.setLayout(new GridLayout(8,8));
          p.setBorder(new MatteBorder(2,2,2,2,Color.black));
          for(int i = 1; i<=64; i++)
               l[i] = new JLabel(""+i);
               p.add(l);
          JFrame f = new JFrame ("Hello");
          f.getContentPane().add(p);
          f.setSize(500,500);
          f.setVisible(true);
     public static void main(String args[])
          new chess();
hello, i have created an array of JLabel and i wannt to set the background of JLabel white n black.. But when i write this statement it does not work..though it shud.
          if (i%2==0)
               l.setBackground(Color.black);
          else
               l[i].setBackground(Color.white);
where i am going wrong n wht is the way to do this...

can u tell wht shud i use in the if else block coz... the output is not wht i was expecting....u nd 2 chng clr wn lb @ % 8

Similar Messages

  • Problem setting JComboBox background in Nimbus

    I am having a problem setting the background component of a JComboBox when using the Nimbus L&F. The call to JComboBox.setBackground() changes the background of both the content area and the button. I do not want the background color of the button to be changed. I reviewed a number of similar posts in the forums and found a suggested change that results in the button keeping its original background. The code for the suggested change is shown below:
    JComboBox bandComboBox;
    bandComboBox.setBackground(Color.YELLOW);
    // This next line of code changes the background color of the button back to its original value
    bandComboBox.getComponent(0).setBackground(UIManager.getColor("control"));
    This change did not work for the Nimbus L&F but it did work for the Metal L&F. I really need a solution for the Nimbus L&F. Any suggestions would be greatly appreciated.
    I am currently running java 1.6_u13.
    Thanks!
    Casey

    Except for the actual arrow, the combo box arrow button doesn't paint anything in Nimbus. The highlight, the border, and the gradient background of the arrow button are drawn by the combo box painter. Hence, when you set the background color on the combo box, it also affects the look of the arrow button.
    So I thought, what if I made the arrow button opaque? Well.. it does become its own color, but it essentially becomes a giant solid-color square appended to the end of the combo box. Not exactly what we were looking for.
    So then I thought that maybe we could use the "Nimbus.Overrides" feature to override the colors on the combo box. Turns out you can't do that. The feature doesn't apply to colors. To change the base colors, you would have to go through the UIManager, and in effect change colors for all the components. Plus, the JComboBox uses colors derived from the same core color - "nimbusBase" - to paint the background of the combo box and the arrow button. So if you change "nimbusBase" to yellow then the whole combo box is still effected (although the results admittedly look better then setBackground(Color.yellow) since the gradient effects are retained).
    Anyway, after fiddling around with some ideas, this is what I came up with.
    import javax.swing.*;
    import javax.swing.UIManager.LookAndFeelInfo;
    import java.awt.Color;
    import java.awt.Graphics;
    public class Test {
        public static void main(String[] args) throws Exception {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
        public static void createAndShowGUI(){
            try {
                for (LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(laf.getName())) {
                        UIManager.setLookAndFeel(laf.getClassName());
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if(!("Nimbus".equals(UIManager.getLookAndFeel().getName()))) {
                    System.err.println("Could not find/install Nimbus LAF!");
                    System.exit(-1);
            SpecialNimbusComboBox specialBox = new SpecialNimbusComboBox(new String[] {
                "One","Two","Three"
            specialBox.boxColor ;     = Color.yellow;
            specialBox.arrowBoxColor = Color.red;
            JComboBox regularBox = new JComboBox(new String[] {
                "One","Two","Three"
            JFrame frame = new JFrame();
            frame.setLayout(new java.awt.FlowLayout());
            frame.add(specialBox);
            frame.add(regularBox);
            frame.setSize(250,150);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        public static class SpecialNimbusComboBox extends JComboBox{
            public Color boxColor;
            public Color arrowBoxColor;
            private boolean ignoreRepaint;
            public SpecialNimbusComboBox() {}
            public SpecialNimbusComboBox(ComboBoxModel aModel) {super(aModel);}
            public SpecialNimbusComboBox(Object[] items) {super(items);}
            @Override
            public void paintComponent(Graphics g) {
                ignoreRepaint = true;
                try {
                    java.awt.Rectangle b = getComponent(0).getBounds();
                    g.setClip(0, 0, getWidth() - b.width, getHeight());
                    setBackground(boxColor);
                    super.paintComponent(g);
                    g.setClip(b.x, b.y, b.width, b.height);
                    setBackground(arrowBoxColor);
                    super.paintComponent(g);
                } finally {
                    ignoreRepaint = false;
            @Override
            public void repaint() {
                if(!ignoreRepaint)
                    super.repaint();
    }

  • Problem in setting the background color of jtable with Nimbus

    Hi
    I have created a java swing application. In which I am using JTable.
    When creating a simple JTable and displaying with Nimbus, the row background color alternates between white and a light blue-grey.
    I want the table with single colour. Only the first column should be grey colour and other should be white.
    I used this code to set the background and foreground colour.
    public Component prepareRenderer
        (TableCellRenderer renderer, int index_row, int index_col){      
                Component objComponent = super.prepareRenderer(renderer, index_row, index_col);         
                Color objGreyColour = new Color(240,240,240);
                Color objWhiteColour = new Color(255,255,255);
                if(index_col == 0){
                    objComponent.setBackground(objGreyColour);
                    objComponent.setFont(new java.awt.Font(CommonClass.getFontName(),java.awt.Font.BOLD, CommonClass.getFontSize()));
                    objComponent.setForeground(Color.BLACK);
                }else{               
                    setSelectionBackground(Color.BLUE);
                    objComponent.setBackground(objWhiteColour);
                    objComponent.setForeground(Color.BLACK);
                return objComponent;
            } Look wise it is fine but when i try to select the cell it is not highlighting the cell and also i m not able to select multiple cell with ctrl key.
    Any help would be appreciated
    Thanks
    Sonal

    Hello,
    1) if you want better help soone,r please post an SSCCE (http://sscce.org) instead of a code extract.
    2) selection highlighting is lost because your code... doesn't handle selection. To take selection into account, you can use <tt>if (super.isRowselected(index_row)) {...}</TT>.
    Regards,
    J.

  • How to set the Background Color of a Text Field in a Tabular Report.

    Hello,
    I tried to set the Background Color of a Text Field in a Tabular Report.
    But I was not able to change this colur.
    In the report attributes --> column attributes
    I tried already:
    1. Column Formating -- >CSS Style (bgcolor: red)
    2. Tabular Form Element --> Element Attributes (bgcolor: red)
    but nothing worked.
    Can anybody help me?
    I Use Oracle Apex 2.2.1 on 10gR2
    thank you in advance.
    Oliver

    in "Report Attributes" select the column to move to the "Column Attributes" page. In the "Element Attributes" field under the "Tabular Form Element" region enter
    style="background-color:red;"
    I will also check if there is a way to do this via the template and post here again
    edit:
    in your template definition, above the template, enter the following:
    < STYLE TYPE="text/css" >
    .class INPUT {background-color:red;}
    < /STYLE >
    (remove the spaces after the < and before the >)
    change "class" to the class that the template is calling
    (I'm using theme 9, the table has: class="t9GCCReportsStyle1" so I would enter t9GCCReportsStyle1)
    A side-effect of using this second version is that ALL input types will have a red background color--checkboxes, input boxes, etc.
    Message was edited by:
    TheJosh

  • Problem setting the fillColor on dynamic tables using javascript

    I try to set the background color on some text fields that I put in a dynamic table. When I set the border color, everything works fine, but not with the fillColor. If I set the fillColor for the enter event for example, the field will only get a colored border, and when I click another field, the field turns completely colored. If I set an exit event with a different color, the field flickers before changing back to white. This happens in dynamic tables only. I am using Designer 8 on Windows XP. This problem has been confirmed by others, and in different forms.

    this.border.fill.color.value = "value" works for me
    value is in 255,255,255 rgb format

  • Can I invoke a SubVI in an event? and how do I set the background color of a pipe to #0000ff?

    When I click an image or a glass pipe(which belongs to Industry/Chesmitry category in palette), I want a SubVI to be invoked.
    The purpose is to fetch an OPC-UA data from a web service and to write to it via the service.
    We are building an HMI solution which displays an interactive water plant diagram.
    When users click pipes and motors in the diagram, clicked devices should be turned on and off and change their animations or colors accordingly.
    OPC-UA is for communication with devices.
    I couldn't even set the background color of a pipe to "#0000ff", but setting it to "Red" or "Blue" was possible, and I don't know how to invoke SubVIs in event scripts.
    The documentations in NI.com are confusing and lack depth.
    Even silverlight references are confusing.
    How do I do all of these?

    Hi iCat,
    Can you provide some more information about your current implementation so that we can help to answer your questions. Some questions I have for you are:
    Are you creating this project in the NI LabVIEW Web UI Builder or in LabVIEW?
    How are you publishing your webservice? Is this also in LabVIEW?
    How is your webservice interacting with an OPC-UA server?
    How is the certification set up with OPC-UA so that you can communicate between the server and the client?
    Best Regards,
    Allison M.
    Applications Engineer
    National Instruments
    ni.com/support

  • How do i set the background of the table( not of cell / row / column).

    How do i set the background of the table( not of cell / row / column).
    What happens when i load the applet the table is blank and displays the background color is gray which we want to be white.
    We tried using the setBackGround but it is not working maybe we are not using it properly. Any help would be gr8.
    Thanks in advance.

    I don't understand very well, but i guess that the background is gray when the table content's empty, isn't it?
    When the table model is empty, the JTable doesn't paint, so its container displays its background (often gray).
    In this case, what you must do is force the table to paint, even if the model is empty. So, you have to create your own table and override three methods :
    public class MyTable extends JTable
    //specify the preferred and minum size when empty
    myPreferredWidth = 200;
    myPreferredHeigth =200;
    myMinimunWidth = ...;
    myMinimunHeigth = ...;
    public Dimension getPreferredSize()
    if(getModel().getRowCount() < 1)
    return new Dimension(myPreferredWidth, myPreferredHeigth);
    else
    return super.getPreferredSize();
    public Dimension getMinimumSize()
    if( getModel().getRowCount() > 0)
    return new Dimension(myMinimunWidth, myMinimunHeigth);
    else
    return super.getMinimumSize();
    protected void paintComponent(Graphics g)
    if (getModel().getRowCount<1 && isOpaque()) { //paint background
    g.setColor(Color.white);
    g.fillRect(0, 0, getWidth(), getHeight());
    else super.paintComponent(g);
    }

  • How do I set the background color of a page_item?

    I've been pasting "background-color:#5CD65C" in a number of places, like "HTML Table Cell Attributes" under the Label and the Element tabs, but get no results. Can anyone tell me how to set the background color of a cell, please?

    Hi Doug,
    Dynamic Actions is an alternative also, especially if it is conditional.
    Action Set Style
    Fire When Event Result Is True
    Style Name background-color
    Value #5CD65C
    Selection Type DOM Object
    DOM Object your item name
    Set your Condition Type and you have it.
    Jeff
    Edited by: jwellsnh on May 24, 2011 3:55 PM

  • How can I set the background color of JLabel?

    Can any one tell me how to set the background color of a JLabel?
    I have:
    JLabel prop = new JLabel("blahblah");
    prop.setBackground(Color.red);
    Thank you in advance...

    JLabel prop = new JLabel("blahblah");
    prop.setBackground(Color.red);
    prop.setOpaque(true);

  • How 2 set the background color in j2me

    Can any body tell me how to set the background color to midlet.??

    if you are using Screen then you can call its setBackColor()
    or if you are using Canvas then you can call
    g.setColor(r,g,b);
    g.fillRect(0,0,getWidth,getHieght);
    where g is the Graphics instance of Canvas,for further information consult documentation

  • TDMS : How to set the background colour programatically for .tdms file

    Hello every one 
    is there any method to set the background colour of .tdms file programatically
    Thank you
    Raja

    Hi Raja,
    "TDMS" is a file on your harddisc. How should a file have a background color?
    Or the other way around:
    How do you set the background color of a simple text file?
    Best regards,
    GerdW
    CLAD, using 2009SP1 + LV2011SP1 + LV2014SP1 on WinXP+Win7+cRIO
    Kudos are welcome

  • Why does the eyedropper tool set the background color swatch instead of the foreground?

    Whenever I try to use the eyedropper tool in Photoshop CS5, the eyedroper sets the background color instead of the foreground. I know you can swap the colors with the arrows, but Photoshop used to be able to just put the color in the foreground swatch without me doing any extra manual work. How do I restore it?

    Click on Windows and color.  There is a setting there to change from
    foreground to background.  The one with the box around it is the default setting.

  • Setting the Background Graphics for a JFrame

    Hi,
    I am designing a GUI-based game and intend to set the background of my JFrame to a specific jgp graphic...I can't seem to find any method for setting the background in the JFrame API.
    Kindly assist!
    Cheers.

    Your search is perhaps marred by being too specific to frame. It is generally considered a bad idea to do any 'custom' thing directly to a frame. Try this search instead.
    [http://www.google.com/search?q=background+panel+java]

  • [SOLVED] nitrogen fails to set the background with openbox

    Hi guys !
    I have a strange issue . Yesterady everything was working fine . nitrogen would set my wallpaper at each boot . This morning for some reason nitrogen sets the background for about one second
    and then it goes back to the default openbox background (the gray one) . I don't remember changing anything yesterday , i just added the multilib repository so i don't think this is an issue .
    Here is my .xinitrc
    nitrogen --restore &
    xfce4-panel &
    exec ck-launch-session openbox-session
    What i'm doing wrong ?
    Last edited by shak (2010-08-28 12:59:45)

    I've finally found the solution . Apparently i forgot to edit my autostart.sh file , so when i 've isntalled the xfce4-panel there was conlfict and the background was reset to the defaul .
    After deleting the content of autostart.sh file and putting there the nitrogen and xfce4-panel commands and leaving the exec openbox-session at the .xinitrc file everything is fine

  • Can I write a new email + set the background 2 anything apart from white?

    This has been driving me mad all day (and last night!) and i am sure I am being really stupid!
    I want to write a new email to a group and I want the background to be a different colour other that white - I can see how i can change the colour of the font but not the background of the actual email.
    Please help as this is seriously doing my head in!!!
    Thanks

    Hi!
    Ok, it took me some time to find it out, but here is how it works:
    * Compose the message and make sure it's composed in Rich Text.
    * Click the Fonts button in the toolbar
    * Pick the 4th icon from the left (Document color)
    Here comes the buggy part:
    * Set the background color (it doesn't reflect in the message)
    * Save the message as Draft and close it
    * Re-open the message
    --> colored background
    Not the most intuitive solution…but it works.
    Rupert
    <Edited by Moderator>

Maybe you are looking for

  • ALV Crytal Report Ehp 2.0

    Hi All. I was recently reading the book by Thomas and Rich - Next Generation ABAP. In the ALV section, it says that with Ehp2.0, we can have crytal reports in ALV without any license or any additional server infrastructure. We only need to download t

  • SAP ProComp model not showing up in ID

    Hello, I have created a model of type "SAP ProComp model " in ESB. Now when I try to use this as template while creating a Configuration Scenario in ID, I do not see it. I am trying to locate it under "SAP Process Component Interaction Model". But wh

  • IMac 2012 OS's not loading while USB-Hub pugged in. Why?

    So i have a problem. I have a new iMac 21,5. I plugged in my iMac through my Smart-UPS and It has an USB control. SO i turn to iMac USB-Hub with 3 USB and card reader in it and to this USB-Hub i have connect USB cable going from Smart-UPS. When i tur

  • EDI processing and the application log

    All, It is my understanding that when an EDI message is processed if there is more than one error the errors will be put in the application log.   If only one error it is not placed in the application log.   Does any one have any ideas why on occassi

  • IPhone vol control doesn't function when on cradle

    Is this a BUG? When on the cradle and playing music, the volume control ceases to work as soon as a stereo output plug (3.5mm) is attached to outboard sound.