简体   繁体   中英

problems in selecting the JComboBox items(in JMenu) Java Swing

Im having trouble in selecting the JCombox items here is a gif: Gif that shows exactly what is the problem

here is code that you can test your own as asked by @UNKNOWN i delete everything that no needed so you can test , i have no error in console; the code works perfectly but the combo slection error is there

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

public class ForTest extends JFrame {
    private JTextArea txtArea= null;
    private JComboBox cmbFontSize = null;
    private JComboBox cmbFontFamily = null;
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ForTest();
            }
        });
    }
    private ForTest(){
        init();
    }

    public void init(){
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setTitle("FOR TEST");
        this.setLocationRelativeTo(null);
        this.setSize(850,500);
        txtArea= new JTextArea();
        txtArea.setSize(830,470);
        JScrollPane panScrollable = new JScrollPane(txtArea);

        panScrollable.setSize(840,480);

        this.add(panScrollable, BorderLayout.CENTER);

        JMenuBar jmbTop= new JMenuBar();

        JMenu modifica = new JMenu("Modifica");
        modifica.setFont(new Font("arial",Font.PLAIN,15));
        JMenu fontSettings= new JMenu("Font Settings");
        JMenuItem cmbFam= new JMenuItem("FONT FAMILY");

        cmbFontSize = new JComboBox();
        cmbFontFamily = new JComboBox();
        cmbFam.add(cmbFontFamily);
        fontSettings.add(cmbFam);
        // fontSettings.add(cmbFontSize);
        //fontSettings.add(cmbFontFamily);
        modifica.add(fontSettings);
        jmbTop.add(modifica);
        loadFontFamily();
        loadFontSize();
        this.setJMenuBar(jmbTop);

        this.setVisible(true);

    }

    private void loadFontFamily() {
        String fonts[] =
                GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

        for ( int i = 0; i < fonts.length; i++ )
        {
            cmbFontFamily.addItem(fonts[i]);
        }
    }
    private void loadFontSize() {
        for (int i= 10; i<50;i++){
            cmbFontSize.addItem(i);
        }
    }
}

the code is simple but cant understand why i cannot select the items thanks in advance:)

You should wrap the JComboBox inside a JMenuItem object and then add the to the JMenu one. That would do the trick

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM