在JPopUpMenu中手动检查JMenuItem?

时间:2015-09-16 13:57:02

标签: select jmenuitem jpopupmenu

我有一个JPopUpMenu,上面有几个JCheckBoxMenuItem

My JPopUpMenu

实际上我想做的是基本选择具有特定索引的JPopUpMenu项。

例如,像myPopUpMenu.setSelected(2)这样的方法,会在JPopUpMenu中选择“Algérie”。

问题是我不知道任何允许我手动检查项目的方法......

以下是我JPopUpMenu的代码:

MainVue.java

public class MainVue extends JFrame implements ActionListener {

 private static final JScrollPopupMenu menuProduit = new JScrollPopupMenu();
 private static final JScrollPopupMenu menuPays = new JScrollPopupMenu();
 private static List<String> listeFiltres = new ArrayList<String>();
 private String listeDeFiltres;
 private String[] tableauFiltrePermanent;
 private String listeFiltrePermanent;

 private String[] tableauPays = { "Autres", "Afrique du sud", "Algérie", "Allemagne", "Arabie Saoudite", "Argentine",
        "Australie", "Bangladesh", "Belgique", "Brésil", "Bulgarie", "Canada", "Chine", "Corée du sud", "Egypte",
        "Emirats-Arabes Unis", "Espagne", "Etats-Unis", "Ethiopie", "Europe", "France", "Hongrie", "Inde",
        "Indonésie", "Irak", "Iran", "Israél", "Italie", "Japon", "Jordanie", "Kazakhstan", "Koweit", "Liban",
        "Libye", "Malaisie", "Maroc", "Mexique", "Monde", "Oman", "Pakistan", "Pays-Bas", "Philippines", "Poligne",
        "Portugal", "Qatar", "République tchéque", "Roumanie", "Russie", "Taïwan", "Tunisie", "Turquie",
        "Ukraine" };
 private String[] tableauProduit = { "Blé", "Colza", "Mais", "Orge", "Orge de Brasserie", "Palme", "Soja",
        "Tournesol", "Tourteaux De Colza", "Tourteaux de Soja", "Huile de Soja", "Huile De Colza" };

 private List<JCheckBoxMenuItem> listJCBProduit = new ArrayList<JCheckBoxMenuItem>();
 private List<JCheckBoxMenuItem> listJCBPays = new ArrayList<JCheckBoxMenuItem>();
 public static PropertiesConfiguration prop;

 public MainVue(Modele modele, Controleur controleur) throws ClassNotFoundException, SQLException, IOException {

  prop = new PropertiesConfiguration("config.properties");

  for (int i = 0; i < tableauProduit.length; i++) {
        listJCBProduit.add(new JCheckBoxMenuItem(tableauProduit[i]));
    }

    for (int j = 0; j < listJCBProduit.size(); j++) {
        JCheckBoxMenuItem produitActuel = listJCBProduit.get(j);
        menuProduit.add(produitActuel);
        produitActuel.addActionListener(new OpenAction(menuProduit, boutonProduit));


    }

    for (int i = 0; i < tableauPays.length; i++) {
        listJCBPays.add(new JCheckBoxMenuItem(tableauPays[i]));
    }

    for (int j = 0; j < listJCBPays.size(); j++) {
        JCheckBoxMenuItem paysActuel = listJCBPays.get(j);
        menuPays.add(paysActuel);
        paysActuel.addActionListener(new OpenAction(menuPays, boutonPays));
    }
    listeDeFiltres = "";

        for (int p = 0; p < listeFiltres.size(); p++) {
            String filtreActuel = listeFiltres.get(p);
            if (listeDeFiltres == "") {
                listeDeFiltres += filtreActuel;
            } else {
                listeDeFiltres += "," + filtreActuel;
            }

            }
  prop.setProperty("listeFiltres", listeDeFiltres);
 }
}

这是JScrollPopUpMenu组件:

JScrollPopUpMenu.java

package fr.views;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.JPopupMenu;
import javax.swing.JScrollBar;

