Jtable cell having multiple font colors

Is it possible to have text in a cell with multiple colors (e.g. using html)?
-Jim

Should be possible:
http://java.sun.com/docs/books/tutorial/uiswing/components/html.html
I was able to do different colors by adding HTML to one of my table cell data items:
<html><center><b>Enable</b> <font color=#ff00dd>middle button</font>

Similar Messages

  • Multiple font colors

    what's up
    does anybody know how to display font in multiple colors in a Jtextarea or another text component every time i change the font color in my jtextarea it changes for all the words i want it to change for only the next words that i type.
    thanks

    the following sample might be useful to u. i am not sure whether this works for textarea.
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.*;
    public class test extends JApplet
         private JTextPane pane;
         private JButton colors;
         private JToolBar tb;
         public void init()
              pane = new JTextPane();
              tb = new JToolBar(JToolBar.HORIZONTAL);
              colors=new JButton("Colors");
              colors.addActionListener(new ActionListener()
                   public void actionPerformed(ActionEvent e)
                        Color newColor = javax.swing.JColorChooser.showDialog(null,"Choose Text Color",Color.cyan);     
                                  if(newColor!=null){
                                            MutableAttributeSet attr = new SimpleAttributeSet();
                                            StyleConstants.setForeground(attr,newColor);
                                            pane.setCharacterAttributes(attr,false);
              tb.add(colors);
              getContentPane().setLayout(new BorderLayout());
              getContentPane().add(tb,"North");
              getContentPane().add(pane,"Center");
    //<applet code=test.class width=300 height=300></applet>
    for more information follow this link.
    http://manning.spindoczine.com/sbe/files/uts2/Chapter20html/Chapter20.htm
    cheers
    sri

  • How to access jtable cells in multiple tabs?

    Hi,
    in my program when I push a button 3 tabs are created. And all these tabs have a table in it. What I want to do is that When I push other button I want to access all these tables and cells. and get the cell values. How can I get the value of one cell in the jtable when I have multiple tabs?
    thanks in advance.

    This is my main window
    public class AuditClient extends javax.swing.JFrame {
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    createCheckList();
    public void createCheckList() {
    CheckList.createCheckList(CheckListChildPane);
    private javax.swing.JTabbedPane CheckListChildPane;
    private javax.swing.JPanel CheckListParentPanel;
    And This is the CheckList.java : This code create tabs and tables in it. (from xml file).
    public class CheckList extends JPanel implements ActionListener{
    JTable table;
    public CheckList(Object[][] TheData) {
    MyTableModel model = new MyTableModel();
    model.setData(TheData);
    table = new JTable(model);
    JScrollPane scrollPane = new JScrollPane(table);
    add(scrollPane);
    class MyTableModel extends AbstractTableModel {
    public String[] columnNames = null;
    public Object[][] data = null;
    public final Object[] longValues = null;
    public void setColumnNames(String[] cols) {
    this.columnNames=cols;
    public void setData(Object[][] datas) {
    this.data=datas;
    public int getColumnCount() {
    return columnNames.length;
    public int getRowCount() {
    return data.length;
    public String getColumnName(int col) {
    return columnNames[col];
    public Object getValueAt(int row, int col) {
    return data[row][col];
    public Class getColumnClass(int c) {
    return getValueAt(0, c).getClass();
    public static void createCheckList(JTabbedPane CheckListChildPane) {
    //creates tabs and tables in it
    I want to access the tables (which I created in CheckList java) from my main window. I want to get the cell values of these tables
    thanks

  • Multiple fonts/colors of characters in JTextArea

    How can I have multi color / font characters in a JTextArea? If it can't be done in JTextArea, then how to do it in Swing?

    Use a JTextPane or a JEditorPane.

  • Having multiple font styles in a menuitem

    I am just trying to apply different "font styles" to a menu item available in a menubar.Font style include the three parameters which is passed to a font constructor.
    Font(java.lang.String name, int style, int size).
    say i want to have a Font("Script",Font.BOLD,20) to a particular menu item which i have created using JMenuBar.
    this is the prob. exactly.
    needed valuable suggestions.(if possible the code)
    regards,
    Ranjit Singh

    Here's a possible Swing solution, if I understand you correctly:
    import java.awt.*;
    import javax.swing.*;
    public class Test
         public static void main(String args[])
              new Test();
         public Test()
              JFrame f = new JFrame("Menu Item Test");
              JMenuBar menu = new JMenuBar();
              JMenu menuFile = new JMenu("File");
              menu.add(menuFile);
              JMenuItem menuItemOpen = new JMenuItem("Open");
              menuItemOpen.setFont(new Font("Serif", Font.BOLD, 16));
              JMenuItem menuItemClose = new JMenuItem("Close");
              menuItemClose.setFont(new Font("Monospaced", Font.ITALIC, 14));
              JMenuItem menuItemExit = new JMenuItem("Exit");
              menuItemExit.setFont(new Font("Dialog", Font.ITALIC, 12));
              menuFile.add(menuItemOpen);
              menuFile.add(menuItemClose);
              menuFile.add(menuItemExit);
              f.setJMenuBar(menu);
              f.pack();
              f.show();
    }

  • How to select rows in the inner JTable rendered in an outer JTable cell

    I have wrriten the following code for creating cell specific renderer - JTable rendered in a cell of a JTable.
    table=new JTable(data,columnNames)
    public TableCellRenderer getCellRenderer(int row, int column)
    if ((row == 0) && (column == 0))
    return new ColorRenderer();
    else if((row == 1) && (column == 0))
    return new ColorRenderer1();
    else
    return super.getCellRenderer(row, column);
    ColorRenderer and ColorRenderer1 are two inner classes, which implement TableCellRenderer to draw inner JTable on the outer JTable cell, having 2 rows and 1 column each.
    Now what is happening the above code keeps executing continously, that is the classes are being initialised continously, inner JTables are rendered (drawn) continously, and this makes the application slow after some time. It throws java.lang.OutOfMemoryException.
    WHY IS IT SO??? I can't understand where's the bug..
    Any advice please???
    Moreover i want selections in inner tables and not on outer table, how can this be possible.
    I am working on this since a long time but have not yet found a way out...

    With your help i have overcome the problem of continous repeatition.
    The major problem which I am facing is, in selecting rows in the inner rendered JTables.
    I have added listener on outer JTable which select rows on the outer JTable, hence the complete inner JTable which being treated as a row, gets selected.
    The thing is i need to select the rows of inner rendered JTables,not the outer JTable.
    How to go about it??
    I have even added listener to inner rendered JTables, but only first row of every table gets selected.
    Please help....
    Thanks in advance.

  • How do you set the font color for a specific entire row inside a JTable?

    How do you set the font color for a specific entire row inside a JTable?
    I want to change the font color for only a couple of rows inside a JTable.
    I've seen some ways to possibly do this with an individual cell.
    Clarification on changing the font color in an individual cell would be helpful too if
    there is no easy way to do this for a row.

    hai,
    Try out with this piece of code.Create your table and assign the renderer to each column in the table.
    CellColorRenderer m_CellColorRenderer = new CellColorRenderer();
    for(int i=0;i<your_JTable.getColumnCount();i++)
    your_JTable.getColumnModel().getColumn(i).setCellRenderer(m_CellColorRenderer);
    class CellColorRenderer extends JLabel implements TableCellRenderer
    CellColorRenderer()     
    setOpaque(true);     
    setHorizontalAlignment(LEFT);
    setVerticalAlignment(CENTER);
    setBackground(Color.white);
    setForeground(Color.black);
    protected void setValue(Object value)
         setText((value == null) ? "" : value.toString());
    public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected, boolean hasFocus, int row,int column)
         if(isSelected == true)
              setForeground(Color.red);
         else
              setForeground(Color.black);
         setValue(value);
         return this;
    regards,
    bala

  • How to fill color in a cell having particular string when using convertto-html

    Hello Scripters,
    I have downloaded AD health check script but I am wondering if the cell color be changed for a particular string. Like all the cells having text "Failed"..should be in red color.
    Here is the script-
    Function Getservicestatus($service, $server)
    $st = Get-service -computername $server | where-object { $_.name -eq $service }
    if($st)
    {$servicestatus= $st.status}
    else
    {$servicestatus = "Not found"}
    Return $servicestatus
    $Forest = [system.directoryservices.activedirectory.Forest]::GetCurrentForest()
    [string[]]$computername = $Forest.domains | ForEach-Object {$_.DomainControllers} | ForEach-Object {$_.Name}
    #Section -1
    $report= @()
    foreach ($server in $computername){
    $temp = "" | select server, pingstatus
    if ( Test-Connection -ComputerName $server -Count 1 -ErrorAction SilentlyContinue ) {
    $temp.pingstatus = "Pinging"
    else {
    $temp.pingstatus = "Not pinging"
    $temp.server = $server
    $report+=$temp
    $b = $report | select server, pingstatus | ConvertTo-HTML -Fragment -As Table -PreContent "<h2>Server Availability</h2>" | Out-String
    #Section - 2
    $report = @()
    foreach ($server in $computername){
    $temp = "" | select server, KDC, NtFrs, DFSR, netlogon, w32Time
    $temp.server = $server
    $temp.KDC = Getservicestatus -service "KDC" -server $server
    $temp.NtFrs = Getservicestatus -service "NtFrs" -server $server
    $temp.DFSR = Getservicestatus -service "DFSR" -server $server
    $temp.netlogon = Getservicestatus -service "netlogon" -server $server
    $temp.w32Time = Getservicestatus -service "w32Time" -server $server
    $report+=$temp
    $b+= $REPORT | select server, KDC, NtFrs, DFSR, netlogon, w32Time | ConvertTo-HTML -Fragment -As Table -PreContent "<h2>Service Status</h2>" | Out-String
    #Section - 3
    add-type -AssemblyName microsoft.visualbasic
    $strings = "microsoft.visualbasic.strings" -as [type]
    $report = @()
    foreach ($server in $computername){
    $temp = "" | select server, Netlogon, Advertising, Connectivity, Replication
    $temp.server = $server
    $svt = dcdiag /test:netlogons /s:$server
    $svt1 = dcdiag /test:Advertising /s:$server
    $svt2 = dcdiag /test:connectivity /s:$server
    $svt3 = dcdiag /test:Replications /s:$server
    if($strings::instr($svt, "passed test NetLogons")){$temp.Netlogon = "Passed"}
    else
    {$temp.Netlogon = "Failed"}
    if($strings::instr($svt1, "passed test Advertising")){$temp.Advertising = "Passed"}
    else
    {$temp.Advertising = "Failed"}
    if($strings::instr($svt2, "passed test Connectivity")){$temp.Connectivity = "Passed"}
    else
    {$temp.Connectivity = "Failed"}
    if($strings::instr($svt3, "passed test Replications")){$temp.Replication = "Passed"}
    else
    {$temp.Replication = "Failed"}
    $report+=$temp
    $b+= $REPORT | select server, Netlogon, Advertising, Connectivity, Replication | ConvertTo-HTML -Fragment -As Table -PreContent "<h2>DCDIAG Test</h2>" | Out-String
    #Section - 4
    $workfile = repadmin.exe /showrepl * /csv
    $results = ConvertFrom-Csv -InputObject $workfile | where {$_.'Number of Failures' -ge 1}
    #$results = $results | where {$_.'Number of Failures' -gt 1 }
    if ($results -ne $null ) {
    $results = $results | select "Source DSA", "Naming Context", "Destination DSA" ,"Number of Failures", "Last Failure Time", "Last Success Time", "Last Failure Status"
    $b+= $results | select "Source DSA", "Naming Context", "Destination DSA" ,"Number of Failures", "Last Failure Time", "Last Success Time", "Last Failure Status" | ConvertTo-HTML -Fragment -As Table -PreContent "<h2>Replication Status</h2>" | Out-String
    } else {
    $results = "There were no Replication Errors"
    $b+= $results | ConvertTo-HTML -Fragment -PreContent "<h2>Replication Status</h2>" | Out-String
    $head = @'
    <style>
    body { background-color:#dddddd;
    font-family:Tahoma;
    font-size:12pt; }
    td, th { border:1px Solid Black;
    border-collapse:collapse; }
    th { color:white;
    background-color:DarkGoldenRod; }
    table, tr, td, th { padding: 2px; margin: 0px }
    table { margin-left:50px; }
    </style>
    $s = ConvertTo-HTML -head $head -PostContent $b -Body "<h1>Active Directory Checklist</h1>" | Out-string
    $emailFrom = ""
    $emailTo = ""
    $smtpserver= ""
    $smtp=new-object Net.Mail.SmtpClient($smtpServer)
    $msg = new-object Net.Mail.MailMessage
    $msg.From = $emailFrom
    $msg.To.Add($emailTo)
    $msg.IsBodyHTML = $true
    $msg.subject="Active Directory Health Check Report From Dlhdc02"
    $msg.Body = $s
    $smtp.Send($msg)
    Like in the Ping Status (section - 1), I'd like all the cell having text "Not Pinging" should be in RED color.
    Also I am facing an issue in the (Section - 4). When the value of $Results is not null I am getting the desired output but when the value is null the text ""There were no Replication Errors""  is not getting displayed in
    the HTML body. Instead it comes as "*32" (32 is the number of letters in the text).
    Please help me in fixing this ....
    BR
    Himanshu
    MCTS|MCSE|MCSA:Messaging|CCNA

    Here are instructions on  ways to color cells based on content.
    http://tech-comments.blogspot.com/2012/07/powershell-dynamically-color-posh.html
    \_(ツ)_/

  • In Dreamweaver 6, I created a new fluid layout. I set up (4) DIVs. In the 3rd div, I changed the font color. The new color shows up on the website when viewed in my computer desktop, but, when viewed in a tablet and a cell phone, the color of the font doe

    In Dreamweaver 6, I created a new fluid layout. I set up (4) DIVs. In the 3rd div, I changed the font color. The new color shows up on the website when viewed in my computer desktop, but, when viewed in a tablet and a cell phone, the color of the font does not change. It's the same in Dreamweaver's Live view. It shows the new color on Desktop view and not in the cell phone or tablet view. Also, I changed the font itself in one of the DIVs and it shows up in the new font on the desktop view and website viewed thru the computer, but, not on the tablet or cell phone. Can someone please explain. I want to be able to change the fonts and colors for viewing in the tablet and cell phone, also. The fonts were all standard fonts. Sans-erif and Verdana and Arial were tried. Thanks.

    I will lock this discussion because of duplicate post.

  • JTable cell background color

    Hi All,
    I want to change the background color of the same of the cells in jTabel. How can I do that.
    Like, I want to keep 3 cells in red color. Another 2 cells in in green color, And keep other cell background color to default.
    Also I want to change the background color of the cell, with different selection in other component(like jTree, jList).
    Thank you,
    Avin Patel

    The same cell renderer is used by all cells of the table. You can't just set the background color of a cell once. You have to reset it every time the cell renderer is called (depending on which row/column you are being asked to render). Here is an example:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    public class TableCell extends JFrame
         JTable table;
         public TableCell()
              Object[][] data = { {"1", "A"}, {"2", "B"}, {"3", "C"}, {"4", "D"}  };
              String[] columnNames = {"Number","Letter"};
              DefaultTableModel model = new DefaultTableModel(data, columnNames);
              table = new JTable( model );
              //  Set default cell renderer
              TableCellRenderer renderer = new TestRenderer();
              table.setDefaultRenderer(Object.class, renderer);
              JScrollPane scrollPane = new JScrollPane( table );
              getContentPane().add( scrollPane );
         public static void main(String[] args)
              TableCell frame = new TableCell();
              frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
              frame.pack();
              frame.setVisible(true);
         class TestRenderer 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);
                   if (column == 0)
                        setHorizontalAlignment( LEFT );
                        setBackground( Color.blue );
                   else
                        setHorizontalAlignment( RIGHT );
                        setBackground( Color.green );
                   if (row == 1)
                        setBackground( Color.red );
                   return this;
    }

  • I failed to  color  JTable cells

    Hello,
    I'm trying to set the background color of JTable cells based on their content.
    For example if I edit a cell and put a R, I'd like to paint the background in Red.
    If I enter B, I want it becomes Blue, and so on.
    I tried to use a few examples found in various fora, but all failed.
    in addition, I do not understand why I should extend JLabel and not another Class ?
    Thanks for help.
    Here is a sample code (in which all cells should become red whatever is typed in).
    import javax.swing.table.*;
    import javax.swing.*;
    public class TableRenderer extends JLabel implements javax.swing.table.TableCellRenderer {
    public java.awt.Component getTableCellRendererComponent
        javax.swing.JTable table,
       Object value,
       boolean isSelected,
       boolean hasFocus,
       int row,
       int column
         setBackground(java.awt.Color.red);   // for the moment All cells will be red
         return this;
       mytable  = new javax.swing.JTable(5,5);
       TableColumn[] column = new TableColumn[5];
       TableColumnModel colModel = mytable.getColumnModel();
       for (int i=0; i <5 ;i++)
                    column[i] = colModel.getColumn( i );
                    column.setCellRenderer(new TableRenderer());

    Finally after many tries I found a good way to do what I wanted.
    If some people are interested, here is the code.
    import java.awt.*;
    import javax.swing.table.*;
    import javax.swing.*;
    class MyClass
    public class TableRenderer extends javax.swing.table.DefaultTableCellRenderer
    public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column)
         Component xx = super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
            String val = (String) table.getValueAt(row, column);
            if (gegeutil.Gegetools.isEmpty(val)) super.setBackground(Color.white);
            else if(val.equals("Tournament")) super.setBackground(Color.red);
              else if(val.equals("Class"))super.setBackground(Color.orange);
              else if(val == "")           super.setBackground(Color.white);
            else super.setBackground(Color.blue);          
                   return xx;
    public TableRenderer() {
         super();
    JTable table = new JTable(nbrow, nbcolumn);
    table.setDefaultRenderer(Object.class, new TableRenderer());
    }

  • Dynamic colored in theh JTable cell

    Hi, All
    I am using a JTable to display a Jlable in the JTable cells.
    The lable is divided to, say 10 , sub rectangles. I would liketo fill each rectangles with different bg colors depending on the data I received. I tried to use html in the JLable to control the color, however, it is very expensive cuase it involves a lot of string process and also it is difficult to control the dimension of those rectangles. Do anyone has any better idea to do this ?
    The cell is not necessary to be a JLable, by the way.
    Thanks in advance !

    I have never used JPanel as my table cell component, but u might want to implement performance improvements similiar to ones done in DefaultTableCellRenderer which extends JLabel.
    Here is the code extract from DefaultTableCellRenderer
    * The following methods are overridden as a performance measure to
    * to prune code-paths are often called in the case of renders
    * but which we know are unnecessary. Great care should be taken
    * when writing your own renderer to weigh the benefits and
    * drawbacks of overriding methods like these.
    * Overridden for performance reasons.
    * See the Implementation Note
    * for more information.
    public void validate() {}
    * Overridden for performance reasons.
    * See the Implementation Note
    * for more information.
    public void revalidate() {}
    * Overridden for performance reasons.
    * See the Implementation Note
    * for more information.
    public void repaint(long tm, int x, int y, int width, int height) {}
    * Overridden for performance reasons.
    * See the Implementation Note
    * for more information.
    public void repaint(Rectangle r) { }

  • Code to change JTable cell color not working

    My code below is supposed to change the color
    of a single JTable cell however its not working.
    Could anyone please tell me why?
    Here's the code:
    import java.awt.*;
    import javax.swing.table.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class ColorTable extends JTable {
    public ColorTable() {
    AbstractTableModel tableModel = new AbstractTableModel() {
    public Class getColumnClass(int column) { return Integer.class; }
    public int getColumnCount() { return 6; }
    public int getRowCount() { return 10;}
    public Object getValueAt(int row,int col) { return new Integer(row * col); }
    setModel(tableModel);
    setDefaultRenderer(Integer.class,new ColorRenderer(Color.cyan));
    this.setRowSelectionAllowed(false);
    this.setCellSelectionEnabled(true);
    addMouseListener(new MouseAdapter() {
    private ColorRenderer renderer;
    private JColorChooser chooser = new JColorChooser();
    public void mousePressed(MouseEvent e) {
    if(e.getModifiers() == MouseEvent.BUTTON3_MASK) {
    System.out.print("rowAtPoint(e.getPoint())=" +rowAtPoint(e.getPoint()));
    System.out.print( "columnAtPoint(e.getPoint()))=" + columnAtPoint(e.getPoint()));
    renderer = (ColorRenderer)getCellRenderer(rowAtPoint(e.getPoint()), columnAtPoint(e.getPoint()));
    // chooser.setColor(renderer.getColor());
    renderer.setColor(chooser.showDialog((Component)e.getSource(),"Choose Cell Color",chooser.getColor()));
    class ColorRenderer extends DefaultTableCellRenderer {
    private Color cellColor;
    public ColorRenderer(Color color) { cellColor = color; }
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    System.out.print("row= " + row + "\n");
    System.out.print("column= " + column + "\n");
    System.out.print("OBJECT VALUE=" + value.toString());
    //Component comp = super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
    super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
    if (isSelected) {
    setOpaque(true);
    setBackground(cellColor);
    else {
    setBackground(Color.white);
    setForeground(Color.black);
    return this;
    public void setColor(Color color)
    cellColor = color;
    ColorTable.this.repaint();
    public Color getColor() { return cellColor; }
    public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    ColorTable table = new ColorTable();
    panel.add(table);
    frame.getContentPane().add(panel);
    frame.setSize(500,250);
    frame.setVisible(true);

    My code below is supposed to change the color
    of a single JTable cell however its not working.
    Could anyone please tell me why?
    Here's the code:
    import java.awt.*;
    import javax.swing.table.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class ColorTable extends JTable {
    public ColorTable() {
    AbstractTableModel tableModel = new AbstractTableModel() {
    public Class getColumnClass(int column) { return Integer.class; }
    public int getColumnCount() { return 6; }
    public int getRowCount() { return 10;}
    public Object getValueAt(int row,int col) { return new Integer(row * col); }
    setModel(tableModel);
    setDefaultRenderer(Integer.class,new ColorRenderer(Color.cyan));
    this.setRowSelectionAllowed(false);
    this.setCellSelectionEnabled(true);
    addMouseListener(new MouseAdapter() {
    private ColorRenderer renderer;
    private JColorChooser chooser = new JColorChooser();
    public void mousePressed(MouseEvent e) {
    if(e.getModifiers() == MouseEvent.BUTTON3_MASK) {
    System.out.print("rowAtPoint(e.getPoint())=" +rowAtPoint(e.getPoint()));
    System.out.print( "columnAtPoint(e.getPoint()))=" + columnAtPoint(e.getPoint()));
    renderer = (ColorRenderer)getCellRenderer(rowAtPoint(e.getPoint()), columnAtPoint(e.getPoint()));
    // chooser.setColor(renderer.getColor());
    renderer.setColor(chooser.showDialog((Component)e.getSource(),"Choose Cell Color",chooser.getColor()));
    class ColorRenderer extends DefaultTableCellRenderer {
    private Color cellColor;
    public ColorRenderer(Color color) { cellColor = color; }
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    System.out.print("row= " + row + "\n");
    System.out.print("column= " + column + "\n");
    System.out.print("OBJECT VALUE=" + value.toString());
    //Component comp = super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
    super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
    if (isSelected) {
    setOpaque(true);
    setBackground(cellColor);
    else {
    setBackground(Color.white);
    setForeground(Color.black);
    return this;
    public void setColor(Color color)
    cellColor = color;
    ColorTable.this.repaint();
    public Color getColor() { return cellColor; }
    public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    ColorTable table = new ColorTable();
    panel.add(table);
    frame.getContentPane().add(panel);
    frame.setSize(500,250);
    frame.setVisible(true);

  • Multiple JButtons inside JTable cell - Dispatch mouse clicks

    Hi.
    I know this subject has already some discussions on the forum, but I can't seem to find anything that solves my problem.
    In my application, every JTable cell is a JPanel, that using a GridLayout, places vertically several JPanel's witch using an Overlay layout contains a JLabel and a JButton.
    As you can see, its a fairly complex cell...
    Unfortunately, because I use several JButtons in several locations inside a JTable cell, sometimes I can't get the mouse clicks to make through.
    This is my Table custom renderer:
    public class TimeTableRenderer implements TableCellRenderer {
         Border unselectedBorder = null;
         Border selectedBorder = null;
         public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                   boolean hasFocus, int row, int column) {
              if (value instanceof BlocoGrid) {
                   if (isSelected) {
                        if (selectedBorder == null)
                             selectedBorder = BorderFactory.createMatteBorder(2,2,2,2, table.getSelectionBackground());
                        ((BlocoGrid) value).setBorder(selectedBorder);
                   } else {
                        if (unselectedBorder == null)
                             unselectedBorder = BorderFactory.createMatteBorder(2,2,2,2, table.getBackground());
                        ((BlocoGrid) value).setBorder(unselectedBorder);
              return (Component) value;
    }and this is my custom editor (so clicks can get passed on):
    public class TimeTableEditor extends AbstractCellEditor implements TableCellEditor {
         private TimeTableRenderer render = null;
         public TimeTableEditor() {
              render = new TimeTableRenderer();
        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column) {
             if (value instanceof BlocoGrid) {
                  if (((BlocoGrid) value).barras.size() > 0) {
                       return render.getTableCellRendererComponent(table, value, isSelected, true, row, column);
             return null;
        public Object getCellEditorValue() {
            return null;
    }As you can see, both the renderer and editor return the same component that cames from the JTable model (all table values (components) only get instantiated once, so the same component is passed on to the renderer and editor).
    Is this the most correct way to get clicks to the cell component?
    Please check the screenshot below to see how the JButtons get placed inside the cell:
    http://img141.imageshack.us/my.php?image=calendarxo9.jpg
    If you need more info, please say so.
    Thanks.

    My mistake... It worked fine. The cell span code was malfunctioning. Thanks anyway.

  • Rendering multiple objects in JTable cells

    Hi,
    I wish to render in a single JTable cell (actually all cells in a Column) an ImageIcon and it's associated description string. The description string should be rendered below the ImageIcon, as it's caption. How can this be done? I assume a custom cell renderer, but the details are muddy at this point.
    thanks!
    JPL

    Thanks.
    I have looked atthe tutorial, but it focuses on a single object type per cell - I've got two different types (an ImageIcon and a String) that will occupuy a single cell.
    Taking a conceptual leap here, is it possible to create a new TableCellRenderer that houses a JPanel and GridLayout, and then place an ImageIcon and JLabel into that panel? I guess as long as things are JComponents, should I be OK with this approach?
    thanks,
    jpl

Maybe you are looking for