我正在使用JTable(AbstractTableModel)。实际上我有三个表模型来显示并通过单击按钮填充表模型。 现在我想使用jtextfield添加,更新和删除数据。
这是我的GUI:
import java.sql.SQLException;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.util.*;
public class Main
{
// JFrame
private JFrame fr = new JFrame("JTable Biblioteca");
// JPanels
private JPanel p = new JPanel();
private JPanel pl = new JPanel();
private JPanel pc = new JPanel();
private JPanel pp = new JPanel();
private JPanel pwest = new JPanel();
private JPanel pabajo = new JPanel();
private JPanel plib = new JPanel();
// JTextFields
private JTextField DescripcionjTextField1;
private JTextField CantidadjTextField2;
private JTextField EdicionjTextField3;
private JTextField ObservacionjTextField4;
// TableModels
private TableModel tm = new TableModel();
private TableModelCliente tmc = new TableModelCliente();
private TableModelPrestamo tmp = new TableModelPrestamo();
// JTables
private JTable tableLibros = new JTable(tm);
private JTable tableClientes = new JTable(tmc);
private JTable tablePrestamos = new JTable(tmp);
// JButtons
private JButton btnRefresh = new JButton("Refresh");
private JButton btnAddl = new JButton("Agregar");
private JButton btnUpdatel = new JButton("Actualizar");
private JButton btnDeletel = new JButton("Eliminar");
private JLabel label1 = new JLabel("Descripción");
// hago que el ScrollPane esté siempre visible
private JScrollPane sc = new JScrollPane(tableLibros, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
private JScrollPane scc = new JScrollPane(tableClientes, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
private JScrollPane scp = new JScrollPane(tablePrestamos, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
public Main(){
// indico en cada JTable que voy a hacer seleccion simple de filas (no multiple)
tableLibros.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tableClientes.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tablePrestamos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
p.setLayout(new GridLayout(1,3));
pl.setLayout(new BorderLayout()); // panel libro
pc.setLayout(new BorderLayout()); // panel cliente
pp.setLayout(new BorderLayout()); // panel prestamo
// jtables
pl.add("Center", sc);
pc.add("Center", scc);
pp.add("Center", scp);
// títulos
pl.add("North", new JLabel("Libros"));
pc.add("North", new JLabel("Clientes"));
pp.add("North", new JLabel("Préstamos"));
p.add(pl);
p.add(pc);
p.add(pp);
pwest.add(btnRefresh);
pabajo.add(btnAddl);
pabajo.add(btnUpdatel);
pabajo.add(btnDeletel);
fr.add("Center", p);
fr.add("West", pwest);
fr.add("South", pabajo);
// eventos
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnRefresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
try{
tm.refresh();
tm.fireTableDataChanged();
tmc.refreshCli();
tmc.fireTableDataChanged();
}
catch(ClassNotFoundException f){System.out.println("Exception");}
catch(InstantiationException f){System.out.println("Exception");}
catch(IllegalAccessException f){System.out.println("Exception");}
catch(SQLException f){System.out.println("Exception");
}
}
});
btnAddl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
}
});
fr.setSize(1200, 500);
fr.setVisible(true);
}
public static void main(String[] args){
new Main();
}
}
我想显示JTextField向下按钮