java swing怎样将数据库取出的内容填到JTable里?

java swing怎样将数据库取出的内容填到JTable里?,第1张

设置模型(model)然后使用模型的方法添加行! 例如:dtm

=

new

DefaultTableModel(new

String[]{"姓名","性别","年龄"},0)

JTable

jTable

=

new

JTable(dtm)//使用一下方法添加dtm.addRow(new

String[]{"sa","dasd","dasd"})

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<Titles>getAll()具体实现自己写。

* 把114行的代码改成:List<Titles>lists = new TitleDaoImpl().getAll()

* */

List<Titles>lists = Titles.list()

// 将表行数设为0行,防止异常

tableModel.setRowCount(0)

for (int i = 0i <lists.size()i++) { // 当每有一个Titles对象就添加一行

tableModel.addRow(new Object[] {})

}

Titles instance = null

Object obj = null

Object name = null

for (int i = 0i <tableModel.getRowCount()i++) { // 行循环

instance = lists.get(i) // 为当前Titles对象赋值

for (int j = 0j <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 = 0i <fields.lengthi++) {

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)

}

}

就是用getValueAt(intr,intc)啊!用户一旦修改了表的数据,你在监听器里,得到那个table的对象,就可以之间用这个getValueAt方法的如果还是修改之前的,可能有几个可能:1.那条数据并没有真正的修改到了table中,这种情况也是最常见的错误解决方法:修改数据不能用简单的setValueAt()方法,而是应该对tablemodel里面的数据做修改,然后setModel(Model),这样传回去的table才是真正已经修改过数据的table,否则就不是,这也是大家都用的方法2.自己的监听器写错了,可能在监听器里部分代码有问题其他,应该没有问题了,你看下吧,估计是你的table数据没有真正的插入到table中,不是真正的修改数据,也就是说,你没有用到setModel()方法,而是直接在界面上点击修改数据,没有用到监听器去setModel()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10031832.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存