解决方案 »
- 谢谢各位大侠。。。初学者的烦恼啊
- 一个sql语句 一个dom4j! 求解决!!!!!!
- Resultse.updateRow()更新数据库失败 恳请各位大虾帮下忙
- synchronized 方法 和 synchronized代码块 竞争的是同一把锁吗?
- 想学JavaBean,看什么书入门比较好?
- 请问在J2SE下有没有类似XLGrid的数据表格控件?
- 想在jb中调用一个.class文件,而该文件又没有所属的包,它是独立的,该怎么办???-------急,在线等!!!
- 如何断开数据库连接
- 怎么在电脑中快速打开MyEclipse软件
- jbuilder6.o个人版 的注册码!!!急急急
- 请教linux下使用svn命令的几个问题
- 帮我改改代码,谢谢!
看看这个。
import java.awt.BorderLayout;
import java.awt.Component;import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;public class SampleTable extends JPanel
{
JTable table; public SampleTable()
{
setLayout(new BorderLayout());
table = new JTable(new SampleTableModel());
table.setCellSelectionEnabled(true); table.getColumn("Button").setCellRenderer(new MyTableCellRenderer()); JScrollPane pane = new JScrollPane(table);
add(pane, BorderLayout.CENTER);
} public static void main(String[] args)
{
JFrame f = new JFrame("Sample Table");
SampleTable sample = new SampleTable();
f.getContentPane().add(sample, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 200);
f.setVisible(true);
}
}class MyTableCellRenderer extends JButton implements TableCellRenderer
{
public MyTableCellRenderer()
{
super();
} public String SetImgpath(int row)
{
if(row % 2 == 0 )
{
return "D:\\pause.png";
}
else
{
return"D:\\play.png";
}
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column)
{
// this.setText((value == null) ? "" : ((JButton) value).getText());
//
// if (isSelected)
// {
// int choose = JOptionPane.showConfirmDialog(null, "第" + row + "行"
// + "第" + column + "列" + " JButton Clicked", "Test",
// JOptionPane.OK_CANCEL_OPTION);
// if (choose == JOptionPane.OK_OPTION)
// {
// super.setForeground(Color.red);
// super.setBackground(table.getSelectionBackground());
// }
// else
// {
// super.setForeground(Color.blue);
// super.setBackground(table.getSelectionBackground());
// }
// }// JLabel jlbl = new JLabel("String text", new ImageIcon("D:\\pause.png"), 0);
JLabel jlbl = new JLabel("Name " + String.valueOf(row) , new ImageIcon(this.SetImgpath(row)), 0);
return jlbl;
}
}class SampleTableModel extends AbstractTableModel
{
Object data[][] = {
{ "Jon", "Apple", null },
}; Object names[] = { "NAME", "FURUTS", "Button" }; public int getRowCount()
{
return data.length;
} public int getColumnCount()
{
return names.length;
} public Object getValueAt(int row, int col)
{
return data[row][col];
} public String getColumnName(int colIndex)
{
return names[colIndex].toString();
} public Class getColumnClass(int c)
{
return getValueAt(0, c).getClass();
} public boolean isCellEditable(int row, int col)
{
return getColumnClass(col) == String.class;
} public void setValueAt(Object aValue, int row, int col)
{
data[row][col] = aValue;
}
}
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class SampleTable extends JPanel{
JTable table;
public SampleTable(){
setLayout(new BorderLayout());
table = new JTable(new SampleTableModel());
table.setCellSelectionEnabled(true);
table.getColumn("Button").setCellRenderer(new MyTableCellRenderer());
JScrollPane pane = new JScrollPane(table);
add(pane, BorderLayout.CENTER);
}
public static void main(String[] args){
JFrame f= new JFrame("Sample Table");
SampleTable sample = new SampleTable();
f.getContentPane().add(sample, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 200);
f.setVisible(true);
}
}
class MyTableCellRenderer extends JButton implements TableCellRenderer{
public MyTableCellRenderer() {
super();
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
this.setText((value == null)? "": ((JButton)value).getText());
if(isSelected){
int choose = JOptionPane.showConfirmDialog(null,"第" +row +"行" +"第" + column+"列" +" JButton Clicked","Test",JOptionPane.OK_CANCEL_OPTION);
if(choose == JOptionPane.OK_OPTION){
super.setForeground(Color.red);
super.setBackground(table.getSelectionBackground());
}else{
super.setForeground(Color.blue);
super.setBackground(table.getSelectionBackground());
}
super.setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
}else{
super.setForeground(table.getForeground());
super.setBackground(table.getBackground());
}
if(hasFocus){
super.setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
}else{
super.setForeground(table.getForeground());
super.setBackground(table.getBackground());
}
return this;
}
}
class SampleTableModel extends AbstractTableModel{
Object data[][] = {
{ "Jon","Apple", new JButton("Apple")},
{ "Marry","Pine", new JButton("Pine")},
{ "Ben","Peach", new JButton("Peach")},
{ "Mike","Orange", new JButton("Orange")},
{ "Patty","Apple", new JButton("Apple")},
{ "Jimmy","Lemon", new JButton("Lemon")},
{ "Jon","Apple", new JButton("Apple")},
{ "Marry","Pine", new JButton("Pine")},
{ "Ben","Peach", new JButton("Peach")},
{ "Mike","Orange", new JButton("Orange")},
{ "Patty","Apple", new JButton("Apple")},
{ "Jimmy","Lemon", new JButton("Lemon")},
};
Object names[] = {"NAME", "FURUTS", "Button"};
public int getRowCount(){
return data.length;
}
public int getColumnCount(){
return names.length;
}
public Object getValueAt(int row, int col){
return data[row][col];
}
public String getColumnName(int colIndex){
return names[colIndex].toString();
}
public Class getColumnClass(int c){
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col){
return getColumnClass(col) == String.class;
}
public void setValueAt(Object aValue, int row, int col){
data[row][col] = aValue;
}
}