스윙 콤포넌트 추가 방법 2 : JTable
참조 : 스윙 콤포넌트 추가 방법 1(기본 콤포넌트 추가 과정 설명) JFrame을 상속받아 MainUI 클래스 만들기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class MainUI extends JFrame{ public MainUI(){ this.setTitle("201712345 홍길동"); this.setSize(800, 600); this.setLayout(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 콤포넌트 추가는 이곳에 this.setVisible(true); } public static void main(String[] args) { new MainUI(); } } |
콤보넌트 예제 : 그림의 오른쪽 부분은 위 참조 사이트에 설명되어 있음 JTable 콤포넌트 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class MainUI extends JFrame{ JTable table; DefaultTableModel model; public MainUI(){ this.setTitle("201712345 홍길동"); this.setSize(800, 600); this.setLayout(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Vector<Object> vecs = null; Vector<Object> cols = null; model = new DefaultTableModel(vecs, cols); // model.setDataVector(vecs, cols); table = new JTable(model); JScrollPane scroll = new JScrollPane(table); scroll.setBounds(10, 10, 300, 480); table.setBackground(Color.green); //table.addMouseListener(this); // 리스너 등록 this.add(scroll); this.setVisible(true); } public static void main(String[] args) { new MainUI(); } } |
getTableHeader() 멤버 메서드 생성
1 2 3 4 5 6 7 8 9 10 11 |
public Vector<Object> getTableHeader() { Vector<Object> col = new Vector<Object>(); col.add("아이디"); col.add("이름"); col.add("전화"); col.add("학과"); col.add("성별"); col.add("이메일"); return col; } |
getListVector() 멤버 메서드 생성
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public Vector<Object> getListVector() { Vector<Object> data = new Vector<Object>(); for(int i=0; i<3; i++) { Vector<String> row = new Vector<String>(); row.add("아이디" + i); row.add("이름" + i); row.add("전화" + i); row.add("학과" + i); row.add("성별" + i); row.add("이메일" + i); data.add(row); } return data; } |
소스 수정
1 2 |
Vector<Object> vecs = getListVector(); Vector<Object> cols = getTableHeader(); |
getListVector() 멤버 메서드 수정(DAO 클래스로… Continue Reading 스윙 콤포넌트 추가 방법 2 : JTable