[Java] swing_ItemListener

Jeiniยท2022๋…„ 12์›” 19์ผ
0

โ˜•๏ธย  Java

๋ชฉ๋ก ๋ณด๊ธฐ
46/59
post-thumbnail

๐Ÿ’กItemListener


ItemSelectable ํ•ญ๋ชฉ ์ด๋ฒคํŠธ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ตฌ์„ฑ ์š”์†Œ์— ์˜ํ•ด ์‹œ์ž‘๋œ๋‹ค.

์ผ๋ฐ˜์ ์œผ๋กœ ItemSelectable ๊ตฌ์„ฑ ์š”์†Œ๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ํ•ญ๋ชฉ์— ๋Œ€ํ•ด ์ผœ์ง/๊บผ์ง ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•œ๋‹ค.

ํ•ญ๋ชฉ ์ด๋ฒคํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” Swing ๊ตฌ์„ฑ ์š”์†Œ์—๋Š” ์ฒดํฌ ์ƒ์ž , ์ฒดํฌ ๋ฉ”๋‰ด ํ•ญ๋ชฉ , ํ† ๊ธ€ ๋ฒ„ํŠผ ๋“ฑ๊ณผ ๊ฐ™์€ ๋ฒ„ํŠผ๊ณผ ์ฝค๋ณด ์ƒ์ž ๊ฐ€ ํฌํ•จ๋œ๋‹ค.

โœ๏ธ ์ดˆ๊ธฐํ™”๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ณณ

checkbox,addItemListener(this);
...
public void itemStateChanged(ItemEvent e) {
	if(e.getStateChange() == ItemEvent.SELECTED) {
    	label.setVisible(true);
    ...
    } else {
    	label.setVisible(false);
    }
}

์šฐ๋ฆฌ๊ฐ€ ๋ฒ„ํŠผ๊ณผ ์ฝค๋ณด์ƒ์ž์—์„œ ActionListener ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ด์œ ๋Š” ๋™์ผํ•œ ์„ ํƒ์— ๋Œ€ํ•ด ์ค‘๋ณต๋˜๋Š” ์‹คํ–‰์ด ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด, ์„ ํƒ๋œ ๋ผ๋””์˜ค๋ฒ„ํŠผ์˜ ์ƒํƒœ๋Š” ์ฒดํฌํ•  ๋•Œ ๋ฐœ์ƒํ•œ๋‹ค.

ItemListener iListener = new ItemListener() {
			
	@Override
	public void itemStateChanged(ItemEvent ie) {
		JRadioButton rbtn = (JRadioButton)ie.getSource();
        
		// ์„ ํƒ๋์„ ๋•Œ๋งŒ ์กฐ๊ฑดํ•œ๋‹ค๋Š” ๋œป 
		if(ie.getStateChange() == ItemEvent.SELECTED) {
			System.out.println("์„ ํƒ => ");
          
          // ์„ ํƒ์•ˆ๋์„ ๋•Œ
		} else if(ie.getStateChange() == ItemEvent.DESELECTED) {
			System.out.println("์„ ํƒํ•ด์ œ => ");
		}
        
		System.out.println(rbtn.getText());
	}
};

โœ”๏ธ ie.getStateChange() โžก๏ธ ์ด๋ฒคํŠธ ์ข…๋ฅ˜๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
โœ”๏ธ ItemEvent.SELECTED โžก๏ธ ์„ ํƒ ์‹œ
โœ”๏ธ ItemEvent.DESELECTED โžก๏ธ ์„ ํƒํ•ด์ œ ์‹œ

์—ฌ๊ธฐ์„œ ์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์งœ๋ณด๋ฉด ๊ฒฐ๊ณผ๊ฐ’์ด ์„ ํƒ ์„ ํƒํ•ด์ œ ์ด 2๊ฐœ๊ฐ€ ์ฐํžŒ๋‹ค.

๊ทธ ์ด์œ ๋Š” ๋ฒ„ํŠผ์„ ์„ ํƒํ•˜๊ณ  ๋‹ค๋ฅธ ๋ฒ„ํŠผ ์„ ํƒํ• ๋•Œ, ์ทจ์†Œ๋œ ๋ฒ„ํŠผ์€ ๊ณ„์† ์‹คํ–‰๋˜๋ฉด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ActionListener ๋Š” ์“ฐ์ง€ ์•Š๋Š”๋‹ค. (๊ณ„์† ์‹คํ–‰๋ผ์„œ ์ค‘๋ณต์ด ๋จ)

