Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news-out.readnews.com!transit3.readnews.com!news-out.news.tds.net!newsreading01.news.tds.net!53ab2750!not-for-mail From: "clusardi2k" Subject: Hints Appreciated: How to fix a DefaultTableCellRenderer and Message-ID: <50182C7F.55833.calajapr@time.synchro.net> X-Comment-To: All Newsgroups: comp.lang.java.programmer X-FTN-AREA: COMP.LANG.JAVA.PROGRAMMER X-FTN-MSGID: 1:261/38 1c2ff2ed Content-Type: text/plain; charset=IBM437 Content-Transfer-Encoding: 8bit X-Gateway: time.synchro.net [Synchronet 3.16a-Win32 NewsLink 1.98] Lines: 123 Date: Tue, 31 Jul 2012 20:07:03 GMT NNTP-Posting-Host: 69.21.70.65 X-Complaints-To: news@tds.net X-Trace: newsreading01.news.tds.net 1343765223 69.21.70.65 (Tue, 31 Jul 2012 15:07:03 CDT) NNTP-Posting-Date: Tue, 31 Jul 2012 15:07:03 CDT Organization: tds.net Xref: csiph.com comp.lang.java.programmer:16864 From: "clusardi2k" From: clusardi2k@aol.com //How do I get the below project to use the tooltips and color the rows cyan //and gray. If I comment output the below indicated three lines I get column //coloring, but lose tooltips. Thank you, // Reference: // http://www.roseindia.net/java/example/java/swing/CustomCellRenderer.shtml package customcellrenderer; import javax.swing.*; import javax.swing.table.*; import java.awt.*; public class CustomCellRenderer { JTable table; TableColumn tcol; public static void main(String[] args) { new CustomCellRenderer(); } public static void StudentColTooltip(TableColumn OnLine_Col, String text) { DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(); renderer.setToolTipText(text); OnLine_Col.setCellRenderer(renderer); } public CustomCellRenderer() { JFrame frame = new JFrame("Creating a Custom Cell Reanderer!"); JPanel panel = new JPanel(); String data[][] = {{"Vinod","Computer","3"}, {"Rahul","History","2"}, {"Manoj","Biology","4"}, {"Sanjay","PSD","5"}}; String col [] = {"Name","Course","Year"}; DefaultTableModel model = new DefaultTableModel(data,col); table = new JTable(model); tcol = table.getColumnModel().getColumn(0); tcol.setCellRenderer(new CustomTableCellRenderer()); tcol = table.getColumnModel().getColumn(1); tcol.setCellRenderer(new CustomTableCellRenderer()); tcol = table.getColumnModel().getColumn(2); tcol.setCellRenderer(new CustomTableCellRenderer()); JTableHeader header = table.getTableHeader(); header.setBackground(Color.yellow); JScrollPane pane = new JScrollPane(table); panel.add(pane); frame.add(panel); frame.setSize(500,150); frame.setUndecorated(true); frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public class CustomTableCellRenderer extends DefaultTableCellRenderer { public Component getTableCellRendererComponent (JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) { Component cell = super.getTableCellRendererComponent( table, obj, isSelected, hasFocus, row, column); if ( isSelected ) { cell.setBackground(Color.green); } else { if ( row % 2 == 0 ) { cell.setBackground(Color.cyan); } else { cell.setBackground(Color.lightGray); } } //Commenting out the below 3 lines yields cyan and gray columns StudentColTooltip(table.getColumnModel().getColumn(0),"Student's Last Name"); StudentColTooltip(table.getColumnModel().getColumn(1),"Student's Major"); StudentColTooltip(table.getColumnModel().getColumn(2),"Year In School"); return cell; } } } -+- BBBS/Li6 v4.10 Dada-1 + Origin: Prism bbs (1:261/38) -+- 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/38) --- Synchronet 3.16a-Win32 NewsLink 1.98 Time Warp of the Future BBS - telnet://time.synchro.net:24