ScrollBar dans JCombobox Personnalisé

sisisousou Messages postés 43 Date d'inscription samedi 5 janvier 2008 Statut Membre Dernière intervention 24 janvier 2012 - 23 janv. 2012 à 14:20
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 - 24 janv. 2012 à 15:46
J'ai Un Jcombobox personalisé mais j'ai un problem ... l'image vous explique exactement le problem, y'a pas de scrollbarr

 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package de.jugms.sd.components.comboboxes;
 
/**
 *
 * @author hansolo
 */
public class SDComboBox extends javax.swing.JComboBox
{
    private de.jugms.sd.tools.RadiusType radiusType;    
 
    public SDComboBox()
    {
        super();        
        editor = new de.jugms.sd.components.comboboxes.SDComboBoxEditor();        
        setUI(new de.jugms.sd.ui.SDComboBoxUI(this));
        setPreferredSize(new java.awt.Dimension(150, 22));
        this.radiusType = de.jugms.sd.tools.RadiusType.ROUNDED;
    }
 
    public SDComboBox(javax.swing.ComboBoxModel model)
    {
        super(model);
        editor = new de.jugms.sd.components.comboboxes.SDComboBoxEditor();
        setUI(new de.jugms.sd.ui.SDComboBoxUI(this));
        setPreferredSize(new java.awt.Dimension(150, 22));
        this.radiusType = de.jugms.sd.tools.RadiusType.ROUNDED;
    }
 
    public SDComboBox(Object[] items)
    {
        super(items);
        editor = new de.jugms.sd.components.comboboxes.SDComboBoxEditor();
        setUI(new de.jugms.sd.ui.SDComboBoxUI(this));
        setPreferredSize(new java.awt.Dimension(150, 22));
        this.radiusType = de.jugms.sd.tools.RadiusType.ROUNDED;
    }
 
    public SDComboBox(java.util.Vector<?> items)
    {
        super(items);
        editor = new de.jugms.sd.components.comboboxes.SDComboBoxEditor();
        setUI(new de.jugms.sd.ui.SDComboBoxUI(this));
        setPreferredSize(new java.awt.Dimension(150, 22));
        this.radiusType = de.jugms.sd.tools.RadiusType.ROUNDED;
    }
 
    @Override
    public void setEditable(boolean editable)
    {
        super.setEditable(editable);
    }
 
    public void setRadiusType(de.jugms.sd.tools.RadiusType radiusType)
    {
        this.radiusType = radiusType;
        ((de.jugms.sd.components.comboboxes.SDComboBoxEditor) getEditor()).setRadiusType(radiusType);
        ((de.jugms.sd.ui.SDComboBoxUI) getUI()).getArrowButton().setRadiusType(radiusType);
        ((de.jugms.sd.components.comboboxes.SDComboBoxCellRenderer) getRenderer()).setRadiusType(radiusType);
    }
 
    public de.jugms.sd.tools.RadiusType getRadiusType()
    {
        return this.radiusType;
    }
 
    @Override
    public String toString()
    {
        return "SDComboBox";
    }
}


 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.jugms.sd.components.comboboxes;
 
import java.awt.event.ActionListener;
 
import javax.swing.JScrollPane;
import javax.swing.plaf.basic.BasicComboPopup;
 
 
/**
 *
 * @author hansolo
 */
public class SDComboPopup extends BasicComboPopup implements ActionListener
{
    private javax.swing.JPopupMenu popupMenu = new javax.swing.JPopupMenu();
    private static final int LEFT_SHIFT = 22;
 
    public SDComboPopup(javax.swing.JComboBox combo)
    {
        super(combo);        
        popupMenu.setUI(new de.jugms.sd.ui.SDPopupMenuUI());
        this.comboBox = combo;
        comboBox.addActionListener(this);
    }
 
    private void clearAndFillMenu()
    {
 
        popupMenu.removeAll();
 
        javax.swing.ButtonGroup buttonGroup = new javax.swing.ButtonGroup();
 
        // add the given items to the popup menu.
        for (int i = 0; i < comboBox.getModel().getSize(); i++)
        {
            Object item = comboBox.getModel().getElementAt(i);
            de.jugms.sd.components.misc.SDMenuItem menuItem = new de.jugms.sd.components.misc.SDMenuItem(item.toString());
            menuItem.addActionListener(createMenuItemListener(item));                        
            buttonGroup.add(menuItem);
            popupMenu.add(menuItem);
 
            // if the current item is selected, make the menu item reflect that.
            if (item.equals(comboBox.getModel().getSelectedItem()))
            {
                menuItem.setSelected(true);
                popupMenu.setSelected(menuItem);
            }
        }
 
        popupMenu.pack();
        int popupWidth = comboBox.getWidth() + 5;
        int popupHeight = popupMenu.getPreferredSize().height;
        popupMenu.setPreferredSize(new java.awt.Dimension(popupWidth, popupHeight));
    }
 
