상세 컨텐츠

본문 제목

JPopupMenu 팝업메뉴 구성 관련

JAVA/JAVA UI

by AlrepondTech 2011. 9. 16. 14:45

본문

반응형

 

 

 

=================================

=================================

=================================

 

 

 

 

 

 

JPopupMenu 상속해서 구성하기

package com.cyberoro.wbaduk.common;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;

import java.net.URL;

public class CPopMenuextends JPopupMenu{
   
    JMenuItem items[] = null;
    //JRadioButtonMenuItem items[] = null;  //라디오 버튼 팝업메뉴구성
   
    public CPopMenu()
    {
        super();
    }
   
    public CPopMenu(String[] col)
    {
        super();
        addCols(col);
    }
   
    public void addCols(String[] col)
    {
        ButtonGroup group = new ButtonGroup();

        items = new JMenuItem[col.length];
        for(int cnt = 0; cnt<col.length; cnt++)
        {
            items[cnt] = new JMenuItem(col[cnt]);
            this.add(items[cnt]);
            group.add(items[cnt]);
        }
    }
   
    public void checkForTriggerEvt(MouseEvent e)
    {
        //if(e.isPopupTrigger())
        //{
            //this.show(e.getComponent(), e.getX(), e.getY());
        //}
       
        this.show(e.getComponent(), e.getX(), e.getY());
    }
}

 

 

=================================

=================================

=================================

 

 



사용예

main() //예로든 함수
{
    

//구성
     //
     String[] col ={"1","2","3"};

     CPopMenu pop = new CPopMenu(col );
  
     //or
 
     CPopMenu pop2 = new CPopMenu();
     pop2.addCols(col);

     //

    
    

//호출
     //
     버튼.리스너() //버튼 이외 리스너도 된다.
     {
        버튼클릭호출()
        {
            pop.checkForTriggerEvt(...);
        }
     }

}

 

 

=================================

=================================

=================================

 

 



출처: http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=1040201&docId=69952676&qb=anRhYmxlIHNldENvbXBvbmVudFBvcHVwTWVudQ==&enc=utf8&section=kin&rank=1&search_sort=0&spq=0&pid=gFY3TF5Y7tNsstWfPw0ssc--083987&sid=Tnv3WnW-e04AABtSeA4

import javax.swing.*;

 

public class JTablePopupMenu {
    public static void main(String[] args) {
        // 팝업메뉴 생성
        JPopupMenu popup = new JPopupMenu("예제 메뉴");
        popup.add("서울");
        popup.add("대전");
        popup.add("대구");
        popup.add("부산");
        popup.add("찍고");
        popup.add("빠밤");
       
        // 테이블 생성 및 팝업 지정
        JTable table = new JTable(4, 4);
        table.setComponentPopupMenu(popup);
       
        // 프레임 생성 및 화면에 디스플레이
        JFrame frame = new JFrame("popup test");
        frame.getContentPane().add(table);
        frame.setSize(400, 400);
        frame.setVisible(true);
    }
}

 

 

 

=================================

=================================

=================================

 

 

 

 

반응형


관련글 더보기

댓글 영역