我有一個帶有自定義單元格渲染器的JTable。我添加了一個帶有多個JLabel的面板給JTable。問題是,當鼠標懸停在每個JLabel上時,我似乎無法獲得每個工具的提示。我不想設置jpanel工具提示,我想顯示我在jpanel內的3個JLabel上設置的工具提示。JTable中JPanel中的JLabel工具提示不起作用
繼承人我的JTable初始化:
public static JTable users = new JTable(model) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
@Override
public Class<? extends Object> getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
@Override
public String getToolTipText(MouseEvent event) {
String tip = null;
Point p = event.getPoint();
// Locate the renderer under the event location
int hitColumnIndex = columnAtPoint(p);
int hitRowIndex = rowAtPoint(p);
if (hitColumnIndex != -1 && hitRowIndex != -1) {
TableCellRenderer renderer = getCellRenderer(hitRowIndex, hitColumnIndex);
Component component = prepareRenderer(renderer, hitRowIndex, hitColumnIndex);
Rectangle cellRect = getCellRect(hitRowIndex, hitColumnIndex, false);
component.setBounds(cellRect);
component.validate();
component.doLayout();
p.translate(-cellRect.x, -cellRect.y);
Component comp = component.getComponentAt(p);
if (comp instanceof JComponent) {
return ((JComponent) comp).getToolTipText();
}
}
// No tip from the renderer get our own tip
if (tip == null) {
tip = getToolTipText();
}
return tip;
}
};
和我的自定義單元格渲染代碼:
public class TransparentRenderer extends DefaultTableCellRenderer implements TableCellRenderer {
public JPanel p = new JPanel();
........
// This method is called each time a cell in a column
// using this renderer needs to be rendered.
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
....
// Now lets color in the table accordingly..
if (value.toString().equals(gmeLobby.user)) {
JLabel l = new JLabel(value.toString());
l.setForeground(Color.YELLOW);
p.add(l);
//setHorizontalAlignment(SwingConstants.LEFT);
} else {
// If username is not yours
//setHorizontalAlignment(SwingConstants.LEFT);
if(value instanceof ImageIcon)
{
final JLabel l = new JLabel();
l.setIcon((ImageIcon) value);
l.setAlignmentY(0.85f);
l.setToolTipText(((ImageIcon) value).getDescription());
p.add(l);
if(vColIndex==0)
{
final JLabel w = new JLabel();
final JLabel d = new JLabel();
try {
w.setIcon(gmeImages.gmeImages("globe"));
w.setToolTipText(((ImageIcon) value).getDescription());
d.setIcon(gmeImages.gmeImages("dnds"));
d.setToolTipText(((ImageIcon) value).getDescription());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p.add(d);
p.add(w);
setPosition(w, 5, 0);
setPosition(d, 15, 2);
}
setPosition(l, 0, 0);
}else{
JLabel l = new JLabel(value.toString());
l.setForeground(Color.WHITE);
p.add(l);
}
return p;
}
if (isSelected) {
setBackground(new Color(rsbc.getRed() - 100, rsbc.getGreen(), rsbc.getBlue()));
} else {
setBackground(bgu);
}
if (hasFocus) {
// this cell is the anchor and the table has the focus
p.setBackground(new Color(rsbc.getRed() - 100, rsbc.getGreen(), rsbc.getBlue()));
}
// Since the renderer is a component, return itself
return this;
}
任何幫助將是巨大的。工具提示的JTable上的當前覆蓋似乎只是獲得了添加的第一個組件的工具提示。
感謝
編輯:
謝謝大家誰是幫助,同時使SSCCE爲安德烈評論,我似乎得到問題的固定,而不是確定錯誤是什麼exacly,我認爲這可能與我初始化imageicon變量的方式有關。
反正在每個圖像上的提示小區2個imageicons一個示例程序如下:
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.MatteBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.text.BadLocationException;
@SuppressWarnings("serial")
public class design extends JFrame{
// Create the JTable users list and set editable to false
public static DefaultTableModel model = new DefaultTableModel();
public static JTable users = new JTable(model) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
@Override
public Class<? extends Object> getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
@Override
public String getToolTipText(MouseEvent event) {
String tip = null;
Point p = event.getPoint();
// Locate the renderer under the event location
int hitColumnIndex = columnAtPoint(p);
int hitRowIndex = rowAtPoint(p);
if (hitColumnIndex != -1 && hitRowIndex != -1) {
TableCellRenderer renderer = getCellRenderer(hitRowIndex, hitColumnIndex);
Component component = prepareRenderer(renderer, hitRowIndex, hitColumnIndex);
Rectangle cellRect = getCellRect(hitRowIndex, hitColumnIndex, false);
component.setBounds(cellRect);
component.validate();
component.doLayout();
p.translate(-cellRect.x, -cellRect.y);
Component comp = component.getComponentAt(p);
if (comp instanceof JComponent) {
return ((JComponent) comp).getToolTipText();
}
}
// No tip from the renderer get our own tip
if (tip == null) {
tip = getToolTipText();
}
return tip;
}
};
/**
* @param args
* @throws Exception
*/
public design() throws Exception
{
initialize();
setSize(400, 400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
add(users);
setVisible(true);
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
new design();
}
public static void initialize() throws Exception
{
model.addColumn("icons");
model.addColumn("text");
users.setRowHeight(40);
TableColumn col = users.getColumnModel().getColumn(0);
col.setCellRenderer(new TransparentRenderer());
TableColumn col2 = users.getColumnModel().getColumn(1);
col2.setCellRenderer(new TransparentRenderer());
model.addRow(new Object[] { new ImageIcon(), "text here" });
}
}
class TransparentRenderer extends JPanel implements TableCellRenderer {
private static final long serialVersionUID = 1L;
public JPanel p = new JPanel();
private static SpringLayout spring = new SpringLayout();
public TransparentRenderer() throws Exception, BadLocationException {p.setLayout(spring);}
// This method is called each time a cell in a column
// using this renderer needs to be rendered.
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
if(value instanceof ImageIcon)
{
if(vColIndex==0)
{
JLabel w = new JLabel();
JLabel d = new JLabel();
try {
ImageIcon icon1 = new ImageIcon();
extracted("http://fxtrade.oanda.com/wandacache/star_icon-c10fffd09c7a7548f329f56e446f3cfe5463558b.png"
, icon1, "Tooltip for icon 1");
ImageIcon icon2 = new ImageIcon();
extracted("http://hotels.online.com.sg/DB/icon/star_icon2.gif"
, icon2, "Tooltip for icon 2");
w.setIcon(icon1);
w.setToolTipText("ToolTip 1");
d.setIcon(icon2);
d.setToolTipText("ToolTip 2");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p.add(d);
p.add(w);
setPosition(d, 5, 2);
setPosition(w, 5, 0);
}
return p;
}else{
add(new JLabel(value.toString()));
}
return this;
}
public void setPosition(JComponent c, int w, int n)
{
spring.putConstraint(SpringLayout.WEST, c, w, SpringLayout.WEST, p);
spring.putConstraint(SpringLayout.NORTH, c, n, SpringLayout.NORTH, p);
}
public void extracted(String link, ImageIcon icon, String desc) throws Exception
{
BufferedImage image = ImageIO.read(new URL(link));
icon.setImage(image);
if(desc.equals("")){} else{ icon.setDescription(desc);}
}
}
感謝所有誰幫助:)
爲了更快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2013-03-14 11:11:58