为JTable添加JScrollPane给corner设置颜色
时间:2011-01-17 BlogJava kissjava
有时候为了使JTable配合软件风格配置不同的颜色,当table的有表头以及添加了JScrollPane有ScollBar的时候会出现有个右上角有个区域的颜色是和底色是一样的,如下图1:
图一
为了使得那个灰色区域和table header 的颜色一致,一时居然不知道如何是好,赶快查jdk的API文档发现这个图二:
图二
知道了这个corner component就好办了,那就添加如下:
jScrollPane1.getCorner(JScrollPane.UPPER_RIGHT_CORNER).setBackground(Color.red),
却又发现jScrollPane1.getCorner(JScrollPane.UPPER_RIGHT_CORNER)为NULL,原来要给他添加一个corner component,
那就再添加如下代码:
JLabel jb = new JLabel();
jb.setBackground(Color.black);
jScrollPane1.setCorner(JScrollPane.UPPER_RIGHT_CORNER,jb);
葎JTable耶紗JScrollPane公corner譜崔冲弼(2)
扮寂:2011-01-17 BlogJava kissjava
徽頁宥狛霞編窟?椎倖corner議冲弼卆隼短嗤子弼?囈弾??臥孀彿創誼繍lable.setOpaque(true)
購噐緩圭隈議盾瞥泌和?
泌惚葎 true?夸乎怏周紙崙凪円順坪議侭嗤?殆。倦夸乎怏周辛嬬音紙崙凪蝶乂賜侭嗤?殆?貫遇塋俯凪和中議?殆邑篇竃栖。
斤噐 JComponent 遇冱?緩奉來議潮範峙頁 false。徽頁斤噐寄謹方炎彈議 JComponent 徨窃?泌 JButton 才 JTree??緩奉來議潮範峙頁才翌鉱?購議。
宸劔祥丼惚泌夕眉?
夕眉
音狛輝扮音岑祇宸倖setOpaque(true)?遇頁喘阻総翌匯倖曳熟運議圭隈?泌?
JLabel jb = new JLabel();
//jb.setOpaque(true);
jb.setBackground(Color.black);
jb.setBorder(BorderFactory.createLineBorder(Color.red,20));
jScrollPane1.setCorner(JScrollPane.UPPER_RIGHT_CORNER,jb);
霞編議旗鷹泌?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
public class KJTable extends JFrame{
private JTable table;
private KJTableCellRenderer renderer = new KJTableCellRenderer();
private BorderLayout borderLayout = new BorderLayout();
private JScrollPane jScrollPane1 = new JScrollPane();
public KJTable(){
this.setLayout(borderLayout);
table = new JTable();
table.setRowHeight(20);
Object rows[][] ={
{ "藍苧", "嶄忽", 44 },
{ "勁苧", "嶄忽", 25 },
{ "孕徨霜", "廉包", 1234 },
{ "蝦荷", "臼虜", 2112 },
{ "Bill Gates", "胆忽", 45 },
{ "Mike", "哂忽", 33 },
{ "藍苧", "嶄忽", 44 },
{ "勁苧&
|