public class JScrollPopupMenu extends JPopupMenu {
protected int maximumVisibleRows = 10;

public JScrollPopupMenu() {
    this(null);
}


public JScrollPopupMenu(String label) {
    super(label);
    setLayout(new ScrollPopupMenuLayout());

    super.add(getScrollBar());
    addMouseWheelListener(new MouseWheelListener() {
        @Override public void mouseWheelMoved(MouseWheelEvent event) {
            JScrollBar scrollBar = getScrollBar();
            int amount = (event.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL)
                         ? event.getUnitsToScroll() * scrollBar.getUnitIncrement()
                         : (event.getWheelRotation() < 0 ? -1 : 1) * scrollBar.getBlockIncrement();

            scrollBar.setValue(scrollBar.getValue() + amount);
            event.consume();
        }
    });
}

private JScrollBar popupScrollBar;
protected JScrollBar getScrollBar() {
    if(popupScrollBar == null) {
        popupScrollBar = new JScrollBar(JScrollBar.VERTICAL);
        popupScrollBar.addAdjustmentListener(new AdjustmentListener() {
            @Override public void adjustmentValueChanged(AdjustmentEvent e) {
                doLayout();
                repaint();
            }
        });

        popupScrollBar.setVisible(false);
    }

    return popupScrollBar;
}

public int getMaximumVisibleRows() {
    return maximumVisibleRows;
}

public void setMaximumVisibleRows(int maximumVisibleRows) {
    this.maximumVisibleRows = maximumVisibleRows;
}

public void paintChildren(Graphics g){
    Insets insets = getInsets();
    g.clipRect(insets.left, insets.top, getWidth(), getHeight() - insets.top - insets.bottom);
    super.paintChildren(g);
}

protected void addImpl(Component comp, Object constraints, int index) {
    super.addImpl(comp, constraints, index);

    if(maximumVisibleRows < getComponentCount()-1) {
        getScrollBar().setVisible(true);
    }
}

public void remove(int index) {
    // can't remove the scrollbar
    ++index;

    super.remove(index);

    if(maximumVisibleRows >= getComponentCount()-1) {
        getScrollBar().setVisible(false);
    }
}

public void show(Component invoker, int x, int y){
    JScrollBar scrollBar = getScrollBar();
    if(scrollBar.isVisible()){
        int extent = 0;
        int max = 0;
        int i = 0;
        int unit = -1;
        int width = 0;
        for(Component comp : getComponents()) {
            if(!(comp instanceof JScrollBar)) {
                Dimension preferredSize = comp.getPreferredSize();
                width = Math.max(width, preferredSize.width);
                if(unit < 0){
                    unit = preferredSize.height;
                }
                if(i++ < maximumVisibleRows) {
                    extent += preferredSize.height;
                }
                max += preferredSize.height;
            }
        }

        Insets insets = getInsets();
        int widthMargin = insets.left + insets.right;
        int heightMargin = insets.top + insets.bottom;
        scrollBar.setUnitIncrement(unit);
        scrollBar.setBlockIncrement(extent);
        scrollBar.setValues(0, heightMargin + extent, 0, heightMargin + max);

        width += scrollBar.getPreferredSize().width + widthMargin;
        int height = heightMargin + extent;

        setPopupSize(new Dimension(width, height));
    }

    super.show(invoker, x, y);
}

protected static class ScrollPopupMenuLayout implements LayoutManager{
    @Override public void addLayoutComponent(String name, Component comp) {}
    @Override public void removeLayoutComponent(Component comp) {}

    @Override public Dimension preferredLayoutSize(Container parent) {
        int visibleAmount = Integer.MAX_VALUE;
        Dimension dim = new Dimension();
        for(Component comp :parent.getComponents()){
            if(comp.isVisible()) {
                if(comp instanceof JScrollBar){
                    JScrollBar scrollBar = (JScrollBar) comp;
                    visibleAmount = scrollBar.getVisibleAmount();
                }
                else {
                    Dimension pref = comp.getPreferredSize();
                    dim.width = Math.max(dim.width, pref.width);
                    dim.height += pref.height;
                }
            }
        }

        Insets insets = parent.getInsets();
        dim.height = Math.min(dim.height + insets.top + insets.bottom, visibleAmount);

        return dim;
    }

    @Override public Dimension minimumLayoutSize(Container parent) {
        int visibleAmount = Integer.MAX_VALUE;
        Dimension dim = new Dimension();
        for(Component comp : parent.getComponents()) {
            if(comp.isVisible()){
                if(comp instanceof JScrollBar) {
                    JScrollBar scrollBar = (JScrollBar) comp;
                    visibleAmount = scrollBar.getVisibleAmount();
                }
                else {
                    Dimension min = comp.getMinimumSize();
                    dim.width = Math.max(dim.width, min.width);
                    dim.height += min.height;
                }
            }
        }

        Insets insets = parent.getInsets();
        dim.height = Math.min(dim.height + insets.top + insets.bottom, visibleAmount);

        return dim;
    }

    @Override public void layoutContainer(Container parent) {
        Insets insets = parent.getInsets();

        int width = parent.getWidth() - insets.left - insets.right;
        int height = parent.getHeight() - insets.top - insets.bottom;

        int x = insets.left;
        int y = insets.top;
        int position = 0;

        for(Component comp : parent.getComponents()) {
            if((comp instanceof JScrollBar) && comp.isVisible()) {
                JScrollBar scrollBar = (JScrollBar) comp;
                Dimension dim = scrollBar.getPreferredSize();
                scrollBar.setBounds(x + width-dim.width, y, dim.width, height);
                width -= dim.width;
                position = scrollBar.getValue();
            }
        }

        y -= position;
        for(Component comp : parent.getComponents()) {
            if(!(comp instanceof JScrollBar) && comp.isVisible()) {
                Dimension pref = comp.getPreferredSize();
                comp.setBounds(x, y, width, pref.height);
                y += pref.height;
            }
        }
    }
}
}

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

从getIndex()方法获得的索引使用如下。您正在JScrollPopupMenu的0索引处添加ScrollBar。因此,要删除转换错误,请按以下步骤更新代码。

int index = getIndex("name");
      ((JCheckBoxMenuItem)menuProduit.getComponentAtIndex(index+1)).setState(true);