已创建表时如何修改表getToolTipText

时间:2018-11-23 22:24:32

标签: java swing

我想为每个单元格添加一个工具提示,但是当已经用JTable创建了表getToolTipText时,我遇到了麻烦。table = new JTable(model); 我发现也许使用getTableCellRendererComponent可以工作,但是我找不到方法。这是我所拥有的一个例子。

package com.QA;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;


public class cellrenderer {
    private JScrollPane getContent() {
        DefaultTableModel model = new DefaultTableModel();
        model.setColumnIdentifiers(new Object[] { "Name", "Age", "State" });
        model.addRow(new Object[] { "Peter",   29, "Florida" });
        model.addRow(new Object[] { "Gabriel", 32, "Oregon"  });
        model.addRow(new Object[] { "Hans",    27, "Texas"   });
        model.addRow(new Object[] { "Serge",   30, "Ohio"    });
        JTable table = new JTable(model);
        TableColumnModel colModel = table.getColumnModel();
        for(int j = 0; j < colModel.getColumnCount(); j++)
            colModel.getColumn(j).setCellRenderer(new RowRenderer());
        return new JScrollPane(table);
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(new cellrenderer().getContent());
        f.setSize(400,140);
        f.setLocation(200,200);
        f.setVisible(true);
    }
}

class RowRenderer extends DefaultTableCellRenderer {
    public Component getTableCellRendererComponent(JTable table,
                                                   Object value,
                                                   boolean isSelected,
                                                   boolean hasFocus,
                                                   int row, int column) {
        super.getTableCellRendererComponent(table, value, isSelected,
                hasFocus, row, column);
        ****TRYING TO SET TOOL TIP FOR EVERYCELL***


        return this;
    }
}

这是我想要的工作代码,但仅当创建表时JTable table = new JTable(data,columns){....

package com.QA;


import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import java.awt.event.MouseEvent;


public class Stooltipxcell extends JFrame
{
    public Stooltipxcell()
    {
        //headers for the table
        String[] columns = new String[] {
                "Id", "Name", "Hourly Rate", "Part Time"
        };

        //actual data for the table in a 2d array
        Object[][] data = new Object[][] {
                {1, "John", 40.0, false },
                {2, "Rambo", 70.0, false },
                {3, "Zorro", 60.0, true },
        };
        //create table with data
        JTable table = new JTable(data, columns){
            //Implement table cell tool tips.
            public String getToolTipText(MouseEvent e) {
                String tip = null;
                java.awt.Point p = e.getPoint();
                int rowIndex = rowAtPoint(p);
                int colIndex = columnAtPoint(p);

                try {
                    tip = getValueAt(rowIndex, colIndex).toString();
                } catch (RuntimeException e1) {
                    //catch null pointer exception if mouse is over an empty line
                }

                return tip;
            }};

        //add the table to the frame
        this.add(new JScrollPane(table));

        this.setTitle("Table Example");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Stooltipxcell();
            }
        });
    }
}

0 个答案:

没有答案