He estado haciendo una aplicación SWING en Java y me encontré con un problema que me esta dando dolres de cabeza el cual es ingresar un tooltip diferente dependiendo el campo que seleccionen del combobox... (esto debido a que el campo es pequeño y el texto interno es más largo, no puedo ampliar el combobox debido a limitaciones del template que no me permiten mover).
Bien, yo cuento con lo siguiente:
Código PHP:
package com.example;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class ComboboxToolTipRenderer extends DefaultListCellRenderer {
ArrayList tooltips;
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
JComponent comp = (JComponent) super.getListCellRendererComponent(list,
value, index, isSelected, cellHasFocus);
if (-1 < index && null != value && null != tooltips) {
list.setToolTipText(tooltips.get(index));
}
return comp;
}
public void setTooltips(ArrayList tooltips) {
this.tooltips = tooltips;
}
}
Código PHP:
package comboBox;
import java.awt.FlowLayout;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class ComboBoxCreate extends JFrame {
public ComboBoxCreate() {
initialize();
}
private void initialize() {
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel label1 = new JLabel("Month :");
String[] months = {"January", "February", "March", "April", "Mei", "June",
"July", "August", "September", "October", "November", "December"};
JComboBox comboBox = new JComboBox();
ComboboxToolTipRenderer renderer = new ComboboxToolTipRenderer();
comboBox.setRenderer(renderer);
//make a loop as needed
comboBox.addItem(months);
tooltips.add(tooltipString);
renderer.setTooltips(tooltips);
//JComboBox comboBox = new JComboBox(months);
getContentPane().add(label1);
getContentPane().add(comboBox);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ComboBoxCreate().setVisible(true);
}
});
}
}
Les dejo el comboBox, funcionando pero sin el tooltip (aunque el de arriba tampoco lo tiene :S jeje)
Código PHP:
package comboBox;
import java.awt.FlowLayout;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class ComboBoxCreate extends JFrame {
public ComboBoxCreate() {
initialize();
}
private void initialize() {
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel label1 = new JLabel("Month :");
String[] months = {"January", "February", "March", "April", "Mei", "June",
"July", "August", "September", "October", "November", "December"};
/*JComboBox comboBox = new JComboBox();
ComboboxToolTipRenderer renderer = new ComboboxToolTipRenderer();
comboBox.setRenderer(renderer);
//make a loop as needed
comboBox.addItem(months);
tooltips.add(tooltipString);
renderer.setTooltips(tooltips);*/
JComboBox comboBox = new JComboBox(months);
getContentPane().add(label1);
getContentPane().add(comboBox);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ComboBoxCreate().setVisible(true);
}
});
}
}
Muchas gracias!!!