Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to color a cell in a JTable

Reply
Thread Tools

How to color a cell in a JTable

 
 
clusardi2k
Guest
Posts: n/a
 
      07-29-2012
From: "clusardi2k" <clusardi2k@1:261/38.remove-5bm-this>

From: "clusardi2k" <clusardi2k@1:261/38.remove-5ky-this>

From: "clusardi2k" <clusardi2k@1:261/38.remove-z1h-this>

From: "clusardi2k" <clusardi2k@1:261/38.remove-nwi-this>

From: "clusardi2k" <clusardi2k@1:261/38.remove-10ae-this>

From: http://www.velocityreviews.com/forums/(E-Mail Removed)

How would you use the idea in PROJECT 1 to color a cell in the PROJECT 2.

References:
(1) http://tips4java.wordpress.com/2010/...row-rendering/

(2) http://www.java-tips.org/java-se-tip...-and-filtering
-tables.html

Thank you,

//-------------- PROJECT 1 ---------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;

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

public class TableRowRenderingTip extends JPanel {
public TableRowRenderingTip()
{
Object[] columnNames = {"Type", "Company", "Shares", "Price",
"Boolean"};
Object[][] data =
{
{"Buy", "IBM", new Integer(1000), new Double(80.5),
Boolean.TRUE},
{"Sell", "Dell", new Integer(2000), new Double(6.25),
Boolean.FALSE},
{"Short Sell", "Apple", new Integer(3000), new
Double(7.35), Boolean.TRUE},
{"Buy", "MicroSoft", new Integer(4000), new
Double(27.50), Boolean.FALSE},
{"Short Sell", "Cisco", new Integer(5000), new
Double(20), Boolean.TRUE}
};

DefaultTableModel model = new DefaultTableModel(data,
columnNames)
{
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
};

JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Data", createData(model));
add( tabbedPane );
}

private JComponent createData(DefaultTableModel model)
{
JTable table = new JTable( model )
{
public Component prepareRenderer(TableCellRenderer
renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer,
row, column);

// Color row based on a cell value

if (!isRowSelected(row))
{
c.setBackground(getBackground());
int modelRow =
convertRowIndexToModel(row);
String type =
(String)getModel().getValueAt(modelRow, 0);
if ("Buy".equals(type))
c.setBackground(Color.GREEN);
if ("Sell".equals(type))
c.setBackground(Color.YELLOW);
}

return c;
}
};

table.setPreferredScrollableViewportSize(table.get PreferredSize
());
table.changeSelection(0, 0, false, false);
table.setAutoCreateRowSorter(true);
return new JScrollPane( table );
}

public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}

public static void createAndShowGUI()
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Table Row Rendering");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.add( new TableRowRenderingTip() );
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}



//-------------- PROJECT 2 ---------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.table.DefaultTableModel; import
javax.swing.table.TableModel; import javax.swing.table.TableRowSorter;

