What color is the rectangle of a targeted in a JTable cell

Hello

where do you look to the color scheme from a table top to set the color corect for the rectangle of a target cell?
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class FocusRect {

  public static void main(String args[]) {
    JFrame frame = new JFrame("Focus rectangle");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 150);
    Container contentPane = frame.getContentPane();

    String headers[] = {"Fruit", "Colour", "Count"};
    Object data[][] = {
         {"Tomato", "Red", 1000},
         {"Banana", "Yellow", 2000}
       };

    TableModel model = new DefaultTableModel(data, headers);

    JTable table = new JTable(model);
    table.getColumn("Count").setCellRenderer(new IntegerRenderer());
    JScrollPane scrollPane = new JScrollPane (table);
    contentPane.add(scrollPane, BorderLayout.CENTER);
    frame.setVisible(true);
  }


  static class IntegerRenderer extends DefaultTableCellRenderer {
    public IntegerRenderer() {
      setHorizontalAlignment(SwingConstants.RIGHT);
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
          boolean isSelected, boolean hasFocus, int row, int column) {
      setFont(table.getFont());
      if (value instanceof Integer)
     setText(String.format("%,d", value));
      else // String
     setText(String.format("%,d", Integer.parseInt((String)value)));
      if (isSelected) {
           setBackground(table.getSelectionBackground());
     setForeground(table.getSelectionForeground());
      }
      else {
           setBackground(table.getBackground());
     setForeground(table.getForeground());
      }
      if (hasFocus)
     setBorder(new LineBorder(Color.BLUE)); // Wrong colour!
      else
     setBorder(null);
      return this;
    }
  }

}
Edited by: Jörg on 28.10.2011 20:32

Since you extend DefaultTableCellRenderer you even not need this code. Simply invoke:

super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

at the beginning of the method and the rendering engine all the properties will be set for you. Then all you need to do is to format the text.

However, if all you try to do is the format of the text there is a simpler solution. Instead of override the getTableCellRendererComponent() method, you can override the method setValue (...).

