Question

Can someone explain me something? How can I create a yellow hint/notification with Java swt?

I want that popup a small notice when I move over a cell in a table. Something like this:

enter image description here

This is my Java-Code:

protected void checkAction() throws Exception {

    //Erstellen einer neuen Shell
    Shell shell = new Shell();
    shell.setSize(280, 300);
    shell.setText("Testtabelle");

    //Erstellen einer neuen Tabelle
    final Table table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
    table.setLinesVisible(true);
    table.setHeaderVisible(true);

    //Einlesen der Überschriften und Vergabe der Namen
    String[] titles = {"Element", "Stage", "Type", "Generate-User", "Change-User" }; 
    for (int i = 0; i < titles.length; i++) {
      TableColumn column = new TableColumn(table, SWT.NONE);
      column.setText(titles[i]);
    }

    // Inhalte hinzufügen
    final int count = 4;
    for (int i = 0; i < count; i++) {
      TableItem item = new TableItem(table, SWT.NONE);
      item.setText(0, "Test "+i);
      item.setText(1, ""+(i+1));
      item.setText(2, "Testtype");
      item.setText(3, "562910");
      item.setText(4, "423424");
    }

   // Tabelle und Shell Packen  
   for (int i = 0; i < titles.length; i++) {
      table.getColumn(i).pack();
    }
    table.setSize(table.computeSize(SWT.DEFAULT, 200));
    shell.pack();

    //MouseListener

    table.addListener(SWT.MouseHover, new Listener() {
        public void handleEvent(Event event) {
          Rectangle clientArea = table.getClientArea();
          Point pt = new Point(event.x, event.y);
          int index = table.getTopIndex();
      //    System.out.println("TopIndex: "+index);
          while (index < table.getItemCount()) {
            boolean visible = false;
            TableItem item = table.getItem(index);
            for (int i = 0; i < (table.getItemCount()+1); i++) {
              Rectangle rect = item.getBounds(i);
              if (rect.contains(pt)) {
                String selected = table.getItem(index).getText(i);

                if (selected.equals("562910")){
                    String real = "Jonas";
                    System.out.println(real);

                }
                else{
                    System.out.println("Ausgewählt: "+selected);    
                }

              }
              if (!visible && rect.intersects(clientArea)) {
                visible = true;
              }
            } 
            if (!visible)
              return;
            index++;
          }
        }
      });


    // Shell öffnen
      shell.open();



}   
Was it helpful?

Solution

Using just SWT use the code in SWT Snippet 125.

If you can use JFace then ColumnViewerToolTipSupport is rather easier.

OTHER TIPS

You should start using jface TableViewer. To create table column, you will use TableViewerColumn.

For each column, add CellLabelProvider which has getToolTipXXX() methods.

TableViewerColumn.setLabelProvider(CellLabelProvider)

here is good article to start with

http://www.vogella.com/tutorials/EclipseJFaceTable/article.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top