解决方案 »
- jmf录像问题——如何设置录像的长宽比
- svnkit commit时is not a working copy
- 如何判断输入字符串其中有空格
- 如何禁用Jtabbedpane上的其它面板,只设置其中的一个有效?
- 有几个链接 一个链接默认已经点击 档点击其他某个链接 这个链接回到原来的状态怎么做?
- 为什么显示'javac' 不是内部或外部命令,也不是可运行的程序
- 请教!
- 怎么用applet通过画一个车辆的父类来构造car和truck子类?
- 关于Jbuilder一个很鸟的问题,不知道该怎么解决???
- 请问weblogic怎么解决中文乱码问题
- 请教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;
}
}