See [url http://www.camick.com/java/blog.html?name=table-format-renderers] Table Format converters for an example that uses this approach. Table Format Converter contains an example of creating a rendering engine of percentage. Of course these converters assume that the code is of the same type. This isn't a good idea to mix Integer and String data in the same column.

Tags: Java

Similar Questions

  • Fill color of the rectangle does not?

    I have a document where I'm trying to draw a simple rectangle with a fill color using the Rectangle tool. All of a sudden the fill color will not be displayed. I can't understand why he did this. I tried to create a new layer for the rectange but it will not always filled correctly. It is not hidden behind whatever it is, it is already 'arranged in front."  I enclose a screencap in order to explain my problem.

    As you can see from the pictures, I tried trawing a teal rectangle, but only half of it is met (note the blue frame, showing that half left of the color of the rectangle is not displayed).

    Someone at - he encountered this before? Help! I use CS4.

    Picture 3.png

    Picture 2.png

    The effects panel says there is a kind of effect applied. Maybe that's the problem...

  • AS the color of the rectangle fill

    Hi I want to set the fill color of a rectangle with applescript:

    say application «Adobe InDesign CS4»

    Activate

    put myRectangle to rectangle 1 page 1 of document 1 whose label is "testa"

    set the fill color of myRectangle to "C = 20 M = 100 Y = 100 K = 10.

    end say

    It does not work.

    Help, please

    Thank you

    It works if there is a shade of color match with the name 'C = 20 M = 100 Y = 100 K = 10'. (Name with color value is enabled in Options of nuance).

  • What color is the police?

    I have a project with a lot of different blocks of text and changing colors from letter to letter in the words.

    How to identify what is any given color?

    When I click on a letter, it shows me a Visual block of color, but don't tell me what is the number of corresponding color.

    Problem is, if I see a letter and want to take its color to use elsewhere, I have no way of knowing what is this color.

    Any ideas?

    Screen Shot 2013-06-26 at 10.03.14.png

    Screen Shot 2013-06-26 at 10.04.43.png

    Zoom, use the eyedropper tool to sample it and the color will go into the squares of color.

  • How to color and the number of items vary continuously cursor instead of the time?

    I did an animation flash simple as below:

    the first key frame: the second keyframe:

    1.jpg2.jpg

    I want to create a slider to make the color of the rectangle varies from red to yellow by the cursor permanently ' movement rather than the hour and make balls fewer 15-8 at the same time.

    could you give me some advice about this?

    Thank you very much.

    Do you understand how to operate the cursor using code to read its value?  You will need to consider the issue to do this.

    What you can do, is get the yello rectangle be a movieclip which is located on the red rectangle and use the slider to control the alpha of the yellow rectangle from 0 to 1 property.  In this way the yolk will fade gradually while sliding.

    You'll then need to do is to find a way to make the number of dots that appear cut from 15 to 8 for the same interval of the cursor.  You must use the fractional value of the scabbard to decide how many points appear or not appear.

    You should not start new assignments for the same subjects you arlready have validations for the.  Do so in the future could lead to them being deleted.

  • Change the color of the field

    I want to create a field that contains a rectangle I want to change the color of the rectangle it flashes. The implementation of flashing

    With some delay in treatment, I think that the approach proposed by stef will tie up your Blackberry looped continuously update the field.  Invalidate in the paint method will stop the treatment immediately in order to redraw this field.

    Instead, use a Thread or a TimerTask to invalidate the ground at regular intervals.

    As example where it's done, take a look on:

    How - to view an animated GIF
    Article number: DB-00652
    http://www.BlackBerry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800345/...

    This article uses a Thread to schedule updates on a field that displays an animated gif image.

  • Issue of strange writing with no fill color for the area of redaction

    I put it to any color in the fill color of the area of redaction.  When I mark my redaction, choose apply the drafting, the selected area is displayed.  When I save the file, exit, and then re - open, writing is a party as it is supposed to.  If I choose a fill color of area of redaction, mark my writing and then apply the fill color box now works.  I have to redact the number of pages a day, and have to save and reopen to check my work is very time-consuming.  The strange thing is that it doesn't have to all the documents I'm working on.  I use Adobe 11.0.07.  Any solution greatly appreciated!  Thank you.

    This is because you chose "No Color", which is equivalent to transparent.

    If your use of a white background white.

    Choose what color is the background.

  • Illustrator CC: How can I change the color of the artboard?

    It's frustrating.

    No matter how to set preferences for the interface, when I do a new work plan, work plan is white.

    The brain dead workaround is to put a rectangle of color I want on a locked background layer and make it the same size as my work plan, but this turns 'Shift + O, drag' to ' Shift + O, M, drag, slide, set fill color desired, uncheck the rectangle option, restore the previous fill color tool and selection. "

    It seems that there should be an option of the interface somewhere that allows me to set the color of the new work plans, but advice only that I can find on the setting of the color of the artboard is said to use "simulate colored paper." Colorful paper simulation is not the color of your work plan. It simulates the way ink some of the color of the paper still leaves shine through.

    I don't want my work plan to show through. I just want to is it a different color. When I press Shift + O and drag a new work plan, I want this artboard to be of the same color. If I decide I want a different color, I want to change a single setting somewhere and have the colors of all work plans in changing the document at a time.

    I can't find any setting for this in the interface. Am I missing something, or is it simply not there?

    It's just the way it is. If you want all your work plans to share the same color, create a giant rectangle that covers the entire work area, all work plans will share the same base color. Change the color on all layers by changing the color of the rectangle unique. Or you could use an overall color and change the build of the color in the swatch Panel.

  • Problems with the Rectangle tool

    Hello

    I'm having a problem. I created three camembert grahics with some text and boxes. Everything has been created in Muse, with the exception of the three piecharts, which are some PNGs. I created a box colored (using the rectangle tool) and placed behind the content. When I discover in preview (as well as the preview in the browser), two of the graphics look good, but the first, is a couple of pixles shorter in height. I have attached a screenshot of the workspace of MUSE and the preview for comparrison screen. Everyone on the pitch "containers" should be 600 px in height, but when I inspect element in the browser, the height of the first 'container' is 594px and the other two "containers" are 599px each. How is it possible for the first "container" to lose 6px in height and two others to lose a px of height?

    Can someone help me with this please? As a designer with little knowledge of coding, I really like the potential of MUSE, but it's these little "quirks" who eat my time and are not really profitable to my budget projects and timelines.

    Muse01.jpgMuse02.jpg

    Post edited by: kmonk9000

    The exact issue and ways to get out of it is detailed in the video here - http://www.screencast.com/t/ao3syjgXa.

    Thank you

    Vinayak

  • Changing colors of the MovieClip

    I have a simple mc of a rectangle in a library with a link that I'm wearing in the scene with an attachMovieClip. I want to change the fill color of the rectangle. All of the examples I've seen look like you need to draw a line with a beginfill object before the first line and an endfill when your done. Is there an easier way?

    You can easily change the color of a movieclip using the class color, and setRGB(). but it will not be limited to your rectangle fill color unless the filling is a child movieclip from your library video. is it?

  • Entry into the JTable cell

    Hello again,

    I am trying to prohibit the development of transfer of a modified JTable cell which is empty.
    So I have
    -activated new cell
    -restore the cell edition
    -asked the editor in Chief of the cell to get the point
    -attached an InputVerifier to the editor-in-Chief of the cell
    but none of these attempts worked.

    Any ideas I could try?
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
    
    public class ForceInput {
    
      public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
          public void run() {
         new ForceInput().createAndShowGUI();
          }
        });
      }
    
    
      void createAndShowGUI() {
        JFrame frame= new JFrame("Force Input");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setContentPane(createContentPane());
        frame.setVisible(true);
      }
    
    
      public JComponent createContentPane() {
        JScrollPane scrollPane= new JScrollPane(new MustFillTable());
        return scrollPane;
      }
    
    
      class MustFillTable extends JTable {
        final String[] HEADER= {"Fruit", "Colour"};
        String data[][] = {
             {"Tomato", "Red"},
             {"Banana", "Yellow"},
             {"Orange", "Orange"},
             {"Kiwi", "Green"},
             {"Grape", "Red blue"},
             {"Grape", "Green"},
             {"Plum", "Blue"}
           };
    
        public MustFillTable() {
          TableModel model= new DefaultTableModel(data, HEADER);
          setModel(model);
          JTextField tf= new JTextField();
          tf.setInputVerifier(new InputVerifier() {
         public boolean verify(JComponent comp) {
           String buf= ((JTextField)comp).getText();
           if (buf.trim().length()==0) return false;
           return true;
         }
          });
          DefaultCellEditor dce= new DefaultCellEditor(tf);
          getColumn(HEADER[0]).setCellEditor(dce);
          getColumn(HEADER[1]).setCellEditor(dce);
        }
    
        @Override
        public void editingStopped(ChangeEvent e) {
          super.editingStopped(e);
          int i= getSelectedRow();
          int j= getSelectedColumn();
          if (getValueAt(i,j).toString().trim().length()==0) {
         JOptionPane.showMessageDialog(this, "Input required.", "Error",
                                  JOptionPane.ERROR_MESSAGE);
         ListSelectionModel lsm= getSelectionModel();
         lsm.setSelectionInterval(i,i); // Doesn't work
         lsm.setLeadSelectionIndex(i); // Doesn't work either
         editCellAt(i,j);
            JTextField tf=
              (JTextField)((DefaultCellEditor)getCellEditor(i,j)).getComponent();
         tf.requestFocusInWindow();
          }
        }
      }
    
    }
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    import javax.swing.table.*;
    
    public class TableEdit extends JFrame
    {
         TableEdit()
         {
              JTable table = new JTable(5,5);
              table.setPreferredScrollableViewportSize(table.getPreferredSize());
    
              JScrollPane scrollpane = new JScrollPane(table);
              getContentPane().add(scrollpane);
    
              //  Use a custom editor
    
              TableCellEditor fce = new FiveCharacterEditor();
              table.setDefaultEditor(Object.class, fce);
         }
    
         class FiveCharacterEditor extends DefaultCellEditor
         {
              FiveCharacterEditor()
              {
                   super( new JTextField() );
              }
    
              public boolean stopCellEditing()
              {
                   try
                   {
                        String editingValue = (String)getCellEditorValue();
    
                        if(editingValue.length() != 5)
                        {
                             JTextField textField = (JTextField)getComponent();
                             textField.setBorder(new LineBorder(Color.red));
                             textField.selectAll();
                             textField.requestFocusInWindow();
    
                             JOptionPane.showMessageDialog(
                                  null,
                                  "Please enter string with 5 letters.",
                                  "Alert!",JOptionPane.ERROR_MESSAGE);
                             return false;
                        }
                   }
                   catch(ClassCastException exception)
                   {
                        return false;
                   }
    
                   return super.stopCellEditing();
              }
    
              public Component getTableCellEditorComponent(
                   JTable table, Object value, boolean isSelected, int row, int column)
              {
                   Component c = super.getTableCellEditorComponent(
                        table, value, isSelected, row, column);
                   ((JComponent)c).setBorder(new LineBorder(Color.black));
    
                   return c;
              }
    
         }
    
         public static void main(String [] args)
         {
              JFrame frame = new TableEdit();
              frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
              frame.pack();
              frame.setLocationRelativeTo( null );
              frame.setVisible(true);
         }
    }
    
  • How is - what cut you a rectangle in 2 colors with a diagonal from top to bottom?  Once I draw a diagonal with the online tool I am at loss to create 2 color split?

    How is - what cut you a rectangle in 2 colors with a diagonal from top to bottom?  Once I draw a diagonal with the online tool I am at loss to create 2 color split?

    Rosemary,

    A simple way would be to:

    (1) give the rectangle a color, then Ctrl > Cmd + C + F;

    (2) use the direct Selection tool, and then deselect (click and empty space), then click on the opposite corner to the part where you want to keep the color, then press DELETE, then change the fill color to the other.

    Or, you can ClickDrag with the line tool along the diagonal line from corner to corner (guides are your friends), select it all and Pathfinder > divide, then select live from the triangle where you want to change the color and change.

  • Charger Mag Safe 2 what color the light should be?

    MAG 2 sure what color the light should be? I care and it works but the light is red or green?

    Green is in charge...

    Portable Apple computers: Troubleshooting MagSafe adapters - Apple Support

  • What is the color of the bar in disk utility?

    Using disk utility, I ran a check on my main HD and my three external drives.

    The S.M.A.R.T. status of my main HD has been "verified". External drives were 'not supported '. I guess that means that Dick Utility is unable to "verify".

    What concerns me is the color of the bar above the chart. On my main HD and two of my external hard drives, it is blue (see picture #1). However, on an external drive, it is red (see photo #2).

    I can't find any information on the color of the bar in the Apple Help. The reader has a bit more than 33% available force and seems to work correctly in

    to access the media. Do you have any idea what this could mean?

    Thank you

    Philippe

    You can see all partitions on your hard drive in this bar, and each color represents a partition. Knowing this, don't worry because this does not mean that your external drive is not working properly.

  • What do the colors of the windows logo mean?

    What do the colors of the windows logo mean?

    It depends on what you mean. If you mean the microsoft logo then four colors have meaning yellow-Bing of Office Orange/red green blue-Windows-Xbox is the meaning of the four colors I hope that find you it useful

Maybe you are looking for

  • I have a deskjet 1000 and bought the HP 61 cartridge.

    I have a deskjet 1000 and bought the HP 61 cartridge. The deskjet printer came with 122 HP cartridges.  I tried to install the HP 61 cartridge but the message is "incompatible cartridge".

  • PCI-6528

    I'm looking for a letter of volatility for the PCI-6528.

  • Class LV instrument drivers

    Are there good examples of using the LV class to instrument drivers. Just learn the LV classes and simple examples such as GPIB DMM food or power would be great. Thank you Jim

  • Lack of Service SM BIOS driver?

    You seem to be missing the driver of service SM BIOS... I thought it was part of the driver chipset but installing it didn't solve it. I have run the utility was updated and that does ' t get the driver to make it. Any ideas guys? Link to photo

  • Blank pages occur if I try to print e-mail messages. Virus 'All white' cause?

    Printing other stuff also gives incomplete pages. I have Verizon Internet Security and other protection tools, but they seem powerless. I read that 'All white' has been victimizing more people. How can I permanently delete? Some security scans are no