简体   繁体   中英

Activate ActionListener on JComboBox setSelectedItem()

I have a form in a JDialog starting with a JComboBox:

myJComboBox = new JComboBox(itemOfBox);
myJpanel.add(myJComboBox);

When I don't care what my default itemOfBox is, I have some JSliders shown by default.

When I change my itemOfBox from the JDialog user interface, I have an ActionListener on my myJComboBox which changes the JSliders in function of the itemOfBox (ranges, default values, etc). So far, all works fince since I don't care about my default itemOfBox.

Now I want to keep all these working, but to add that if I have a requiredItemOfBox, to be able to set the myJComboBox to it, but also to activate the ActionListener on myJComboBox on the new itemOfBox (which is my requiredItemOfBox now)

myJComboBox.setSelectedItem(requiredItemOfBox);

So when I don't have a requiredItemOfBox (in this case it's "", so the previous line does nothing to the program), I have the first item of my JComboBox with the default values and default JSliders and when I select another item of the list, I have my ActionListener to take care of it.

When I have the requiredItemOfBox (!), I manage to make my JDialog select it, but every time I have to click again on it to update my JSliders.

How may I solve this problem ?

Thank you for your help and sorry for my english.

I'm taking a guess here.

Since you're setting a selected item, you can execute the action listener code yourself. The trick is to code your action listener like this one from one of my projects.

package com.ggl.crossword.controller;

import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import com.ggl.crossword.view.CrosswordFrame;

public class CreateImageActionListener implements ActionListener {

    private CrosswordFrame frame;

    private JPanel panel;

    public CreateImageActionListener(CrosswordFrame frame,
            JPanel panel) {
        this.frame = frame;
        this.panel = panel;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        writeImage();
    }

    public void writeImage() {
        FileFilter filter = 
                new FileNameExtensionFilter("PNG file", "png");
        JFileChooser fc = new JFileChooser();
        fc.setFileFilter(filter);
        int returnValue = fc.showSaveDialog(frame.getFrame());
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            if (!file.getAbsolutePath().endsWith(".png")) {
                file = new File(file.getAbsolutePath() + ".png");
            }
            RenderedImage image = createImage(panel);
            try {
                ImageIO.write(image, "png", file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private BufferedImage createImage(JPanel panel) {
        int w = panel.getWidth();
        int h = panel.getHeight();
        BufferedImage bi = new BufferedImage(w, h, 
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        panel.paint(g);
        g.dispose();
        return bi;
    }

}

You would keep the listener instance as a class field and execute listener.writeImage() . In this way, you can execute the action listener code without having to trigger the action listener itself.

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