숙제-중복체크 완성하기
package dev_java.week4;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class DeptTable7_1 extends JFrame implements ActionListener {
// 선언부
// 파라미터의 this는 DeptTable7_1 타입이고 main에서 호출된 생성자로
// 현재 메모리에 로딩 중인 객체를 가리킨다
// JTable7Dialog에서 부서번호, 부서명, 지역을 입력한 후 저장버튼을 누르면
// Vector에 추가하고 추가된 로우를 포함하는 Vector가 부모창에
// 새로 리프레쉬(재사용>부모클래스에 구현) 되어야 하니까 jtd를 호출해야함
JTable7Dialog_1 jtd7 = new JTable7Dialog_1(this);
String header[] = { "부서번호", "부서명", "지역" };
String datas[][] = new String[0][3];
DefaultTableModel dtm_dept = new DefaultTableModel(datas, header);
JTable jtb_dept = new JTable(dtm_dept);
JScrollPane jsp_dept = new JScrollPane(jtb_dept,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JPanel jp_north = new JPanel();
JButton jtbn_sel = new JButton("조회");
JButton jtbn_ins = new JButton("입력");
JButton jtbn_upd = new JButton("수정");
JButton jtbn_del = new JButton("삭제");
JButton jtbn_det = new JButton("상세보기");
// DeptVO 벡터 선언 및 생성
static Vector<DeptVO> vData = new Vector<>(); // vData.size() = 0
// 생성자
public DeptTable7_1() {
initDisplay();
}
// 화면그리기
public void initDisplay() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
jp_north.setLayout(new FlowLayout(FlowLayout.RIGHT));
// 버튼 색 설정
jtbn_sel.setBackground(new Color(150, 100, 150));
jtbn_sel.setForeground(Color.WHITE);
jtbn_ins.setBackground(new Color(19, 99, 57));
jtbn_ins.setForeground(Color.WHITE);
jtbn_upd.setBackground(new Color(7, 84, 170));
jtbn_upd.setForeground(Color.WHITE);
jtbn_del.setBackground(new Color(200, 50, 50));
jtbn_del.setForeground(Color.WHITE);
jtbn_det.setBackground(new Color(250, 200, 50));
jtbn_det.setForeground(Color.WHITE);
// 버튼 추가
jp_north.add(jtbn_sel);
jp_north.add(jtbn_ins);
jp_north.add(jtbn_upd);
jp_north.add(jtbn_del);
jp_north.add(jtbn_det);
// 이벤트리스너 연결
jtbn_sel.addActionListener(this);
jtbn_ins.addActionListener(this);
jtbn_upd.addActionListener(this);
jtbn_del.addActionListener(this);
jtbn_det.addActionListener(this);
// this(상속받은 JFrame)에 추가
this.add("North", jp_north);
this.add("Center", jsp_dept);
this.setTitle("부서관리 시스템 Ver1.0");
this.setSize(500, 400);
this.setVisible(true);
}
// 새로고침-Vector에 담긴것 출력하기
// 입력, 수정화면에서 저장 버튼을 누르면 Vector에 String[]을 추가하고
// 그 다이얼로그 화면은 닫히고 부모창은 새로고침 처리한다
// 다이얼로그 창에서 부모클래스의 refreshData메소드를 호출해야함
// 그러니 인스턴스화할 때 파라미터에 this를 넘겨서 사용할 수 있도록 할 것(NullPointerException발생하지 않도록)
public void refreshData() {
System.out.println("refreshData 호출");
if (DeptTable7_1.vData.size() <= 0) {
JOptionPane.showMessageDialog(this, "조회결과가 없습니다.", "WARNING", JOptionPane.ERROR_MESSAGE);
return; // refreshData() 탈출
}
System.out.println("DeptTable7_1: " + vData.size());
// 입력, 수정 전에 조회된 정보는 삭제함
while (dtm_dept.getRowCount() > 0) {
dtm_dept.removeRow(0);
}
// 벡터의 크기만큼 반복하면서 dtm_dept 데이터셋에 vData추가
for (int i = 0; i < vData.size(); i++) {
DeptVO oneRow = vData.get(i);
Vector<Object> vOne = new Vector<>();
vOne.add(oneRow.getDeptNo());
vOne.add(oneRow.getDeptName());
vOne.add(oneRow.getDeptLoc());
dtm_dept.addRow(vOne);
}
}
@Override
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj == jtbn_sel) { // 조회버튼을 눌렀을 때
refreshData();
} else if (obj == jtbn_ins) { // 입력버튼을 눌렀을 때
jtd7.set("입력", true, null, true);
} else if (obj == jtbn_upd) { // 수정버튼을 눌렀을 때
int index = jtb_dept.getSelectedRow();
if (index == -1) { // -1은 end of file의 의미
JOptionPane.showMessageDialog(null, "수정을 원하는 부서를 선택하세요.", "불러오기 실패", JOptionPane.ERROR_MESSAGE);
} else {
// 어떤 row를 수정할지 확인
// JTable 목록에서 선택한 로우의 index값을 가져옴
// 데이터셋객체로 벡터를 사용중이니 벡터에서 꺼낸 값으로 String[] 초기화
// 테이블의 양식 폼인 JTable 이벤트로 얻어옴
DeptVO pdVO = vData.get(index);
jtd7.set("수정", true, pdVO, true);
}
} else if (obj == jtbn_del) {
int index = jtb_dept.getSelectedRow();
if (index == -1) { // -1은 end of file의 의미
JOptionPane.showMessageDialog(null, "삭제를 원하는 부서를 선택하세요.", "불러오기 실패", JOptionPane.ERROR_MESSAGE);
} else {
vData.remove(index);
dtm_dept.removeRow(index);
}
} else if (obj == jtbn_det) { // 상세보기를 눌렀을 때
int index = jtb_dept.getSelectedRow();
if (index == -1) {
JOptionPane.showMessageDialog(null, "상세보기를 원하는 부서를 선택하세요.", "불러오기 실패", JOptionPane.ERROR_MESSAGE);
return;
} else {
DeptVO pdVO = vData.get(index);
jtd7.set("상세보기", true, pdVO, false);
}
}
}
// 메인메소드
public static void main(String[] args) {
new DeptTable7_1();
}
}
package dev_java.week4;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JTable7Dialog_1 extends JDialog implements ActionListener {
// 선언부
DeptTable7_1 dt7 = null;
JPanel jp_center = new JPanel();
JScrollPane jsp_center = new JScrollPane(jp_center);
JPanel jp_south = new JPanel();
JLabel jlb_deptNo = new JLabel("부서번호");
JTextField jtf_deptNo = new JTextField(10);
JLabel jlb_deptName = new JLabel("부서명");
JTextField jtf_deptName = new JTextField(20);
JLabel jlb_deptLoc = new JLabel("지역");
JTextField jtf_deptLoc = new JTextField(20);
JButton jbtn_check = new JButton("중복검사");
// jp_south속지에 버튼 붙임
JButton jbtn_save = new JButton("저장");
JButton jbtn_close = new JButton("닫기");
JButton jbtn_sample = new JButton("자동입력");
// DeptVO 선언
DeptVO pdVO = null;
// 생성자
public JTable7Dialog_1(DeptTable7_1 dt7) {
this.dt7 = dt7;
initDisplay();
}
// 화면그리기
public void initDisplay() {
// 레이아웃 설정
jp_center.setLayout(null);
jp_south.setLayout(new FlowLayout(FlowLayout.RIGHT));
jlb_deptNo.setBounds(20, 20, 80, 20);
jtf_deptNo.setBounds(120, 20, 80, 20);
jbtn_check.setBounds(210, 20, 90, 20);
jlb_deptName.setBounds(20, 45, 100, 20);
jtf_deptName.setBounds(120, 45, 150, 20);
jlb_deptLoc.setBounds(20, 70, 100, 20);
jtf_deptLoc.setBounds(120, 70, 150, 20);
jp_center.add(jlb_deptNo);
jp_center.add(jtf_deptNo);
jp_center.add(jbtn_check);
jp_center.add(jlb_deptName);
jp_center.add(jtf_deptName);
jp_center.add(jlb_deptLoc);
jp_center.add(jtf_deptLoc);
jp_south.add(jbtn_save);
jp_south.add(jbtn_close);
jp_south.add(jbtn_sample);
jbtn_save.addActionListener(this);
jbtn_close.addActionListener(this);
jbtn_sample.addActionListener(this);
jbtn_check.addActionListener(this);
this.add("Center", jsp_center);
this.add("South", jp_south);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setTitle("상세보기");
this.setSize(400, 360);
this.setVisible(false);
}
// 각 컬럼(부서집합-부서번호, 부서명, 지역)의 값들을
// 설정하거나 읽어오는 getter/setter 메소드
public String getDeptNo() {
return jtf_deptNo.getText();
}
public void setDeptNo(String deptNo) {
jtf_deptNo.setText(deptNo);
}
public String getDeptName() {
return jtf_deptName.getText();
}
public void setDeptName(String deptName) {
jtf_deptName.setText(deptName);
}
public String getDeptLoc() {
return jtf_deptLoc.getText();
}
public void setDeptLoc(String deptLoc) {
jtf_deptLoc.setText(deptLoc);
}
// 아래 메소드는 DeptTable7_1에서 호출됨
// actionPerformed에서 이벤트(입력, 수정, 상세보기)가 발생되면 호출됨
public void set(String title, boolean isView, DeptVO pdVO, boolean isEdit) {
this.setTitle(title);
this.setVisible(isView);
this.pdVO = pdVO;
setValue(pdVO);
setEditable(isEdit);
}
// 입력 혹은 수정일때는 true로 처리하고
// 상세보기일때는 false를 주어서 read only로 만든다
private void setEditable(boolean isEdit) {
jtf_deptNo.setEditable(isEdit);
jtf_deptName.setEditable(isEdit);
jtf_deptLoc.setEditable(isEdit);
jbtn_sample.setVisible(isEdit);
jbtn_save.setVisible(isEdit);
jbtn_check.setVisible(isEdit);
}
public void setValue(DeptVO pdVO) {
// 입력을 위한 윈도우 설정-모든 값을 빈문자열로 세팅
// 상세조회, 수정시는 받은 값으로 세팅
if (pdVO == null) {
setDeptNo("");
setDeptName("");
setDeptLoc("");
} else {
setDeptNo(String.valueOf(pdVO.getDeptNo()));
setDeptName(pdVO.getDeptName());
setDeptLoc(pdVO.getDeptLoc());
}
} // end of setValue
// 메인
public static void main(String[] args) {
new JTable7Dialog_1(null);
}
@Override
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj == jbtn_save) {
// pdVO가 존재하면 수정모드, 그렇지 않으면 입력모드
// 다이얼로그 화면에서 저장버튼 하나로 입력, 수정처리
// 부모창에서 버튼이 눌려졌을때 set메소드 호출하는데
// 입력이면 세번째 파라미터에 null을 입력
// 수정이면 JTable에 선택된 로우의 인덱스 추출해서
// 세번째 파라미터에null 대신 입력한다
if (pdVO != null) { // 수정모드
for (int i = 0; i < DeptTable7_1.vData.size(); i++) {
DeptVO comVO = DeptTable7_1.vData.get(i);
// dt7에서 받아온 부서번호와 벡터에서 꺼낸 부서번호가 같은가
if (pdVO.getDeptNo() == comVO.getDeptNo()) {
DeptVO updVO = DeptVO.builder().deptNo(Integer.parseInt(getDeptNo())).deptName(getDeptName())
.deptLoc(getDeptLoc()).build();
DeptTable7_1.vData.remove(i);
DeptTable7_1.vData.add(i, updVO);
break;
}
}
dt7.refreshData();
this.dispose();
} else { // 입력모드
DeptVO insVO = DeptVO.builder().deptNo(Integer.parseInt(jtf_deptNo.getText())).deptName(jtf_deptName.getText())
.deptLoc(jtf_deptLoc.getText()).build();
System.out.println(insVO.getDeptNo() + ", " + insVO.getDeptName() + ", " + insVO.getDeptLoc());
// System.out.println("before: " + DeptTable7_1.vData.size());
DeptTable7_1.vData.add(insVO);
// System.out.println("after: " + DeptTable7_1.vData.size());
dt7.refreshData();
this.dispose();
} // end of save
} else if (obj == jbtn_sample) {
setDeptNo("30");
setDeptName("운영부");
setDeptLoc("세종");
} else if (obj == jbtn_check) {
boolean isOk = true;
try {
for (int i = 0; i < DeptTable7_1.vData.size(); i++) {
DeptVO comVO = DeptTable7_1.vData.get(i);
if (Integer.parseInt(jtf_deptNo.getText()) == comVO.getDeptNo()) {
JOptionPane.showMessageDialog(this, "중복된 부서 번호입니다!", "중복확인 결과", JOptionPane.WARNING_MESSAGE, null);
isOk = false;
}
}
if (jtf_deptNo.getText().isEmpty()) {
JOptionPane.showMessageDialog(this, "값을 입력해주세요.", "입력값 없음", JOptionPane.ERROR_MESSAGE, null);
} else if (isOk) {
JOptionPane.showMessageDialog(this, "중복되지 않은 부서번호입니다.", "중복확인 결과", JOptionPane.INFORMATION_MESSAGE, null);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "값을 입력해주세요.", "입력값 없음", JOptionPane.ERROR_MESSAGE, null);
}
} else if (obj == jbtn_close) {
this.dispose();
}
}
}
'국비학원 > 숙제' 카테고리의 다른 글
DAY27_오라클 연습문제 (0) | 2023.01.03 |
---|---|
DAY26_오라클 연습문제 (0) | 2023.01.02 |
Day18_단순연산 계산기 (0) | 2022.12.27 |
Day14_자바 연습문제 (0) | 2022.12.27 |
Day13_랜덤 숫자 게임 (0) | 2022.12.27 |
댓글