반응형

스윙으로 GUI를 만들어 리스너에 코딩을 하고 했는데 코딩상에서 해당 컴포넌트에 걸린 이벤트를

실행하려고 별짓을 다 했다. ㅡㅡ; 그냥 이벤트 내에 들어가는 코딩을 메소드로 만들어 메소드를 호출하기도..

이건 아니다 싶어 이벤트 생성하여 해당 리스너를 참조하여 

 testListener.actionPerformed(new ActionEvent(jBtntest,ActionEvent.ACTION_PERFORMED, "openEvent"));

이렇게 해서 사용했다.. 대충 이렇게 하는게 낫나 싶어서 .. 므튼 이리저리 찾다보니 관련 자료를 찾았다.
 

원문: http://mindprod.com/jgloss/event11.html#SYNTHETICEVENTS

참조: http://www.codeguru.com/java/articles/162.shtml

JButton은 보통 doClick() 메소드를 이용하여 처리하면 되지만 다른 방법도 있으니 알아보자.

내가 주로 사용했던 방법은 2번 이었음..^^; 뭐 편법은 아닌듯.. ㅋㅋ

원문에서 필요한 부분만 발췌 하였음..

There are a number of ways to fake an event.

2) The simplest is just to call a Listener method directly with a dummy Event object, filled in with just enough data to keep the method happy.

3)Create an Event and introduce it to the Component that will handle it at the processEvent method. with:

 4)Create an Event 

 

 5)Generating MouseMoved Events has no effect on the screen mouse cursor. To make the underlying native GUI see your generated Events, use the Robot class to generate move clicks, moves etc. 

기본적으로 버튼에 따른 이벤트를 실행한다면 doClick()을 사용해야겠다.
그리고 일단 막히면 API 부터 제대로 뒤져봐야겠다 ㅡㅡ; 끙.. doClick 모르고 있었다는 ㅡㅡ;

2010.11.03  추가
오랫만에 기존에 작성했던 프로그램에 간단한 수정을 하면서 이벤트를 강제로 발생하는 부분을 검색

내가 기존에 포스팅 했던 글이 뜬다. ㅡㅡ;  예전에 몰라서 포스팅한걸 지금도 까먹다니 ㅠㅠ

그래서 조금 내용을 추가하여 갱신하기로 했다. 다시한번 잘 정리한다는 생각으로..^^

 

반응형

반응형
이벤트 응모수신거부
반응형

자바에서 각 이벤트는 하나의 이벤트 타입에 속하게 됩니다.

사용자가 JButton 컴포넌트를 클릭할 때 발생하는 actionPerformed 이벤트는 ActionEvent라고 하는 이벤트 타입에

속합니다. 여기에서 이벤트 타입은 EventObject 클래스에서 파생되는 클래스로 정의됩니다.

그리고 각 이벤트 타입에 속하는 모든 이벤트는 리스너 인터페이스의 멤버가 됩니다.

예를 들어 ActionEvent 이벤트 타입에 속하는 actionPerformed 이벤트는 ActionListener라고 하는 리스너

인터페이스의 멤버가 되는 것이지요.

 

결국 하나의 이벤트 타입에 대하여 하나의 리스터 인터페이스가 있는 셈입니다.

 

Interface

Class

ActionListener

ActionEvent

AdjustmentListener

AdjustmentEvent

AWTEventListener

AWTEvenetListenerProxy

ComponentListener

ComponentAdapter

ContainerListener

ComponentEvent

FocusListener

ContainerAdapter

HierachyBoundsListener

ContainerEvent

HierachyListener

FocusAdapter

InputMethodListener

FocusEvent

ItemListener

HierachyBoundsAdapter

KeyListener

HierachyEvent

MouseListener

InputEvent

MouseMotionListener

InputMethodEvent

MouseWheelListener

InvocationEvent

TextListener

ItemEvent

WindowFocusListener

KeyAdapter

WindowListener

KeyEvent

WindowStateListener

MouseAdapter

 

MouseEvent

 

MouseMotionAdapter

 

MouseWheelEvent

 

PaintEvent

 

TextEvent

 

WindowAdapter

 

WindowEvent

 

 

하나의 리스너 인터페이스에 여러 메소드 즉, 여러 이벤트가 포함될 수 있습니다.

예를 들어 창과 관련된 이벤트에서는 windowActivated, windowClosed, windowClosing, windowDeactivated,

windowDeiconified windowIconified, windowOpened 등이 있으며, 이들 이벤트는 모두 windowListener라고 하는

리스너 인터페이스의 멤버가 됩니다.

 

이러한 리스너 인터페이스를 구현하는 클래스를 어댑터(Adapter)라고 하며, 어댑터의 인스턴스를 리스너라고 합니다.

그리고 리스너 인터페이스를 구현하고 있는 리스너를 이벤트 소스에 등록시켜야 합니다.

이벤트 소스란 이벤트를 발생시키는 객체입니다.

public class MyPanel extends Jpanel implements ActionListener{

public void actionPerformed(ActionEvent e){

JoptionPane.showMessageDialog(

this,

getSource().ToString() + “ 를 클릭했습니다.”,

“actionPerformed 이벤트”,

JoptionPane.OK_OPTION);

}

public MyPanel(){

jButton = new Jbutton(“클릭하세요.”);

jButton.addActionListener(this);

}

}

 