    private java.awt.event.ActionListener createMenuItemListener(final Object comboBoxItem)
    {
        return new java.awt.event.ActionListener()
        {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent event)
            {
                comboBox.setSelectedItem(comboBoxItem);
            }
        };
    }
 
    @Override
    public void show()
    {
        clearAndFillMenu();
        java.awt.Rectangle popupBounds = calculateInitialPopupBounds();
 
        popupMenu.show(comboBox, popupBounds.x, popupBounds.y);
    }
 
    @Override
    public void hide()
    {
        popupMenu.setVisible(false);
    }
 
    @Override
    public boolean isVisible()
    {
        return popupMenu.isVisible();
    }
 
    private java.awt.Rectangle calculateInitialPopupBounds()
    {
        // grab the right most location of the button.
        int comboBoxRightEdge = comboBox.getWidth();
 
        // figure out how the height of a menu item.
        java.awt.Insets insets = popupMenu.getInsets();
 
        // calculate the x and y value at which to place the popup menu. by
        // default, this will place the selected menu item in the popup item
        // directly over the button.
        int x = comboBoxRightEdge - popupMenu.getPreferredSize().width - LEFT_SHIFT;
        int selectedItemIndex = popupMenu.getSelectionModel().getSelectedIndex();        
        int componentCenter = comboBox.getHeight() / 2;        
        int menuItemHeight = popupMenu.getComponent(selectedItemIndex).getPreferredSize().height;
        int menuItemCenter = insets.top + (selectedItemIndex * menuItemHeight) + menuItemHeight / 2;
        int y = componentCenter - menuItemCenter;
 
        return new java.awt.Rectangle(new java.awt.Point(x, y), popupMenu.getPreferredSize());
    }
 
    @Override
    public void actionPerformed(java.awt.event.ActionEvent event)
    {
        int popupWidth;
        int popupHeight;
 
        switch (comboBox.getSelectedIndex())
        {
            case -1:
                comboBox.addItem(comboBox.getSelectedItem().toString());
                popupWidth = comboBox.getWidth() + 5;
                popupHeight = popupMenu.getSize().height + popupMenu.getSubElements()[0].getComponent().getHeight();
                popupMenu.setPreferredSize(new java.awt.Dimension(popupWidth, popupHeight));
                break;
            default:
                if (comboBox.getSelectedItem().toString().isEmpty())
                {
                    comboBox.removeItemAt(comboBox.getSelectedIndex());
                    popupWidth = comboBox.getWidth() + 5;
                    popupHeight = popupMenu.getSize().height + popupMenu.getSubElements()[0].getComponent().getHeight();
                    popupMenu.setPreferredSize(new java.awt.Dimension(popupWidth, popupHeight));
                }
                break;
        }
    }
 
    @Override
    public String toString()
    {
        return "SDComboPopup";
    }
}


 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.jugms.sd.ui;
 
import de.jugms.sd.components.comboboxes.SDComboBoxCellRenderer;
import de.jugms.sd.components.comboboxes.SDComboBoxEditor;
 
 
/**
 *
 * @author hansolo
 */
public class SDComboBoxUI extends javax.swing.plaf.basic.BasicComboBoxUI implements javax.swing.SwingConstants
{
    private de.jugms.sd.tools.RadiusType radiusType;    
    de.jugms.sd.components.buttons.SDArrowButton button;
 
    public SDComboBoxUI(javax.swing.JComboBox comboBox)
    {
        super();
        super.comboBox = comboBox;
 
        comboBox.setOpaque(false);
        comboBox.setForeground(new java.awt.Color(1.0f, 1.0f, 1.0f, 0.6f));
        comboBox.setFont(new java.awt.Font("Verdana", 0, 12));
        this.radiusType = de.jugms.sd.tools.RadiusType.ROUNDED;
 
        if (comboBox.isEditable())
        {
            comboBox.setEditor(new SDComboBoxEditor());
        }
        else
        {            
            comboBox.setRenderer(new SDComboBoxCellRenderer(comboBox));
        }
    }
 
    public de.jugms.sd.components.buttons.SDArrowButton getArrowButton()
    {
        return this.button;
    }
 
    @Override
    protected javax.swing.plaf.basic.ComboPopup createPopup()
    {
        popup = new de.jugms.sd.components.comboboxes.SDComboPopup(comboBox);        
        return popup;
    }
 
    @Override
    protected javax.swing.JButton createArrowButton()
    {
        button = new de.jugms.sd.components.buttons.SDArrowButton(SOUTH, EAST);
        button.setRadiusType(this.radiusType);
        button.setName("ComboBox.arrowButton");
        return button;
    }
 
    public void setRadiusType(de.jugms.sd.tools.RadiusType radiusType)
    {
        this.radiusType = radiusType;
    }
 
    public de.jugms.sd.tools.RadiusType getRadiusType()
    {
        return this.radiusType;
    }
 
    @Override
    public String toString()
    {
        return "SDComboBoxUI";
    }
}

voila mes sources quelqu’un peut m'aider svp

3 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 367
23 janv. 2012 à 15:38
Bonjour,

Inutile de poster cette quantité de code, je ne lis rien de ce qui dépasse 20 lignes.

Pas d'image, je ne vois donc pas le problème...
0
sisisousou Messages postés 43 Date d'inscription samedi 5 janvier 2008 Statut Membre Dernière intervention 24 janvier 2012
24 janv. 2012 à 14:30
tout simplement mon jpopupmenu dépasse l'écrant je veux savoir comment ajouter un scrollbar
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 367
24 janv. 2012 à 15:46
Ok, tu as donc besoin d'un JScrollPane et non d'un JScrollBar (confusion fréquente).

Utilises ca comme ca :

JScrollPane jsp = new JScrollPane(tonTrucQuiDepasseDeLEcran);
fenetre.add(jsp);
0