import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
class Round23_Ex08_Sub extends JFrame {
private JRootPane jrp;
private Container con;
//private JTable jt = new JTable();
//private JTable jt - new JTable(3, 4);
private String[][] str = {
{"1_1", "1_2", "1_3", "1_4"}
, {"2_1", "2_2", "2_3", "2_4"}
, {"3_1", "3_2", "3_3", "3_4"}
};
private String[] str1 = {"1번", "2번", "3번", "4번"};
private JTable jt = new JTable(str, str1);
private JScrollPane jsp = new JScrollPane(jt);
public Round23_Ex08_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
}
public void init() {
jrp = this.getRootPane();
con = jrp.getContentPane();
con.setLayout(new BorderLayout(5, 5));
con.add("North", new JLable("JTable !!", JLabel.CENTER));
JPanel jp = new JPanel(new FlwoLayout(FlowLayout.RIGHT));
jp.add(new JButton("확인"));
jp.add(new JButton("취소"));
con.add("South", jp);
con.add("Center", jsp);
}
public void start() {
}
}
public class Round23_Ex08 {
public static void main(String[] args) {
new Round23_Ex08_Sub();
}
}
DefaultTableModel 로 필드의 셀들에 변화를 줄때
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;
class Round23_Ex09_Sub extends JFrame {
private JRootPane jrp;
private Container con;
//private DefaultTableModel dtm = new DefaultTableModel();
//private DefaultTableModel dtm = new DefaultTableModel(3, 4);
/*
private String[][] str = {
{"1_1", "1_2", "1_3", "1_4"}
, {"2_1", "2_2", "2_3", "2_4"}
, {"3_1", "3_2", "3_3", "3_4"}
};
*/
//private String[] str1 = {"1번", "2번", "3번", "4번"};
//private DefaultTableModel dtm = new DefaultTableModel(str, str1);
private String[] str = {"1번", "2번", "3번", "4번"};
private DefaultTableModel dtm = new DefaultTableModel(str, 5);
private JTable jt = new JTable(dtm);
private JScrollPane jsp = new JScrollPane(jt);
public Round23_Ex09_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
//System.out.println("1 Column's Class Name = " + dtm.getColumnClass(0));
//System.out.println("Column Count = " + dtm.getColumnCount());
//System.out.println("3 Column's Class Name = " + dtm.getColumnClass(2));
//System.out.println("Row Count = " + dtm.getRowCount());
dtm.setValueAt("Test", 2, 2);
//System.out.println("2, 2 Editable = " + dtm.isCellEditable(2, 2));
//System.out.println("2번 Column's Pos = " + dtm.findColumn("2번"));
try { Thread.sleep(2000); } catch (InterruptedException e) { }
dtm.addColumn("5번", new String[] {"1", "2", "3", "4", "5"});
try { Thread.sleep(2000); } catch (InterruptedException e) { }
dtm.addRow(new String[] {"true", "true", "true", "true", "true"});
try { Thread.sleep(2000); } catch (InterruptedException e) { }
dtm.insertRow(2, new String[] {"false", "false", "false", "false", "false"});
try { Thread.sleep(2000); } catch (InterruptedException e) { }
dtm.moveRow(1, 3, 0);
dtm.setColumnIdentifiers(new String[] {"11", "22", "33", "44", "55"});
dtm.setRowCount(14); //dtm.setNumRows(14);
}
public void init() {
jrp = this.getRootPane();
con = jrp.getContentPane();
con.setLayout(new BorderLayout(5, 5));
con.add("North", new JLable("JTable !!", JLabel.CENTER));
JPanel jp = new JPanel(new FlwoLayout(FlowLayout.RIGHT));
jp.add(new JButton("확인"));
jp.add(new JButton("취소"));
con.add("South", jp);
con.add("Center", jsp);
}
public void start() {
}
}
public class Round23_Ex09 {
public static void main(String[] args) {
new Round23_Ex09_Sub();
}
}
DefaultTableColumnModel, DefaultTableCellRenderer, DefaultCellEditor 테이블형태
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;
class Round23_Ex10_Sub extends JFrame {
private JRootPane jrp;
private Container con;
private String[][] str = {
{"1_1", "1_2", "1_3", "1_4"}
, {"2_1", "2_2", "2_3", "2_4"}
, {"3_1", "3_2", "3_3", "3_4"}
};
private String[] str1 = {"1번", "2번", "3번", "4번"};
private DefaultTableModel dtm = new DefaultTableModel(str, str1);
private DefaultTableColumnModel dtcm = new DefaultTableColumnModel();
private JTable jt = new JTable(dtm, dtcm);
private JScrollPane jsp = new JScrollPane(jt);
private TableColumn tc, tc1, tc2, tc3;
private DefaultTableCellRenderer dtcr;
private DefaultCellEditor dce;
public Round23_Ex10_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
}
public void init() {
jrp = this.getRootPane();
con = jrp.getContentPane();
con.setLayout(new BorderLayout(5, 5));
con.add("North", new JLable("JTable !!", JLabel.CENTER));
JPanel jp = new JPanel(new FlwoLayout(FlowLayout.RIGHT));
jp.add(new JButton("확인"));
jp.add(new JButton("취소"));
con.add("South", jp);
tc = new TableColumn(); //0 ,75 기준폭은 75픽셀
tc.setHeaderValue("1번");
tc1 = new TableColumn(1); //1 ,75
tc1.setHeaderValue("2번");
tc2 = new TableColumn(2, 125); //2 ,125
tc2.setHeaderValue("3번");
dtcr = new DefaultTableCellRenderer();
dtcr.setBackground(Color.red);
dtcr.setForeground(Color.blue);
dce = new DefaultCellEditor(new JCheckBox());
tc3 = new TableColumn(3, 30, dtcr, dec); //3 ,20
tc3.setHeaderValue("4번");
dtcm.addColumn(tc);
dtcm.addColumn(tc1);
dtcm.addColumn(tc2);
dtcm.addColumn(tc3);
con.add("Center", jsp);
}
public void start() {
}
}
public class Round23_Ex10 {
public static void main(String[] args) {
new Round23_Ex10_Sub();
}
}
DefaultSelectionModel 클래스로 리스트행 관리
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;
class Round23_Ex11_Sub extends JFrame {
private JRootPane jrp;
private Container con;
private String[][] str = {
{"1_1", "1_2", "1_3", "1_4"}
, {"2_1", "2_2", "2_3", "2_4"}
, {"3_1", "3_2", "3_3", "3_4"}
};
private String[] str1 = {"1번", "2번", "3번", "4번"};
private DefaultTableModel dtm = new DefaultTableModel(str, str1);
private DefaultTableColumnModel dtcm = new DefaultTableColumnModel();
private DefaultListSelectionModel dlsm = new DefaultListSelectionModel();
private JTable jt = new JTable(dtm, dtcm);
private JScrollPane jsp = new JScrollPane(jt);
private TableColumn tc, tc1, tc2, tc3;
private DefaultTableCellRenderer dtcr;
private DefaultCellEditor dce;
private JTableHeader jth = new JTableHeadar();
public Round23_Ex11_Sub() {
super("Test");
this.init();
this.start();
this.setSize(300, 200);
this.setVisible(true);
}
public void init() {
jrp = this.getRootPane();
con = jrp.getContentPane();
con.setLayout(new BorderLayout(5, 5));
con.add("North", new JLable("JTable !!", JLabel.CENTER));
JPanel jp = new JPanel(new FlwoLayout(FlowLayout.RIGHT));
jp.add(new JButton("확인"));
jp.add(new JButton("취소"));
con.add("South", jp);
tc = new TableColumn(); //0 ,75 기준폭은 75픽셀
tc.setHeaderValue("1번");
tc1 = new TableColumn(1); //1 ,75
tc1.setHeaderValue("2번");
tc2 = new TableColumn(2, 125); //2 ,125
tc2.setHeaderValue("3번");
dtcr = new DefaultTableCellRenderer();
dtcr.setBackground(Color.red);
dtcr.setForeground(Color.blue);
dce = new DefaultCellEditor(new JCheckBox());
tc3 = new TableColumn(3, 30, dtcr, dec); //3 ,20
tc3.setHeaderValue("4번");
dtcm.addColumn(tc);
dtcm.addColumn(tc1);
dtcm.addColumn(tc2);
dtcm.addColumn(tc3);
dlsm.addSelectionInterval(0, 2);
dlsm.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
jth.setColumnModel(dtcm);
jth.setReorderingAllowed(false);
jth.setResizinAllowed(false);
jt.setTableHeader(jth);
con.add("Center", jsp);
}
public void start() {
}
}
public class Round23_Ex11 {
public static void main(String[] args) {
new Round23_Ex11_Sub();
}
}
윈도우 검색창 만들기
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;
class Round23_Ex12_Sub extends JFrame implements ActionListener {
private Container con;
private JLabel title_lb = new JLabel("파일 검색", JLabel.CENTER);
private JTextField search_tf = new JTextField(15);
private Vector condition_vc = new Vector();
private JComboBox condition_jcb = new JComboBox(condition_vc);
private JScrollPane condtion_jsp = new JScrollPane(condition_jcb);
private JButton search_bt = new JButton("검색시작");
private JLabel result_lb = new JLabel("RESULTS");
private JLabel view_lb = new JLabel();
private Vector data_vc = new Vector();
private Vector field_vc = new Vector();
private JTable view_jt;
private JScrollPane view_jsp;
private JButton clear_bt = new JButton("CLEAR");
privaet JButton end_bt = new JButton("END");
private JPanel jpjp;
public Round23_Ex12_Sub() {
super("My Search");
this.init();
this.start();
this.setSize(700, 500);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = this.getSize();
int xpos = (int)(screen.getWidth()/2 - frm.getWidth()/2);
int ypos = (int)(screen.getHeight()/2 - frm.getHeight()/2);
this.setLocation(xpos, ypos);
this.setVisible(true);
}
public void init() {
con = this.getContentPane();
con.setLayout(new BorderLayout(10, 10));
JPanel jp = new JPanel(new BorderLayout());
title_lb.setFont(new Font("Sans-Serif", Font.BOLD, 14));
con.add("North", title_lb);
JPanel jp1 = new JPanel(new GridLaout(3, 1));
jp1.setBorder(new BevelBorder(BevelBorder.RAISED));
jp1.setBackground(Color.white);
JPanel jp2 = new JPanel(new BorderLayout());
jp2.setBorder(new TitleBorder("파일검색"));
JPanel jp3 = new JPanel(new GridLaout(2, 1));
search_tf.setBorder(new TitleBorder("검색조건"));
jp3.add(search_tf);
condition_jsp.setBorder(new TitleBorder("검색범위"));
jp3.add(condition_jsp);
jp2.add("Center",jp3);
JPanel jp4 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
jp4.add(search_bt);
jp2.add("South", jp4);
jp1.add(jp2);
jp1.add(new JLabel());
jp1.add(new JLabel());
jp.add("Center", jp1);
jp.setPreferredSize(new Dimension(150, 150));
con.add("West", jp);
jpjp = new Panel(new BorderLayout(10, 10));
JPanel jpjp1 = new JPanel(new BorderLayout(5, 5));
result_lb.setFont(new Font("Sans-Serif", Font.BOLD, 14));
jpjp1.add("West",result_lb);
view_lb.setBorder(new BevelBorder(BevelBorder.LOWERED));
jpjp1.setPreferredSize(new Dimension(550, 50));
JPanel jpjpjp = new JPanel(new BorderLayout());
jpjpjp.setBackground(Color.white);
// view_lb.setBackground(Color.white);
// view_lb.setForeground(Color.black);
jpjpjp.add("Center",view_lb);
jpjp1.add("Center",jpjpjp);
jpjp.add("North",jpjp1);
field_vc.add("파일명");
field_vc.add("파일경로");
field_vc.add("파일크기");
field_vc.add("최종수정일");
field_vc.add("파일종류");
view_jt = new JTable(data_vc, field_vc);
view_jsp = new JScrollPane(view_jt);
jpjp.add("Center", view_jsp);
JPanel jpjp2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
jpjp2.add(clear_bt);
jpjp2.add(end_bt);
jpjp.add("South", jpjp2);
con.add("Center",jpjp);
File[] root = File.listRoots();
condition_vc.add("전체범위");
for(int i=0; i< root.length; i++) {
condition_vc.add(root[i].toString());
}
condition_jcb.updateUI();
}
public void start() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
search_bt.addActionLister(this);
search_tf.addActionLister(this);
clear_bt.addActionLister(this);
end_bt.addActionLister(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == search_tf || e.getSource() == search_bt) {
actionSearch();
} else if (e.getSource() == clear_bt) {
actionClear();
} else if (e.getSource() == end_bt) {
actionEnd();
}
}
private void actionSearch(){
String data = search_tf.getText();
if(data == null || data.trim().length() == 0 ) return ;
int condition = getCondition(data.trim());
String str = condition_jcb.getSelectedItem();
if( str.trim().equals("전체범위")) {
File[] root = File.listRoots();
for(int i = 1; i < root.length; i++ ) {
new MyThread(root[i].toString(), data, condition);
}
} else {
new MyThread(str.trim(), data, condition);
}
}
private int getCondition(String data) {
char ch = data.charAt(0);
char ch1 = data.charAt(data.length() - 1);
if( ch == '*' && ch1 == '*') return 3;
else if( ch == '*' ) return 1;
else if( ch1 == '*' ) return 2;
return 0;
}
private actionClear() {
//data_vc.add(imsi);
//view_jt.updateUI();
data_vc.clear();
jpjp.remove(view_jsp);
view_jt = new JTable(data_vc, field_vc);
view_jsp = new JScrollPane(view_jt);
jpjp.add("Center", view_jsp);
jpjp.validate();
}
private actionEnd() {
System.exit(0);
}
private void updateTable(Vector imsi) {
data_vc.add(imsi);
//view_jt.updateUI();
jpjp.remove(view_jsp);
view_jt = new JTable(data_vc, field_vc);
view_jsp = new JScrollPane(view_jt);
jpjp.add("Center", view_jsp);
jpjp.validate();
}
class MyThread1 extends Thread {
private String path;
public MyThread1(String path) {
this.path = path;
this.start();
}
public void run() {
view_lb.setText(path);
}
}
class MyThread extends Thread {
private String path;
private String data;
private int condition;
public MyThread(String path, String data, int condition) {
this.path = path;
this.data = data;
this.condition = condition;
}
public void run() {
this.searchDirectory();
}
private void searchDirectory() {
File[] files = new File(path).listFiles();
if(files == null) return ;
for(int i=0; i<files.length; i++) {
//view_lb.setText(files[i].getPath());
new Thread1(files[i].getPath());
if(condition == 0) {
update(
files[i].getName().equals(data)
,files[i]
);
} else if (condition == 1) {
update(
files[i].getName().endWith(data.substring(1))
,files[i]
);
} else if (condition == 2) {
update(
files[i].getName().startWith(data.substring(0, data.length()-1))
,files[i]
);
} else if (condition == 3) {
update(
files[i].getName().indexOf(data.substring(1, data.length()-1))
,files[i]
);
}
}
//view_lb.setText("");
for(int i=0; i < files.length; i++ ) {
if( files[i].isDirectory()) {
new MyThread(path + File.sparator + files[i].getName(), data, condition);
}
}
}
private void update( boolean bool, File f ) {
if ( bool ) {
Vector imsi = getVector(f);
//data_vc.add(imsi);
//view_jt.updateUI();
updateTable(imsi);
}
}
private Vector getVector(File f) {
Vector imsi = new Vector();
imsi.add(f.getName());
imsi.add(f.getParent());
imsi.add(String.valueOf(files[i].length());
imsi.add(new Date(f.lastModified()).toString());
imsi.add(f.isDirectory() ? "폴더" : "파일");
}
}
}
public class Round23_Ex12 {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch( Exception e ) {}
new Round23_Ex12_Sub();
}
}
과제
과제1. 다음 화면은 엑셀 창을 내부 프레임(Internal Frame)으로 띄운 것이다. 이벤트 없이 화면의 사용자 인터페이스만 구성되어 있으므로 화면을 보고 만들어 보자.
과제2 다음 예제는 셀을 이용하여 성적 처리를 하는 프로그램이다. 소스를 이해해 보도록 하자.
과제3 다음 예제는 인사관리 ERP를 축소시켜 만들어 보았다. 직접 전체를 구성해 보는 것도 좋지만 먼저 코딩을 해보고 전체를 실행시켜 본 이후에 다시 한번 만들어 보는 것도 괜찮다.