public class MyPanel extends Panel{

ActionAdapter actionAdapter = new ActionAdapter();

public MyPanel(){

jButton = new Jbutton(“클릭하세요.”);

jButton.addActionListener(actionAdapter);

}

}

 

class WindowClosing extends WindowAdapter{

    public void windowClosing(WindowEvent e){

        System.exit(0);

    }

}

 

class MyFrame extends JFrame{

    public MyFrame(){

        WindowClosing listener = new WindowClosing();

        addWindowListener(listener);

    }

}

 

class MyFrame extends JFrame{

    public MyFrame(){

        addWindowListener(new WindowAdapter(){

            public void windowClosing(WindowEvent e){

                System.exit(0);

            }

        });

    }

}

 

* “예전에 누가 Adapter를 사용하면 리스너에 있는 method()를 모두 구현하지 않아도

되며 필요한 method()구현하면 된다고 했었는데, 사실은 Adapter 클래스를

상속받아서 새로운 리스너를 구현하는 것입니.”

반응형

Method Categories and Interface

 

Category

Interface Name

Methods

Action

ActionListener

actionPerformed(ActionEvent)

Item

ItemListener

itemStateChanged(ItemEvent)

Mouse

MouseListener

mousePressed(MouseEvent)

mouseReleased(MouseEvent)

mouseEnterd(MouseEvent)

mouseExited(MouseEvent)

mouseClicked(MouseEvent)

Mouse Motion

MouseMotionListener

mouseDragged(MouseEvent)

mouseMoved(MouseEvent)

Key

KeyListener

keyPressed(KeyEvent)

keyReleased(KeyEvent)

keyTyped(KeyEvent)

Focus

FocusListener

focusGained(FocusEvent)

focusLost(FocusEvent)

Adjustment

AdjustmentListener

adjustmentValueChanged(AdjusmentEvent)

Component

ComponenetListener

componentMoved(ComponentEvent)

componentHidden(ComponentEvent)

componentResized(ComponentEvent)

componentShown(ComponentEvent)

Window

WindowListener

windowClosing(WindowEvent)

windowOpened(WindowEvent)

windowIconified(WindowEvent)

windowDeiconified(WindowEvent)

windowClosed(WindowEvent)

windowActivated(WindowEvent)

windowDeactivated(WindowEvent)

Container

ContainerListener

componentAdded(ContainerEvent)

componentRemoved(ContainerEvent)

Text

TextListener

textValueChanged(TextEvent)

 

 

 

 

 

AWT Components

 

Component Type

Description

Button

A named rectangular box used for receiving mouse clicks

Canvas

A panel used for drawing

Checkbox

A component allowing the user to select an item

ChekboxMenuItem

A checkbox within a menu

Choice

A pull-down static list of items

Component

The parent of all AWT components, except menu components

Container

The parent of all AWT containers

Dialog

The base class of all modal dialog boxes

Frame

The base class of all GUI windows with window manager controls

Label

A text string component

List

A component that containers a dynamic set of items

Menu

An element under the menu bar, which containers a set of menu items

MenuItem

An item within a menu

Panel

A basic container class used most often to create complex layouts

Scrollbar

A component that allows a user to “select from a range of values”

ScrollPane

A container class that implements automatic horizontal and vertical

Scrolling for a single child component

TextArea

A component that allows the user to enter a block of text

TextField

A component that allows the user to enter a single line of text

Window

The base class of all GUI windows, without window manager controls

  

Component Events

 

Component Type

Act

Adj

Cmp

Cnt

Foc

Itm

Key

Mou

MM

Text

Win

Button

V

 

V

 

V

 

V

V

V

 

 

Canvas

 

 

V

 

V

 

V

V

V

 

 

Checkbox

 

 

V

 

V

V

V

V

V

 

 

CheckboxMenuItem

 

 

 

 

 

V

 

 

 

 

 

Choice

 

 

V

 

V

V

V

V

V

 

 

Component

 

 

V

 

V

 

V

V

V

 

 

Container

 

 

V

V

V

 

V

V

V

 

 

Dialog

 

 

V

V

V

 

V

V

V

 

V

Frame

 

 

V

V

V

 

V

V

V

 

V

Label

 

 

V

 

V

 

V

V

V

 

 

List

V

 

V

 

V

V

V

V

V

 

 

MenuItem

V

 

 

 

 

 

 

 

 

 

 

Panel

 

 

V

V

V

 

V

V

V

 

 

Scrollbar

 

V

V

 

V

 

V

V

V

 

 

ScrollPane

 

 

V

V

V

 

V

V

V

 

 

TextArea

 

 

V

 

V

 

V

V

V

V

 

TextField

V

 

V

 

V

 

V

V

V

V

 

Window

 

 

V

V

V

 

V

V

V

 

V

 

Act – ActionListener

Adj – AdjustmentListener

Cmp – ComponentListener

Cnt – ContainerListener

Foc – FocusListener

Itm – ItemListener

Key – KeyListener

Mou – MouseListener

MM – MouseMotionListener

Text – TextListener

Win – WindowListener

 

+ Recent posts