当前位置: 代码迷 >> Eclipse >> jtable cell renderer有关问题
  详细解决方案

jtable cell renderer有关问题

热度:807   发布时间:2016-04-23 14:11:21.0
jtable cell renderer问题
我建了一个类


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import cn.edu.nju.software.datamodel.News;

public class NewsTableRenderer extends JPanel implements TableCellRenderer{
/**

*/
private static final long serialVersionUID = 1L;
private String title;
private String date;
public JLabel viewport;
private String url;
private Image img;
JLabel d;
public NewsTableRenderer(){
super();
this.setLayout(new BorderLayout());
viewport=new JLabel();
d=new JLabel();
this.setBorder(BorderFactory.createLineBorder(Color.black));
viewport.addMouseListener(a1);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isselected, boolean hasfocused, int row, int column) {
// TODO Auto-generated method stub
News news=(News)value;
this.title=news.getTitle();
this.date=news.getDate();
String tmp="<HTML><h3><U>"+this.title+"</U></h3></HTML>";
viewport.setText(tmp);
d.setText(date);
this.add(viewport,"Center");
this.add(d,"East");
if(isselected){
this.setBackground(Color.blue);
}
return this;
}

MouseListener a1=new MouseAdapter(){
public void mouseClicked(MouseEvent e){
new JOptionPane().showMessageDialog(null, "clicked");
}
};
}

这个cellrenderer的问题纠结了我好久。鼠标点击完全无反应



if(isselected){
this.setBackground(Color.blue);
}


添加这行背景也没有变。

时间监听也完全没反应。

是我那里写错了吗? 代码很简单,希望大家看一下。

------解决方案--------------------
你原来的代码已经能够实现一部分你想要的功能,至少背景色变色没问题,给你贴的代码除了侦听部分都实现,侦听没实现是因为单击事件从来没有传进去,这个还要再想想。
我怀疑是你自己实现了Render不会用,所以之一觉得有问题,具体使用这个Render也贴出来了,在下一楼
Java code
import java.awt.BorderLayout;import java.awt.Color;import java.awt.Component;import java.awt.Cursor;import java.awt.FlowLayout;import java.awt.Graphics;import java.awt.Image;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.util.Date;import javax.swing.*;import javax.swing.table.TableCellRenderer;//import cn.edu.nju.software.datamodel.News;public class NewsTableRenderer extends JPanel implements TableCellRenderer{    /**     *     */    private static final long serialVersionUID = 1L;    private String title;    private String date;    public JLabel viewport;    private String url;    private Image img;    JLabel d;//    JButton btn;        public NewsTableRenderer(){        super();        this.setLayout(new BorderLayout());        viewport=new JLabel("init");        d=new JLabel("init");//        btn=new JButton("click");        this.add(viewport,BorderLayout.CENTER);        this.add(d,BorderLayout.EAST);//        this.add(btn,BorderLayout.WEST);        this.setBorder(BorderFactory.createLineBorder(Color.black));        MouseListener a1=new MouseAdapter(){            @Override            public void mouseClicked(MouseEvent e){                new JOptionPane().showMessageDialog(null, "clicked");            }        };        viewport.addMouseListener(a1);   //侦听不起作用,事件从来没有传过去//        btn.addMouseListener(a1);    }        @Override    public Component getTableCellRendererComponent(JTable table, Object value,                                                   boolean isselected, boolean hasfocused, int row, int column) {// TODO Auto-generated method stub//News news=(News)value;//this.title=news.getTitle();//this.date=news.getDate();        this.title="title";        this.date=new Date().toString();        String tmp="<HTML><h3><U>"+this.title+"</U></h3></HTML>";        viewport.setText(tmp);//        viewport.setText("test");        d.setText(date);        if(isselected){            this.setBackground(Color.blue);        }        return this;    }}
  相关解决方案