config panel: fix combobox displayed size

The size was being computed from the enum names during the getPreferredSize() call due to not having the renderer set yet. Additionally the old prototype display value was not being used during this as it was not set yet, either.
This commit is contained in:
Adam
2021-01-13 21:16:39 -05:00
parent 92013816b8
commit 524ebee67d
2 changed files with 11 additions and 7 deletions

View File

@@ -487,15 +487,19 @@ class ConfigPanel extends PluginPanel
if (cid.getType().isEnum())
{
Class<? extends Enum> type = (Class<? extends Enum>) cid.getType();
JComboBox box = new JComboBox(type.getEnumConstants());
JComboBox<Enum<?>> box = new JComboBox<Enum<?>>(type.getEnumConstants()); // NOPMD: UseDiamondOperator
// set renderer prior to calling box.getPreferredSize(), since it will invoke the renderer
// to build components for each combobox element in order to compute the display size of the
// combobox
box.setRenderer(new ComboBoxListRenderer<>());
box.setPreferredSize(new Dimension(box.getPreferredSize().width, 25));
box.setRenderer(new ComboBoxListRenderer());
box.setForeground(Color.WHITE);
box.setFocusable(false);
box.setPrototypeDisplayValue("XXXXXXXX"); //sorry but this is the way to keep the size of the combobox in check.
try
{
Enum selectedItem = Enum.valueOf(type, configManager.getConfiguration(cd.getGroup().value(), cid.getItem().keyName()));
Enum<?> selectedItem = Enum.valueOf(type, configManager.getConfiguration(cd.getGroup().value(), cid.getItem().keyName()));
box.setSelectedItem(selectedItem);
box.setToolTipText(Text.titleCase(selectedItem));
}
@@ -508,7 +512,7 @@ class ConfigPanel extends PluginPanel
if (e.getStateChange() == ItemEvent.SELECTED)
{
changeConfiguration(box, cd, cid);
box.setToolTipText(Text.titleCase((Enum) box.getSelectedItem()));
box.setToolTipText(Text.titleCase((Enum<?>) box.getSelectedItem()));
}
});
item.add(box, BorderLayout.EAST);

View File

@@ -39,11 +39,11 @@ import net.runelite.client.util.Text;
* was very hard to see in the dark gray background, this makes the selected
* item white and adds some padding to the elements for more readable list.
*/
public final class ComboBoxListRenderer extends JLabel implements ListCellRenderer
public final class ComboBoxListRenderer<T> extends JLabel implements ListCellRenderer<T>
{
@Override
public Component getListCellRendererComponent(JList list, Object o, int index, boolean isSelected, boolean cellHasFocus)
public Component getListCellRendererComponent(JList<? extends T> list, T o, int index, boolean isSelected, boolean cellHasFocus)
{
if (isSelected)
{