Skip to content

Commit

Permalink
Add renderer so that truncated names have full tooltips of arbitrary …
Browse files Browse the repository at this point in the history
…length
  • Loading branch information
aymanhab committed Jan 11, 2025
1 parent 8984c00 commit 856521e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.opensim.view.nodes;

import java.awt.Component;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

/**
*
* @author ayman
*/
public class ListCellRendererWithTooltip extends BasicComboBoxRenderer {
JComboBox combox;
public ListCellRendererWithTooltip(JComboBox comboBox){
this.combox = comboBox;
}

@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component comp = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); //To change body of generated methods, choose Tools | Template
((JLabel) comp).setToolTipText(value.toString());
return comp;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import org.opensim.modeling.AbstractPathPoint;
import org.opensim.modeling.ArrayPathPoint;
import org.opensim.modeling.Component;
Expand Down Expand Up @@ -651,23 +653,8 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
AttachmentFrameChosen(((javax.swing.JComboBox)evt.getSource()), num);
}
});
comboBox.addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
adjustDropdownWidth(e);
//To change body of generated methods, choose Tools | Templates.
}

@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
adjustDropdownWidth(e); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void popupMenuCanceled(PopupMenuEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
comboBox.setRenderer(new ListCellRendererWithTooltip(comboBox));

AttachmentsPanel.add(comboBox);
x += 150;

Expand Down Expand Up @@ -861,7 +848,8 @@ private void maybeShowPopup(MouseEvent e) {
for (int i = 0; i < selectedObjects.size(); i++)
updateAttachmentSelections(selectedObjects.get(i), true);
}
public void adjustDropdownWidth(PopupMenuEvent e) {

public void adjustDropdownWidth(PopupMenuEvent e) {
JComboBox comboBox = (JComboBox) e.getSource();
Object accessibleChild = comboBox.getUI().getAccessibleChild(comboBox, 0);
if (!(accessibleChild instanceof JPopupMenu)) return;
Expand Down

0 comments on commit 856521e

Please sign in to comment.