Table
unknown
java
3 years ago
3.0 kB
14
Indexable
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.Vector;
public class Table extends JFrame {
public Table() {
addControls();
addEvents();
}
private DefaultTableModel dtm;
private JTable tblMain;
private JTextField txtId, txtName;
private JTextArea txtNote;
private JButton btnSave;
private void addControls() {
Container con = getContentPane();
JPanel pnMain = new JPanel();
pnMain.setLayout(new BoxLayout(pnMain, BoxLayout.Y_AXIS));
JPanel pnTable = new JPanel();
pnTable.setLayout(new BorderLayout());
dtm = new DefaultTableModel();
dtm.addColumn("MSSV");
dtm.addColumn("Họ tên");
dtm.addColumn("Ghi chú");
tblMain = new JTable(dtm);
JScrollPane scr = new JScrollPane(tblMain, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
pnTable.add(scr, BorderLayout.CENTER);
pnMain.add(pnTable);
JPanel pnTextField = new JPanel();
pnTextField.setLayout(new BoxLayout(pnTextField, BoxLayout.Y_AXIS));
JPanel pnId = new JPanel();
JLabel lblId = new JLabel("MSSV", SwingConstants.RIGHT);
txtId = new JTextField(20);
pnId.add(lblId);
pnId.add(txtId);
JPanel pnName = new JPanel();
JLabel lblName = new JLabel("Họ tên", SwingConstants.RIGHT);
txtName = new JTextField(20);
pnName.add(lblName);
pnName.add(txtName);
JPanel pnNote = new JPanel();
JLabel lblNote = new JLabel("Ghi chú", SwingConstants.RIGHT);
txtNote = new JTextArea(5, 20);
pnNote.add(lblNote);
pnNote.add(txtNote);
pnTextField.add(pnId);
pnTextField.add(pnName);
pnTextField.add(pnNote);
lblId.setPreferredSize(lblNote.getPreferredSize());
lblName.setPreferredSize(lblNote.getPreferredSize());
pnMain.add(pnTextField);
JPanel pnButton = new JPanel();
btnSave = new JButton("Save");
pnButton.add(btnSave);
pnMain.add(pnButton);
con.add(pnMain);
}
private void addEvents() {
btnSave.addActionListener(e -> {
String id = txtId.getText();
String name = txtName.getText();
String note = txtNote.getText();
Vector<String> vec = new Vector<String>();
vec.add(id);
vec.add(name);
vec.add(note);
dtm.addRow(vec);
txtId.setText("");
txtName.setText("");
txtNote.setText("");
});
}
public void showWindow() {
this.setTitle("Hế lô");
this.setSize(500, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new Table().showWindow();
}
}
Editor is loading...