๐Ÿ“Ž itemStateChanged

ItemListener ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ๋งŒ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ์–ด๋Œ‘ํ„ฐ ํด๋ž˜์Šค๊ฐ€ ์—†๋‹ค.

  • itemStateChanged(์•„์ดํ…œ ์ด๋ฒคํŠธ)
    : ๋“ฃ๋Š” ๊ตฌ์„ฑ ์š”์†Œ์˜ ์ƒํƒœ ๋ณ€๊ฒฝ ์งํ›„์— ํ˜ธ์ถœ๋œ๋‹ค.

โœ๏ธ ์˜ˆ์ œ

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ItemEventEx1 extends JFrame {
    private JRadioButton rbtnRed;
    private JRadioButton rbtnBlack;
    private JRadioButton rbtnWhite;

    private JLabel lblMain;

    private ItemEventEx1() {
        init();
        setDisplay();
        addListeners();
        showFrame();
    }
    private void init() {
        rbtnRed = new JRadioButton("Red");
        rbtnBlack = new JRadioButton("Black");
        rbtnWhite = new JRadioButton("White", true);

        ButtonGroup group = new ButtonGroup();
        group.add(rbtnRed);
        group.add(rbtnBlack);
        group.add(rbtnWhite);

        lblMain = new JLabel("White", JLabel.CENTER);
        lblMain.setFont(new Font(Font.SERIF, Font.BOLD, 50));
        lblMain.setOpaque(true); // ๋ถˆํˆฌ๋ช…ํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์คŒ
        lblMain.setBackground(Color.WHITE);
        lblMain.setForeground(Color.GREEN); // ํฐํŠธ ์ƒ‰์ƒ๋ฐ”๊พธ๊ธฐ

    }
    private void setDisplay() {
        JPanel pnlSouth = new JPanel(new GridLayout(1, 4));
        pnlSouth.add(new JLabel("background: ", JLabel.CENTER));
        pnlSouth.add(rbtnRed);
        pnlSouth.add(rbtnBlack);
        pnlSouth.add(rbtnWhite);

        add(lblMain, BorderLayout.CENTER);
        add(pnlSouth, BorderLayout.SOUTH);

    }
    private void addListeners() {
    	// ์ฒดํฌ๋ฐ•์Šค ์„ ํƒ ItemListener
        ItemListener itemListener = new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent ie) {
                Color color = Color.WHITE; // ๋ฐฐ๊ฒฝ์ƒ‰
                String strColor = "white"; // ๋ฐฐ๊ฒฝ๊ธ€

                if(ie.getStateChange() == ItemEvent.SELECTED) {
                    JRadioButton rbtn = (JRadioButton)ie.getSource();
                    if(rbtn == rbtnRed) {
                        color = Color.RED;
                        strColor = "Red";
                    } else if(rbtn == rbtnBlack) {
                        color = Color.BLACK;
                        strColor = "black";
                    }
                    lblMain.setBackground(color);
                    lblMain.setText(strColor);
                }
            }
        };

        rbtnRed.addItemListener(itemListener);
        rbtnBlack.addItemListener(itemListener);
        rbtnWhite.addItemListener(itemListener);
		
        // ์•Œ๋ฆผ์ฐฝ WindowAdapter
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent we) {
               int choice = JOptionPane.showConfirmDialog(
                           ItemEventEx1.this,
                           "์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.",
                           "์ข…๋ฃŒ",
                           JOptionPane.OK_CANCEL_OPTION,
                           JOptionPane.QUESTION_MESSAGE
               );
               if(choice == JOptionPane.YES_OPTION) {
                   System.exit(0);
               }
            }
        });
    }

    private void showFrame() {
        setTitle("ItemEvent");
        pack();
        setLocation(100, 0);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }


    public static void main(String[] args) {
        new ItemEventEx1();
    }
}

profile
Fill in my own colorful colors๐ŸŽจ

0๊ฐœ์˜ ๋Œ“๊ธ€