How to reject all the prompts in the table when cell editing starts?

I have included tooltips in JTable by overriding the JComponent method that inherits JTable:

public String getToolTipText(MouseEvent e) { ... } 

Now suppose that the user is hanging over a cell, a tooltip appears, and then (a) he starts editing the cell, I want to forcefully reject the tooltip.

Currently, the tooltip just hangs until the value that I specified using ToolTipManager # setDismissDelay expires. The tooltip can sometimes hide the view of the cell being edited, so I want to reject it at the moment when any cell on the table goes into edit mode.

I tried the following approach (this is pseudo code)

 public String getToolTipText(MouseEvent e) { if(table-is-editing) return null; else return a-string-to-display-in-the-tooltip; } 

Of course, this only action DOES NOT show the table tooltips ALREADY in edit mode. I knew this would not work, but it was more like a shot in the dark.

+4
source share
5 answers

You can show / hide the tooltip using the following code:

 //Action toolTipAction = component.getActionMap().get("postTip"); Action toolTipAction = component.getActionMap().get("hideTip"); if (toolTipAction != null) { ActionEvent ae = new ActionEvent(component, ActionEvent.ACTION_PERFORMED, ""); toolTipAction.actionPerformed( ae ); } 

You can probably override the prepareCellEditor(...) JTable method to add this code, and it should hide any hint before displaying the editor.

Edit:

In response to Kleopatra's comment, I will add the following to make sure that Action is added to ActionMap:

 table.getInputMap().put(KeyStroke.getKeyStroke("pressed F2"), "dummy"); ToolTipManager.sharedInstance().registerComponent( table ); 
+6
source

I was going to comment “something is wrong with yours,” but I remembered a precedent when a tooltip could not be hidden when editing started :-)

Some facts:

  • tooltips are hidden on mouseExit and on focusLost component that is registered in ToolTipManager
  • when you start editing, and the editing component receives focus, so the tooltip hides automatically
  • by default, JTable does not give focus to the editing component, this editing begins with input into the cell (as opposed to double-clicking or F2): in this case, the focusLost function does not start, and therefore the tooltip is not hidden
  • ToolTipManager really sets up a hide that can be reused (as @camickr mentioned). But - this action is set only if the component has an inputMap of type WHEN_FOCUSED. This does not apply to JTable (all of its bindings are in WHEN_ANCESTOR)

Therefore, to implement the required behavior, several settings are required, the code fragment is given below (pay attention to yourself: implement it in SwingX :-)

 JTable table = new JTable(new AncientSwingTeam()) { { // force the TooltipManager to install the hide action getInputMap().put(KeyStroke.getKeyStroke("ctrl A"), "just some dummy binding in the focused InputMap"); ToolTipManager.sharedInstance().registerComponent(this); } @Override public boolean editCellAt(int row, int column, EventObject e) { boolean editing = super.editCellAt(row, column, e); if (editing && hasFocus()) { hideToolTip(); } return editing; } private void hideToolTip() { Action action = getActionMap().get("hideTip"); if (action != null) { action.actionPerformed(new ActionEvent( this, ActionEvent.ACTION_PERFORMED, "myName")); } } }; 
+6
source

Check out this JTable tutorial . In particular, this is webstart . There are two editable columns with tooltips - "Sport" and "Vegetarian" work fine. Do you use any custom cell renderers?

+1
source

This worked for me and seems simpler than using the action:

ToolTipManager.sharedInstance().mouseExited(new MouseEvent(myJframe, 0, 0, 0, 0, 0, 0, 0, 0, false, 0));

This seems to hide any tooltip shown inside the specified JFrame.

+1
source

With JDK 1.6, when a cell tooltip is displayed, the user cannot expand the range of selected lines using the keyboard. The solution presented above worked fine for this problem. Here is the code:

 public class ToolTipTable extends JTable { /** * Constructor */ public ToolTipTable() { super(); // force the TooltipManager to install the hide action getInputMap().put(KeyStroke.getKeyStroke("ctrl A"), "just some dummy binding in the focused InputMap"); ToolTipManager.sharedInstance().registerComponent(this); //hide the tool tip when row selection changes this.getSelectionModel().addListSelectionListener( new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { hideToolTip(); } }); } /** * Make the cell tool tip show the contents of the cell. (Useful if the * cell contents are wider than the column.) */ @Override public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { Component c = super.prepareRenderer(renderer, row, column); if (c instanceof JComponent) { JComponent jc = (JComponent) c; Object valueObj = getValueAt(row, column); if (valueObj != null) { jc.setToolTipText(getValueAt(row, column).toString()); } } return c; } /** * */ private void hideToolTip() { Action action = getActionMap().get("hideTip"); if (action != null) { action.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "myName")); } } } 
0
source

Source: https://habr.com/ru/post/1391767/


All Articles