JTable设置行背景颜色及实现打印功能的示例代码精品资料.doc
JTable设置行背景颜色及实现打印功能的示例代码2008-01-14 15:24之前JAVA图形化编程一直用得很少,最近应朋友需要理事了两个.java文件,用于实现JTable设置行背景颜色及实现打印功能,相应示例代码如下:JAVA文件一,PlanetTable.classimport java.awt.BorderLayout;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;/* 本类为JTable一个简单应用,实现了JTable的行颜色设置及表格的打印功能* */public class PlanetTable public static void main(String args) JFrame frame = new PlanetTableFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);class PlanetTableFrame extends JFrame public PlanetTableFrame() / 用于控制每一行颜色的数组 String color = "H", "A", "F", "E", "W" ; setTitle("PlanetTable"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); / 定义JTable,实例成自己扩展的JTable类,并传入用于设定颜色的数组 final JTable table = new StyleTable(cells, columnNames, color); / 下面这行代码可实现相邻两行颜色交替的效果,注意与上一行的区别/ final JTable table = new StyleTable(cells, columnNames); add(new JScrollPane(table), BorderLayout.CENTER); JButton printButton = new JButton("Print"); printButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent event) try table.print(); catch (java.awt.print.PrinterException e) e.printStackTrace(); ); JPanel buttonPanel = new JPanel(); buttonPanel.add(printButton); add(buttonPanel, BorderLayout.SOUTH);private Object cells = "Mercury", new Double(2440.0), new Integer(0), new Boolean(false), Color.yellow , "Venus", new Double(60520.0), new Integer(0), new Boolean(false), Color.yellow , "Earth", new Double(6378.0), new Integer(1), new Boolean(false), Color.blue , "Mars", new Double(3397.0), new Integer(2), new Boolean(false), Color.red , "Jupiter", new Double(71492.0), new Integer(16), new Boolean(false), Color.orange ;private String columnNames = "Planet", "Radius", "Moons", "Gaseous", "Color" ;private static final int DEFAULT_WIDTH = 400;private static final int DEFAULT_HEIGHT = 200;下面的类文件是实现颜色控制的核心所在,对于需要有背景颜色设定的表格只需在实例化JTable对象处,实例成StyleTable即可,注意前面绿色背景的文字。JAVA文件二,StyleTable .classimport java.awt.Color;import java.awt.Component;import javax.swing.JTable;import javax.swing.table.DefaultTableCellRenderer;import javax.swing.table.TableColumn;import javax.swing.table.TableColumnModel;/* 本类实现了对JTable颜色的控制,提供了两套方案: * 1.实现了表格行两种颜色交替的效果 * 2.实现了用一个控制颜色的字符串数组来设置所对应行的颜色* * author Sidney* version 1.0 (2008-1-14)*/public class StyleTable extends JTable /* 用于设定行颜色的数组*/private String color = null;public StyleTable() super();public StyleTable(Object rowData, Object columnNames) super(rowData, columnNames); paintRow();public StyleTable(Object rowData, Object columnNames, String color) super(rowData, columnNames); this.color = color; paintColorRow();/* 根据color数组中相应字符串所表示的颜色来设置某行的颜色,注意,JTable中可以对列进行整体操作* 而无法对行进行整体操作,故设置行颜色实际上是逐列地设置该行所有单元格的颜色。*/public void paintRow() TableColumnModel tcm = this.getColumnModel(); for (int i = 0, n = tcm.getColumnCount(); i < n; i+) TableColumn tc = tcm.getColumn(i); tc.setCellRenderer(new RowRenderer(); public void paintColorRow() TableColumnModel tcm = this.getColumnModel(); for (int i = 0, n = tcm.getColumnCount(); i < n; i+) TableColumn tc = tcm.getColumn(i); tc.setCellRenderer(new RowColorRenderer(); /* 定义内部类,用于控制单元格颜色,每两行颜色相间,本类中定义为蓝色和绿色。* * author Sidney* */private class RowRenderer extends DefaultTableCellRenderer public Component getTableCellRendererComponent(JTable t, Object value, boolean isSelected, boolean hasFocus, int row, int column) /设置奇偶行的背景色,可在此根据需要进行修改 if (row % 2 = 0) setBackground(Color.BLUE); else setBackground(Color.GREEN); return super.getTableCellRendererComponent(t, value, isSelected, hasFocus, row, column); /* 定义内部类,可根据一个指定字符串数组来设置对应行的背景色* * author Sidney* */private class RowColorRenderer extends DefaultTableCellRenderer public Component getTableCellRendererComponent(JTable t, Object value, boolean isSelected, boolean hasFocus, int row, int column) /分支判断条件可根据需要进行修改 if (colorrow.trim().equals("E") setBackground(Color.RED); else if (colorrow.trim().equals("H") setBackground(Color.CYAN); else if (colorrow.trim().equals("A") setBackground(Color.BLUE); else if (colorrow.trim().equals("F") setBackground(Color.ORANGE); else setBackground(Color.WHITE); return super.getTableCellRendererComponent(t, value, isSelected, hasFocus, row, column); 我是刚学Swing的,请大哥们帮帮小弟!很急,我已经郁闷3天了! JTable当我选中一行时,就改变这一行的背景色!问题点数:50 回复次数:5 修改 删除 举报 引用 回复 加为好友 发送私信 在线聊天· diggywang ·· 等级:发表于:2007-04-26 21:21:571楼 得分:13用tablecellrenderer修改 删除 举报 引用 回复 加为好友 发送私信 在线聊天· chuan122345 ·· 等级:发表于:2007-04-27 12:29:052楼 得分:13public static void makeFace(JTable table) try DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) if (row % 2 = 0) setBackground(Color.white); /设置奇数行底色 else if (row % 2 = 1) setBackground(new Color(206, 231, 255); /设置偶数行底色 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); ; for (int i = 0; i < table.getColumnCount(); i+) table.getColumn(table.getColumnName(i).setCellRenderer(tcr); catch (Exception ex) ex.printStackTrace(); 修改 删除 举报 引用 回复 加为好友 发送私信 在线聊天· chuan122345 ·· 等级:发表于:2007-04-27 12:31:463楼 得分:0主要是写个REANDER,我以前做过这效果 publicclass JTableCellRenderer extends JPanel implements TableCellRenderer private JComponent component = null; privatestaticfinallongserialVersionUID = 4814418916385836965L; /*透明?*/ privatebooleanisOpaque = true; public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) /*透明?*/ isOpaque = table.isOpaque(); /*获得TableModel*/ MyTableModel myTableModel = (MyTableModel)table.getModel(); boolean canSelected = true; List canSelectedList = myTableModel.getCanBeSelectedModel(); if(canSelectedList != null) if(row < canSelectedList.size() if(!(Boolean)canSelectedList.get(row).booleanValue() canSelected = false; else canSelected = false; /*设定用于显示Cell的panel*/ JPanel cellPanel = new JPanel(); /*绘制Cell*/ getCell(table,cellPanel,row,canSelected,isSelected); int colorChangeFlag = row/myTableModel.getDiv(); /*是否透明*/ if(isOpaque) if(colorChangeFlag % 2 = 0) cellPanel.setBackground(myTableModel.getRowBackgroundColor(); else cellPanel.setBackground(myTableModel.getFirstRowBackgroundColor(); /*设定是否透明 */ cellPanel.setOpaque(isOpaque); if(isSelected) cellPanel.setBackground(table.getSelectionBackground(); rePaintComponents(); / MainRun.middlePanel.repaint(); return cellPanel; privatevoid getCell(JTable table,JPanel cellPanel,int row, boolean canSelected,boolean isSelected) /*设定对齐方式*/ cellPanel.setLayout(new FlowLayout(0,0,0); /*获得TableModel*/ MyTableModel myTableModel = (MyTableModel)table.getModel(); /*获得列数*/ int col = myTableModel.getCol(); for(int i = 0; i < col ; i+ ) /*获得某个单元格*/ Object cellObject = myTableModel.getValueAt(row,i); /*/ JComponent tempLabel = null; /*String*/ if(cellObject instanceof String) tempLabel = new JLabel(String)cellObject); if(canSelected) Color cellColor = myTableModel.getColor(row,i); tempLabel.setForeground(cellColor); else tempLabel.setForeground(Color.gray); if(isSelected) tempLabel.setForeground(table.getSelectionForeground(); /*Icon*/ elseif(cellObject instanceof ImageIcon) tempLabel = new JLabel(ImageIcon)cellObject); /*TextBox*/ elseif(cellObject instanceof JTextField) tempLabel = new JTextField(); else tempLabel = new JLabel(); if(cellObject instanceof JTextField) else /*对齐方式*/ switch(myTableModel.getCellLayout(i) /*左*/ case 0: (JLabel)tempLabel). setHorizontalAlignment(JLabel.LEFT); break; /*中*/ case 1: (JLabel)tempLabel). setHorizontalAlignment(JLabel.CENTER); break; /*右*/ case 2: (JLabel)tempLabel). setHorizontalAlignment(JLabel.RIGHT); break; /*宽度设定*/ int width = myTableModel.getWidth(row,i); /*size设定*/ tempLabel.setPreferredSize (new Dimension(width,table.getRowHeight(); /*字体设定*/ tempLabel.setFont(table.getFont(); /*设定是否透明*/ cellPanel.add(tempLabel); protectedvoid rePaintComponents() if (component != null) for (int i = 0; i < component.length; i+) if (componenti != null) componenti.repaint(); public JComponent getComponent() returncomponent; publicvoid setComponent(JComponent c