51905

Get tooltip from JComboBox Renderer

Question:

I have a ComboBox renderer that extend JPanel and has two labels. In here i need to show tool-tip when mouse goes to <strong>iconLabel</strong> only. If mouse is in <strong>labelItem</strong> tool-tip should not show.

import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.ListCellRenderer; import javax.swing.plaf.metal.MetalIconFactory; public class MyItemRenderer extends JPanel implements ListCellRenderer { private JLabel labelItem = new JLabel(); private JLabel iconLabel = new JLabel(); public MyItemRenderer() { setLayout(new GridBagLayout()); GridBagConstraints constraints = new GridBagConstraints(); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.weightx = 1.0; constraints.insets = new Insets(2, 2, 2, 0); labelItem.setOpaque(true); labelItem.setHorizontalAlignment(JLabel.LEFT); iconLabel.setOpaque(true); iconLabel.setHorizontalAlignment(JLabel.RIGHT); iconLabel.setPreferredSize(new Dimension(14, 16)); iconLabel.setMaximumSize(new Dimension(14, 16)); add(labelItem, constraints); GridBagConstraints constraints1 = new GridBagConstraints(); constraints1.weightx = 0; constraints1.insets = new Insets(0, 0, 0, 2); add(iconLabel, constraints1); setBackground(Color.LIGHT_GRAY); } @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { String item = (String) value; labelItem.setText(item); // set icon iconLabel.setIcon(MetalIconFactory.getFileChooserDetailViewIcon()); if (isSelected) { labelItem.setBackground(Color.BLUE); labelItem.setForeground(Color.YELLOW); iconLabel.setBackground(Color.BLUE); iconLabel.setForeground(Color.YELLOW); } else { labelItem.setForeground(Color.BLACK); labelItem.setBackground(Color.LIGHT_GRAY); iconLabel.setForeground(Color.BLACK); iconLabel.setBackground(Color.LIGHT_GRAY); } return this; } }

My tool-tip only need come when mouse over the icon area.That mean user need to get tool-tip only they want.Please help.

Answer1:

Override the getToolTipText(MouseEvent event) method of the MyItemRenderer, translate the MouseEvent into the coordinate space of the iconLabel (or what ever component you want). If the MouseEvent falls within the bounds of the component, then return a different result

Something like...

@Override public String getToolTipText(MouseEvent event) { String tooltip = super.getToolTipText(event); Point p = SwingUtilities.convertPoint(this, event.getPoint(), iconLabel); if (iconLabel.contains(p)) { tooltip = "I'm a banana"; } return tooltip; }

Recommend

  • How to get multiline for a Jlist text? [closed]
  • how to get data from mysql to jList?
  • Painting problems with custom ScrollPane
  • Links does not work in tag when content has .com inside
  • Select List of Column Names / Aliases from Custom Sub-Query
  • Declaring a scalar inside an if statement?
  • How can I extract lines of text from a file?
  • installing modules when using perlbrew
  • How to get Getopt::Long + pod2usage working?
  • Save SQL to YAML as is
  • add an element to a node, if it doesnot exist
  • Error(5,3): PLS-00103: Encountered the symbol “BEGIN” when expecting one of the following: language
  • How can I open a Windows CMD window for Perl and run a command?
  • Is there anything wrong with using requires after output starts printing?
  • Google Maps getMap returns null
  • Cannot invoke my method on the array type int[]
  • Regarding starting the threads on a condition
  • How to disable all widgets inside Panel or inside Composite?
  • how to avoid repetitive constructor in children
  • Highlight and Bold text in JTextPane
  • How to access EntityManager inside Entity class in EJB3
  • how to do an event when i swipe from fragment to the other
  • Can I display google adwords (AdView) in javafx on android
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • output of program is not same as passed argument
  • Validaiting emails with Net.Mail MailAddress
  • Perl system calls when running as another user using sudo
  • sending/ receiving email in Java
  • Javascript + PHP Encryption with pidCrypt
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • JTable with a ScrollPane misbehaving
  • Getting Messege Twice Using IMvxMessenger
  • Java static initializers and reflection
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?