package java0918_gui;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
class Research2 extends JFrame implements ActionListener, MouseListener {
JTextField nameTxt;
JRadioButton manRad, womanRad;
JComboBox locBox;
JButton regBtn, saveBtn, openBtn;
JTable table;
// tableìì ì¬ì©ëë ë°ì´í°ë¥¼ ê´ë¦¬í´ì£¼ë ê°ì²´ì´ë¤.
DefaultTableModel model;
public Research2() {
nameTxt = new JTextField(10);
// ë¼ëì¤ë²í¼ ìì± ë° ê·¸ë£¹
manRad = new JRadioButton("ë¨", true);
womanRad = new JRadioButton("ì¬");
ButtonGroup bg = new ButtonGroup();
bg.add(manRad);
bg.add(womanRad);
// 콤보ë°ì¤ì ë°ì´í° ì½ì
String[] city = new String[] { "seoul", "jeju", "pusan", "daejon" };
locBox = new JComboBox(city);
regBtn = new JButton("ì ë³´ë±ë¡");
saveBtn = new JButton("íì¼ì ì¥");
openBtn = new JButton("íì¼ì´ê¸°");
JPanel jp1 = new JPanel();
jp1.add(new JLabel("ì´ë¦"));
jp1.add(nameTxt);
JPanel jp2 = new JPanel();
jp2.add(new JLabel("ì±ë³"));
jp2.add(manRad);
jp2.add(womanRad);
JPanel jp3 = new JPanel();
jp3.add(new JLabel("ì§ì"));
jp3.add(locBox);
JPanel jp4 = new JPanel();
jp4.add(regBtn);
jp4.add(saveBtn);
jp4.add(openBtn);
JPanel top = new JPanel(new GridLayout(4, 1));
top.add(jp1);
top.add(jp2);
top.add(jp3);
top.add(jp4);
// í
ì´ë¸ì 컬ë¼ëª
ì ë°°ì´ì ì ì¥íë¤.
String[] columnNames = { "ì´ë¦", "ì±ë³", "ì§ì" };
// í
ì´ë¸ì ë°ì´í°ë¥¼ ê´ë¦¬í´ì¤ model ì ìì±íë¤.
// ìì±ì를 í¸ì¶í ë 컬ë¼ëª
, íì ê°¯ì를 ë겨ì¤ë¤.
model = new DefaultTableModel(columnNames, 0);
table = new JTable(model);
// í
ì´ë¸ì ì¬ì´ì¦ë¥¼ ê³ ì ìí¨ë¤.
table.setAutoResizeMode(0);
JTableHeader header = table.getTableHeader();
// í
ì´ë¸ì 컬ë¼ëª
ì ê³ ì ìí¨ë¤.
header.setReorderingAllowed(false);
// 컬ë¼ëª¨ë¸ì 리í´íë¤.
TableColumnModel columnModel = header.getColumnModel();
// 컬ë¼ë³ë¡ í¬ê¸°ë¥¼ ì¤ì íë¤.
columnModel.getColumn(0).setPreferredWidth(100);
columnModel.getColumn(1).setPreferredWidth(50);
columnModel.getColumn(2).setPreferredWidth(150);
// í
ì´ë¸ì íì ëì´ë¥¼ ì¤ì íë¤.
table.setRowHeight(25);
// í
ì´ë¸ í¸ì§ ë¶ê°
table.setEnabled(false);
// JFrameì Layoutì GridLayout 2í 1ì´ë¡ ë³ê²½íë¤.
setLayout(new GridLayout(2, 1));
// JFrameì ì»´í¬ëí¸ë¥¼ ì°ê²°íë¤.
add(top);
add(new JScrollPane(table));
// ë²í¼ì 리ì¤ë를 ì°ê²°íë¤.
regBtn.addActionListener(this);
saveBtn.addActionListener(this);
openBtn.addActionListener(this);
regBtn.addMouseListener(this);
saveBtn.addMouseListener(this);
openBtn.addMouseListener(this);
// ìëì°ì°½ ìì±
setSize(300, 300);
setLocation(400, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// ì´ë²¤í¸ê° ë°ìë ì¤ë¸ì í¸ë¥¼ 리í´í¨
Object obj = e.getSource();
if (obj == saveBtn) {
saveMethod();
} else if (obj == openBtn) {
openMethod();
} else if (obj == regBtn) {
regMethod();
}
}
public void init() {
nameTxt.setText("");
manRad.setSelected(true);
locBox.setSelectedIndex(0);
nameTxt.requestFocus();
}
private void saveMethod() {
JFileChooser save = new JFileChooser();
int chk = save.showSaveDialog(this);
// ì·¨ì를 ì í í ê²½ì° ìë¬ ìì´ ì·¨ì í ì ìê² í¨
if (chk == JFileChooser.CANCEL_OPTION) {
return;
}
File file = save.getSelectedFile();
FileWriter fw = null;
try {
// false : ì
ë°ì´í¸ , true : append , 기본 ê°ì false
fw = new FileWriter(file);
for (int i = 0; i < table.getRowCount(); i++) {
String name = (String) table.getValueAt(i, 0);
String gen = (String) table.getValueAt(i, 1);
String loc = (String) table.getValueAt(i, 2);
fw.write(name + "/" + gen + "/" + loc + "\r\n");
}
fw.flush();
JOptionPane.showMessageDialog(this, "ì ì¥ëììµëë¤.");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void openMethod() {
JFileChooser open = new JFileChooser();
int chk = open.showOpenDialog(this);
if (chk == JFileChooser.CANCEL_OPTION) {
return;
}
File file = open.getSelectedFile();
Scanner sc = null;
try {
sc = new Scanner(file);
model.setRowCount(0);
while (sc.hasNextLine()) {
String[] line = sc.nextLine().split("/");
model.addRow(line);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
sc.close();
}
}
private void regMethod() {
String[] line = new String[3];
line[0] = nameTxt.getText();
line[1] = manRad.isSelected() ? "ë¨" : "ì¬";
line[2] = (String) locBox.getSelectedItem();
model.addRow(line);
init();
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
Object obj = e.getSource();
if (obj == regBtn) {
regBtn.setText("REGISTER");
} else if (obj == saveBtn) {
saveBtn.setText("SAVE");
} else if (obj == openBtn) {
openBtn.setText("OPEN");
}
}
@Override
public void mouseExited(MouseEvent e) {
Object obj = e.getSource();
if (obj == regBtn) {
regBtn.setText("ì ë³´ë±ë¡");
} else if (obj == saveBtn) {
saveBtn.setText("íì¼ì ì¥");
} else if (obj == openBtn) {
openBtn.setText("íì¼ì´ê¸°");
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
}
public class Java223_gui {
public static void main(String[] args) {
new Research2();
}
}