package dictionary;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
/*query = "create table Word " + "(name varchar(32), "
+ "mean varchar(200), " + "importance TINYINT , "
+ "count SMALLINT)";*/
public class DTable {
private static Connection con = null;
private static Statement stmt = null;
private static JTable table = null;
private JFrame frame = new JFrame();
private DefaultTableModel model = new DefaultTableModel();
public JFrame getFrame(){
return frame;
}
public JTable getTable(){
return table;
}
public DefaultTableModel getDModel(){
return model;
}
public void showWords() {
// Frame
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Table
table = new JTable(model);
table.getSelectionModel().addListSelectionListener(new RowListener());
D_DB.setTable(table, model);
JScrollPane scrollPane = new JScrollPane(table);
// Save
JPanel panel = new JPanel();
JButton button = new JButton("Save");
button.addActionListener(new SaveListener());
panel.add(button);
// 위치 조정
frame.getContentPane().add(panel, BorderLayout.NORTH);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setBounds(50, 50, 300, 300);
frame.setVisible(true);
}
class SaveListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
Dictionary d = new Dictionary();
d.createAndShowGUI();
}
}
class RowListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent event) {
if (event.getValueIsAdjusting()) {
return;
}
if (table.getSelectionModel().isSelectionEmpty() == false) {
int row = table.getSelectionModel().getLeadSelectionIndex();
int col = table.getColumnModel().getSelectionModel()
.getLeadSelectionIndex();
String word = (String) table.getValueAt(row, col);
String[] data = new String[2];
D_DB.getData(data, word);
if (data[0] != null && data[1] != null) {
Dictionary d = new Dictionary(data[0], data[1]);
d.createAndShowGUI();
}
}
}
}
}
이 부분은 단어장을 나타낸다. 즉 Dictionary Form에서 저장해 놓은 단어들을
보여주는 부분이다. 이를 Table에 나타내도록 하였다.
Table 에서 해당 줄을 클릭하면 그 단어의 뜻을 보여주는 창을 띄우도록
하였고 이를 위해서 Listener를 사용하였다.
댓글 없음:
댓글 쓰기