English 中文(简体)
消除习俗单元中的制造ToolTip()
原标题:Overriding createToolTip() in a custom CellRenderer

I m试图为JTable的具体一栏获取一个定制工具。 我已经创建了一个单元管理员(我已经成功地改变其他细胞特性):

private class CustomCellRenderer extends DefaultTableCellRenderer
{
    private static final long   serialVersionUID    = 1L;

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column)
    {
        JComponent c = (JComponent) super.getTableCellRendererComponent(table, value,
                isSelected, hasFocus, row, column);

        if (value != null)
        {
            if(column == 1 && value instanceof Date)
            {
                final DateFormat df = new SimpleDateFormat("h:mm aa");
                table.setValueAt(df.format(value), row, column);
            }
            else if(column == 2)
            {
                c.setToolTipText((String) value);
            }
            else if(column == 4)
            {
                final Mail m = main.selectedPage.messages.get(row);
                JCheckBox checkBox;

                if((Boolean) value)
                {
                    checkBox = new JCheckBox()
                    {
                        @Override
                        public JToolTip createToolTip()
                        {
                            System.out.println("Passed");
                            return new ImageToolTip(m.getImage());
                        }
                    };
                    checkBox.setToolTipText(m.attachName);
                }
                else 
                    checkBox = new JCheckBox();

                checkBox.setSelected(((Boolean)value).booleanValue());
                c = checkBox;
            }
        }
        else
        {
            c.setToolTipText(null);
        }
        return c;
    }
}

当我凌驾于任何其他JComponent制造Tooltip(Tooltip)的方法时,所有公司都在Renderer之外进行罚款。

checkBox = new JCheckBox()
{
    @Override
    public JToolTip createToolTip()
    {
        System.out.println("Passed");
        return new ImageToolTip(m.getImage());
    }
};

从我可以知道的角度来看,该工具是在其他地方创建的,因为“计划”从未印刷过。 The checkBox.setToolTipText(m.attachName); only results in aail ToolTip with that String.

我先发现有人“,但我要说,我完全理解唯一的解决办法。 是否需要扩大可使用和压倒性<代码>。 如果是的话,我不敢确定获得正确的(地雷)工具。

请免除我自封的 we。 预收

EDIT:

感谢Robin, 我能够把根据JTable s getToolTipText(MouseEvent e)

messageTable = new JTable()
{
    @Override
    public JToolTip createToolTip()
    {
        Point p = getMousePosition();

        // 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);

            if (component instanceof JCheckBox) 
            {
                Image img = main.selectedPage.messages.get(hitRowIndex).getImage();
                if(((JCheckBox) component).isSelected())
                    return new ImageToolTip(img);
            }
        }
        return super.createToolTip();
    }
}
最佳回答

在实施过程中,可以看到<代码>可使用代码>不使用你的工具。 <代码>JTable将确实使用由发送人返还的部件,但将要求其提供工具文本。 因此,只有采用习惯工具文本才能发挥作用,如果你坚持“条码”( 执行。 仅是<代码>JTable源代码相关部分的快速复印件,以说明:

    if (component instanceof JComponent) {
        // Convert the event to the renderer s coordinate system
        Rectangle cellRect = getCellRect(hitRowIndex, hitColumnIndex, false);
        p.translate(-cellRect.x, -cellRect.y);
        MouseEvent newEvent = new MouseEvent(component, event.getID(),
                                  event.getWhen(), event.getModifiers(),
                                  p.x, p.y,
                                  event.getXOnScreen(),
                                  event.getYOnScreen(),
                                  event.getClickCount(),
                                  event.isPopupTrigger(),
                                  MouseEvent.NOBUTTON);

        tip = ((JComponent)component).getToolTipText(newEvent);
    }

是的,你必须超越<代码>。 如果你真的想把图像作为检查盒子的工具,则使用JTable方法。

顺便提一句:你的教程法典有令人厌恶的行为。 The

final DateFormat df = new SimpleDateFormat("h:mm aa");
table.setValueAt(df.format(value), row, column);

似乎不正确。 页: 1 电话

JLabel label = new JLabel();//a field in your renderer
//in the getTableCellRendererComponent method
label.setText( df.format( value ) );
return label;

or something similar. The renderer should not adjust the table values, but create an appropriate component to visualize the data. In this case a JLabel seems sufficient. And as Stanislav noticed in the comments, you should not constantly create new components. That defeats the purpose of the renderer which was introduced to avoid creating new components for each row/column combination. Note that the method is called getTableCellRendererComponent (emphasis on get) and not createTableCellRendererComponent

问题回答

你们无法在囚室内设置检查箱的工具。 实际上,当你试图改变这一构成部分时,该构成部分就没有存在。 这只是一个形象。 你们需要为你们的JTable创造工具

private void tableMouseMoved(java.awt.event.MouseEvent evt) {
    String toolTipText;
    int row = table.rowAtPoint(evt.getPoint());
    int column = table.columnAtPoint(evt.getPoint());

    if (row >= 0) {
        Object o = table.getValueAt(row, column);
        if (column == YourTableModel.COLUMN_INDEX_WITH_CHECKBOX) {
            Boolean value = (Boolean) o;
            if (value == Boolean.TRUE) {
                toolTipText = "Tooltip text for true value";
            } else {
                toolTipText = "Tooltip text for false value";
            }  
        } 
    }
}

你们需要为MouseEvent注册听众:

javax.swing.JTable table = new JTable();
table.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
    public void mouseMoved(java.awt.event.MouseEvent evt) {
        tableMouseMoved(evt);
    }
});




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签