public class testtable extends javax.swing.JFrame {

/**
* Creates new form testtable
*/
public testtable() {
initComponents();

Runnable runner = new Runnable() {
public void run() {

Object rows[][] = {
{"AMZN", "Amazon", 41.28},
{"EBAY", "eBay", 41.57},
{"GOOG", "Google", 388.33},
{"MSFT", "Microsoft", 26.56},
{"NOK", "Nokia Corp", 17.13},
{"ORCL", "Oracle Corp.", 12.52},
{"SUNW", "Sun Microsystems", 3.86},
{"TWX", "Time Warner", 17.66},
{"VOD", "Vodafone Group", 26.02},
{"YHOO", "Yahoo!", 37.69}
};
String columns[] = {"Symbol", "Name", "Price"};
TableModel model =
new DefaultTableModel(rows, columns) {
public Class getColumnClass(int column) {
Class returnValue;
if ((column >= 0) && (column < getColumnCount())) {
returnValue = getValueAt(0, column).getClass();
} else {
returnValue = Object.class;
}
return returnValue;
}
};

RowSorter<TableModel> sorter =
new TableRowSorter<TableModel>(model);
jTable1.setModel(model);
jTable1.setRowSorter(sorter);
}
};
EventQueue.invokeLater(runner);
}
/**
* This method is called from within the constructor to initialize the
form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);

javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADIN
G)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 375,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(13, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADIN
G)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG,
layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE,
Short.MAX_VALUE)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 275,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting
code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tu...okandfeel/plaf.
html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClass Name());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(testtable.class .getName()).log(j
ava.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(testtable.class .getName()).log(j
ava.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(testtable.class .getName()).log(j
ava.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(testtable.class .getName()).log(j
ava.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new testtable().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}

-+- BBBS/Li6 v4.10 Dada-1
+ Origin: Prism bbs (1:261/3
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

-+- BBBS/Li6 v4.10 Dada-1
+ Origin: Prism bbs (1:261/3
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

-+- BBBS/Li6 v4.10 Dada-1
+ Origin: Prism bbs (1:261/3
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

-+- BBBS/Li6 v4.10 Dada-1
+ Origin: Prism bbs (1:261/3
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

-+- BBBS/Li6 v4.10 Dada-1
+ Origin: Prism bbs (1:261/3
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/3
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
Reply With Quote
 
 
 
 
clusardi2k
Guest
Posts: n/a
 
      07-29-2012
To: clusardi2k
From: "clusardi2k" <clusardi2k@1:261/38.remove-5bm-this>

To: clusardi2k
From: "clusardi2k" <clusardi2k@1:261/38.remove-5ky-this>

To: clusardi2k
From: "clusardi2k" <clusardi2k@1:261/38.remove-z1h-this>

To: clusardi2k
From: "clusardi2k" <clusardi2k@1:261/38.remove-nwi-this>

To: clusardi2k
From: "clusardi2k" <clusardi2k@1:261/38.remove-10ae-this>

To: clusardi2k
From: (E-Mail Removed)

The below project will paint all cells red!

//-------------- PROJECT 2 ---------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.table.DefaultTableModel; import
javax.swing.table.TableModel; import javax.swing.table.TableRowSorter;

class CustomRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column)
{
Component c = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
c.setBackground(new java.awt.Color(255, 72, 72));
return c;
}
}

public class testtable extends javax.swing.JFrame {

/**
* Creates new form testtable
*/
public testtable() {
initComponents();

Runnable runner = new Runnable() {
public void run() {

Object rows[][] = {
{"AMZN", "Amazon", 41.28},
{"EBAY", "eBay", 41.57},
{"GOOG", "Google", 388.33},
{"MSFT", "Microsoft", 26.56},
{"NOK", "Nokia Corp", 17.13},
{"ORCL", "Oracle Corp.", 12.52},
{"SUNW", "Sun Microsystems", 3.86},
{"TWX", "Time Warner", 17.66},
{"VOD", "Vodafone Group", 26.02},
{"YHOO", "Yahoo!", 37.69}
};
String columns[] = {"Symbol", "Name", "Price"};
TableModel model =
new DefaultTableModel(rows, columns) {
public Class getColumnClass(int column) {
Class returnValue;
if ((column >= 0) && (column < getColumnCount())) {
returnValue = getValueAt(0, column).getClass();
} else {
returnValue = Object.class;
}
return returnValue;
}
};

jTable1.setDefaultRenderer(String.class, new CustomRenderer());

jTable1.setModel(model);

jTable1.setRowSorter(sorter); }
};
EventQueue.invokeLater(runner);
}
/**
* This method is called from within the constructor to initialize the
form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);

javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADIN
G)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 375,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(13, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADIN
G)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG,
layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE,
Short.MAX_VALUE)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 275,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting
code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tu...okandfeel/plaf.
html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClass Name());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(testtable.class .getName()).log(j
ava.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(testtable.class .getName()).log(j
ava.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(testtable.class .getName()).log(j
ava.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(testtable.class .getName()).log(j
ava.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new testtable().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}

-+- BBBS/Li6 v4.10 Dada-1
+ Origin: Prism bbs (1:261/3
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

-+- BBBS/Li6 v4.10 Dada-1
+ Origin: Prism bbs (1:261/3
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

-+- BBBS/Li6 v4.10 Dada-1
+ Origin: Prism bbs (1:261/3
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

-+- BBBS/Li6 v4.10 Dada-1
+ Origin: Prism bbs (1:261/3
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

-+- BBBS/Li6 v4.10 Dada-1
+ Origin: Prism bbs (1:261/3
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/3
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      07-31-2012
To: clusardi2k
From: "markspace" <markspace@1:261/38.remove-rf4-this>

To: clusardi2k
From: "markspace" <markspace@1:261/38.remove-pvc-this>

To: clusardi2k
From: "markspace" <markspace@1:261/38.remove-5bm-this>

To: clusardi2k
From: "markspace" <markspace@1:261/38.remove-5ky-this>

To: clusardi2k
From: "markspace" <markspace@1:261/38.remove-z1h-this>

To: clusardi2k
From: markspace <-@.>

On 7/27/2012 10:07 AM, (E-Mail Removed) wrote:
> How would you use the idea in PROJECT 1 to color a cell in the PROJECT 2.



Do your own homework.

Seriously; asking us stuff like this is just rude.

-+- BBBS/Li6 v4.10 Dada-1
+ Origin: Prism bbs (1:261/3
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

-+- BBBS/Li6 v4.10 Dada-1
+ Origin: Prism bbs (1:261/3
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

-+- BBBS/Li6 v4.10 Dada-1
+ Origin: Prism bbs (1:261/3
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

-+- BBBS/Li6 v4.10 Dada-1
+ Origin: Prism bbs (1:261/3
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

-+- BBBS/Li6 v4.10 Dada-1
+ Origin: Prism bbs (1:261/3
-+- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/3
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Changing the Background Color for One Cell in a JTable Hal Vaughan Java 5 10-28-2008 06:54 PM
Putting a JTable inside a JTable cell? Tivo Escobar Java 1 04-12-2007 11:09 AM
Individual cell color using JTable jpcaballerost@gmail.com Java 2 03-28-2006 02:03 PM
How to set and highlight text in JTable cell on selection and/or invoke cell editor directly. news.rcn.com Java 1 07-12-2004 10:22 PM



Advertisments