Maawii
Maawii

Reputation: 21

JTable Tooltip from sql?

I have been bugging with this for 2 days now. I have a JTable that is being generated with SQL from two tables.

The thing is I now need to get a Mouseover tooltip to work, with specific information from one of the tables. And its not working.

public class Ausgabe extends JPanel {

    public JPanel panel1;
    private Point hintCell;

    Main j;

    MA ali = new MA();

    public Ausgabe(Main j) {
        this.j = j;

    }

    public void createDefault (){
        panel1 = new JPanel();
        panel1.setLayout( null );

        JLabel lblDieseMitarbeiterSind = new JLabel("Diese Mitarbeiter sind Heute abwesend.");
        lblDieseMitarbeiterSind.setBounds(10, 11, 239, 14);
        panel1.add(lblDieseMitarbeiterSind);

        panel1.add(getHeute());
    }
    public JTable getHeute(){

            DBconnect verbinden = new DBconnect();
            verbinden.erstelleVerbindung();

            JTable Habwesend=new JTable();
            Habwesend.setBounds(10, 30, 200, 400);
            Habwesend.setEnabled(false);
            DefaultTableModel dm=new DefaultTableModel();

            try {
            ResultSet rs= verbinden.sqlStatement.executeQuery("select Vor, Nach from MA_Tabelle where MA_ID in (select distinct MA_ID from AB_Spanne where(Date() >= Start and Date() <= Ende)) ");
            ResultSetMetaData rsmd=rs.getMetaData();
            //Coding to get columns-
            int cols=rsmd.getColumnCount();
            String c[]=new String[cols];
            for(int i=0;i<cols;i++){
                c[i]=rsmd.getColumnName(i+1);
                for (int k = 0; k < c.length; k++) {
                    dm.addColumn(c[k]);
                }


            }
            Object row[]=new Object[cols];
            while(rs.next()){
                for(int i=0;i<cols;i++){
                        row[i]=rs.getString(i+1);
                    }
                dm.addRow(row);

            }

            Habwesend.setModel(dm);
            Habwesend.setToolTipText(getToolTipText((MouseEvent) verbinden.sqlStatement.executeQuery("select Start, Ende from AB_Spanne where MA_ID = "+rs)));
            verbinden.schliesseVerbindung();


            }catch(SQLException e){
                System.out.println("this failed");
                System.out.print(e);
            }
            return Habwesend;
        }

    public String result() {
        // TODO Auto-generated method stub
        return null;
    }
}

hope someone can help me.

Thanks in advance.

Upvotes: 0

Views: 120

Answers (2)

Robin
Robin

Reputation: 36611

The JTable respects the tooltip text set on the renderer. No need to override the getToolTipText of the JTable.

Use a renderer in which you specify the tooltip. I however highly discourage you to run SQL queries in the renderer as this will slow down your application. Typically, database queries are done on a background thread, and the results are fed into your model, at which point you update the view. Consult the Concurrency in Swing tutorial for more information

Upvotes: 1

Audrius Meškauskas
Audrius Meškauskas

Reputation: 21748

In your code, you set the single tooltip text for the whole table so it is the same for all cells.

Instead, use the custom table class derived from JTable and override the method

@Override
public String getToolTipText(MouseEvent e) {
    Point where = e.getPoint();
    int column = columnAtPoint(where);
    int row = rowAtPoint(where);

    // now run your SQL to get the tooltip text
    ...
    return tooltip;
}

Upvotes: 1

Related Questions