JComboBox Tooltip
Posted by blogmeister on
July 3, 2011
You wonder why there may be a need to have a tooltip inside a JComboBox. What if your item is very long like the image shown below?
This where tooltips come in handy unless you plan to have your JComboBox display items in multi-line if they are very long but that won’t look good. A tooltip is the best solution and the way to go about this is to assign a renderer to the JComboBox that does this.
This class does the trick. You only need to instantiate this and assign it to the JComboBox by calling the setRenderer() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class ComboBoxToolTipRenderer extends BasicComboBoxRenderer { @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { if (isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); if (-1 < index) { list.setToolTipText(list.getSelectedValue().toString()); } } else { setBackground(list.getBackground()); setForeground(list.getForeground()); } setFont(list.getFont()); setText((value == null) ? "" : value.toString()); return this; } } |
JProgressBar Cell Renderer
Posted by blogmeister on
March 24, 2010
The code below is a TableCellRenderer that shows a JProgressBar that can be used as a cell renderer in a JTable cell.
If you check the overridden getTableCellRendererComponent() method, the value should be a percentage value so that the JProgressBar can adjust its visual status according to the percentage.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class JProgressBarTableCellLabelRenderer implements TableCellRenderer { private JProgressBar jpb; public JProgressBarTableCellLabelRenderer(){ jpb = new JProgressBar(); jpb.setMinimum(0); jpb.setValue(1); jpb.setMaximum(100); } @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (value != null) { int v = Integer.parseInt(value.toString()); jpb.setValue(v); } return jpb; } } |









