我用JAVA的swing创建了一个窗口程序,拖了一个jTable控件在窗体上,怎样把数据库中的内容显示在Table上?

2024年12月04日 17:59
有5个网友回答
网友(1):

public class T extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel jPanel = null;
private JTextField jTextField = null;
private JTextField jTextField1 = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
private JTable jTable = null;
private DefaultTableModel tableModel = null;

private JPanel getJPanel() {
if (jPanel == null) {
JLabel jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(30, 50, 80, 25));
jLabel1.setText("请输入ISBN:");
JLabel jLabel = new JLabel();
jLabel.setBounds(new Rectangle(30, 20, 80, 25));
jLabel.setText("请输入书名:");
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.setPreferredSize(new Dimension(1, 120));
jPanel.add(jLabel);
jPanel.add(jLabel1);
jPanel.add(getJTextField());
jPanel.add(getJTextField1());
jPanel.add(getJButton());
jPanel.add(getJButton1());
jPanel.add(getJButton2());
}
return jPanel;
}

private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(120, 20, 140, 25));
}
return jTextField;
}

private JTextField getJTextField1() {
if (jTextField1 == null) {
jTextField1 = new JTextField();
jTextField1.setBounds(new Rectangle(120, 50, 140, 25));
}
return jTextField1;
}

private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(270, 20, 60, 25));
jButton.setText("显示");
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/* 修改处 */
}
});
}
return jButton;
}

private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(270, 50, 60, 25));
jButton1.setText("显示");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/* 修改处 */
}
});
}
return jButton1;
}

private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setBounds(new Rectangle(240, 80, 90, 30));
jButton2.setText("显示全部");
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/*
* 修改处此处只要改下面一行,换成从数据库读就行
* 应该在DAO层加一个查询全部的方法
* public List getAll();具体实现自己写。
* 把114行的代码改成:List lists = new TitleDaoImpl().getAll();
* */
List lists = Titles.list();
// 将表行数设为0行,防止异常
tableModel.setRowCount(0);
for (int i = 0; i < lists.size(); i++) { // 当每有一个Titles对象就添加一行
tableModel.addRow(new Object[] {});
}
Titles instance = null;
Object obj = null;
Object name = null;
for (int i = 0; i < tableModel.getRowCount(); i++) { // 行循环
instance = lists.get(i); // 为当前Titles对象赋值
for (int j = 0; j < tableModel.getColumnCount(); j++) {
name = tableModel.getColumnName(j);
if (name.equals("ISBN")) {
obj = instance.getISBN();
} else if (name.equals("title")) {
obj = instance.getTitle();
} else if (name.equals("publisher")) {
obj = instance.getPublisher();
} else if (name.equals("date")) {
obj = instance.getDate();
} else if (name.equals("price")) {
obj = instance.getPrice();
} else {
obj = "";
}
tableModel.setValueAt(obj, i, j);
}
}
}
});
}
return jButton2;
}

private JPanel getJPanel1() {
JScrollPane jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getJTable());
JPanel panel = new JPanel(new BorderLayout());
panel.add(jScrollPane, BorderLayout.CENTER);
return panel;
}

private JTable getJTable() {
if (jTable == null) {
// 获得Titles类里的字段,一个字段在JTable中应该表现为一列
Field[] fields = Titles.class.getDeclaredFields();
// 将字段名保存为列名数组为DefaultTableModel所用
Object[] columnNames = new Object[fields.length];
for (int i = 0; i < fields.length; i++) {
columnNames[i] = fields[i].getName();
}
tableModel = new DefaultTableModel(columnNames, 0);
jTable = new JTable(tableModel);
}
return jTable;
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
T thisClass = new T();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}

public T() {
super();
setSize(580, 475);
getContentPane().add(getJPanel(), BorderLayout.SOUTH);
getContentPane().add(getJPanel1(), BorderLayout.CENTER);
}

}

网友(2):

Arraylist 数据 要转换成Vector保存数据,Vector这个容器线程安全.
然后构造一个DefaultTableModel 设置 数据 和表格 头部, 可以用构造,也可以用set
//定义
JTable table;
DefaultTableModel model;
Vector> users = new Vector>();

//操作
users = new UserDaoImp().getAllUser();
model = new DefaultTableModel(users, makeColumn()) {
public boolean isCellEditable(int row, int column) {
return false;
}
};
table = new JTable(model);
JScrollPane userList = new JScrollPane(table);//table放到一个滚动的JScrollPane,表格可以下拉
JPanel userPanel = new JPanel(new BorderLayout()); //用户列表表格面板
userPanel.add(userList);//添加滑动面板到 用户面板中
// 返回 表格的头部
public Vector makeColumn() {
Vector head = new Vector();
head.add("账号");
head.add("姓名");
head.add("年龄");
head.add("性别");
head.add("在线状态");
head.add("账号状态");
head.add("地址");
head.add("注册时间");
return head;
}

网友(3):

嗯,明天给你弄个例子,你看看

你看一下这个。
-------------------------------------------

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class AppDemo extends JFrame implements ActionListener {
private JTable table;
private DefaultTableModel model = null;
private String[] columns = { "no", "name", "age", "email" };
private DefaultTableColumnModel columnModel = new DefaultTableColumnModel();
private TableColumn column = null;

public AppDemo() {
getContentPane().setLayout(null);

JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(12, 46, 418, 217);
getContentPane().add(scrollPane);
model = new DefaultTableModel(columns, 0);
table = new JTable(model);
scrollPane.setViewportView(table);

JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(22, 13, 93, 23);
btnNewButton.addActionListener(this);
getContentPane().add(btnNewButton);

// table.getColumn(0).setWidth(80);
// table.getColumn(1).setWidth(120);
// table.getColumn(2).setWidth(60);
// table.getColumn(3).setWidth(100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(450, 300);
setVisible(true);
}

public static void main(String[] args) {
new AppDemo();
}

public void actionPerformed(ActionEvent actionevent) {
List list = new ArrayList();
for (int i = 1; i <= 20; i++) {
User user = new User();
user.no = i;
user.name = "name" + i;
user.age = i * 2;
user.email = "name@163.com";
list.add(user);
}
for (int i = 0; i < list.size(); i++) {
User user = list.get(i);
model.addRow(user.toArray());
}
}
}

class User {
public int no = 0;
public String name = null;
public int age = 0;
public String email = null;

public Object[] toArray() {
return new Object[] { no, name, age, email };
}

}

网友(4):

连接数据库。 然后把数据循环输出到界面上

网友(5):

楼上讲的很详细了, 另外,用数组和List当数据容器也可以.