JComboBox smaller than the popup...

N

nuria

hi

I am trying do this: have a JComboBox wich display part of the items
of the popup.. if the popup have:

P1 - PRUEBA1
P2 - PRUEBA2
P3 - PRUEBA3

And I select the second the combo only show P2. And the size of this
comboBox is smaller than his popup.

I am trying but can´t get any solution, my example is this(with other
data...)

Can anyone help me?? really thanks!!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MiComboBox extends JPanel {
String[] petStrings = {"Bird", "Cat", "Dog", "Rabbit", "Pig"};
String[] petTraduccion = {"Pajaro", "Gato", "Perro", "Conejo",
"Cerdo"};

class ComboBoxRenderer extends JLabel implements ListCellRenderer {
private Font uhOhFont;

public ComboBoxRenderer() {
setOpaque(true);
setHorizontalAlignment(CENTER);
setVerticalAlignment(CENTER);
}

/*
* This method finds the image and text corresponding
* to the selected value and returns the label, set up
* to display the text and image.
*/
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {



//Get the selected index. (The index param isn't
//always valid, so just use the value.)
int selectedIndex = ((Integer) value).intValue();



if (isSelected) {
// setText("P1" + petTraduccion[selectedIndex]);
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
//Set the icon and text. If icon was null, say so.
// ImageIcon icon = images[selectedIndex];
String pet = petStrings[selectedIndex];
String traduccion = petTraduccion[selectedIndex];
// setIcon(icon);
// if (icon != null) {
if (isSelected) {
setText(pet + " - " + traduccion);

}
else{
setText(pet);
}
((JComponent)list.getParent().getParent().getParent()).setPreferredSize(new
Dimension (300, 50));
((JComponent)list.getParent().getParent().getParent()).setSize(new
Dimension (300, 50));

list.setFixedCellHeight(20);

// setText(popupIsVisible()?""+pet+" very long currency name" :
// ""+traduccion);



this.revalidate();

return this;
}

//Set the font and text when no image was found.
protected void setUhOhText(String uhOhText, Font normalFont) {
if (uhOhFont == null) { //lazily create this font
uhOhFont = normalFont.deriveFont(Font.ITALIC);
}
setFont(uhOhFont);
setText(uhOhText);
}
}
/*
* Despite its use of EmptyBorder, this panel makes a fine content
* pane because the empty border just increases the panel's size
* and is "painted" on top of the panel's normal background. In
* other words, the JPanel fills its entire background if it's
* opaque (which it is by default); adding a border doesn't change
* that.
*/
public MiComboBox() {
super(new BorderLayout());

//Load the pet images and create an array of indexes.
// images = new ImageIcon[petStrings.length];
Integer[] intArray = new Integer[petStrings.length];

for (int i = 0; i < petStrings.length; i++) {
intArray = new Integer(i);
}

//Create the combo box.
JComboBox petList = new JComboBox(intArray);
ComboBoxRenderer renderer= new ComboBoxRenderer();
// renderer.setPreferredSize(new Dimension(100, 30));
petList.setRenderer(renderer);
petList.setMaximumRowCount(3);

//Lay out the demo.
add(petList);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
// JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.
JFrame frame = new JFrame("MiComboBox");
frame.setDefaultCloseOperation(0);

//Create and set up the content pane.
JComponent newContentPane = new MiComboBox();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

//Display the window.
frame.pack();
frame.setVisible(true);
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL =
CustomComboBoxDemo.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
public Dimension getSize() {
return new Dimension(200, 20);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top