我在JScrollPane中有一个JTable。在调用我应该填充表格的函数后,我看不到任何变化!我试过table.repaint()
,model.fireTableDataChanged()
以及其他一些我不记得了的东西 - 没有用。我做错了什么?
我的功能:
public static void fillVodicTable(JTable table){
DefaultTableModel model=new DefaultTableModel(); //also tried (DefaultTableModel)table.getModel();
table.setModel(model);
model.setColumnIdentifiers(new String[]{"Ime", "Priimek", "Epošta", "Datum rojstva", "Licenca", "Jeziki"});
String [] row={"a","b","c","d","e","f"};
model.addRow(row);
}
答案 0 :(得分:3)
1)没有理由要求model.fireTableDataChanged()
DefaultTableModel
2)没有理由要求table.repaint()
,是没用的
3)for(Jezik j : v.getTuji_jeziki()) row[5]+=", "+j.toString();
您的问题可能超过100% * *
4)DefaultTableModel
适用于所有情况,但需要仅在EDT上更新,在另一方面,这是所有Swing JComponents的共同问题
5)为了更好的帮助,请尽快使用SSCCE
编辑您的问题6)EDT == Event Dispatch Thread
7)SSCCE
的DefaultTableModel
model.addRow(行);
短
自包含
更正
实施例
然后
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
public class DefaultTableModelDemo {
public static final String[] COLUMN_NAMES = {
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
};
private DefaultTableModel model = new DefaultTableModel(COLUMN_NAMES, 0);
private JTable table = new JTable(model);
private JPanel mainPanel = new JPanel(new BorderLayout());
private Random random = new Random();
public DefaultTableModelDemo() {
JButton addDataButton = new JButton("Add Data");
JPanel buttonPanel = new JPanel();
buttonPanel.add(addDataButton);
addDataButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addDataActionPerformed();
}
});
model = new DefaultTableModel(COLUMN_NAMES, 0) {
private static final long serialVersionUID = 1L;
@Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
table = new JTable(model) {
private static final long serialVersionUID = 1L;
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if (isRowSelected(row) && isColumnSelected(column)) {
((JComponent) c).setBorder(new LineBorder(Color.red));
}
return c;
}
};
mainPanel.add(new JScrollPane(table), BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
}
private void addDataActionPerformed() {
for (int i = 0; i < 5; i++) {
Object[] row = new Object[COLUMN_NAMES.length];
for (int j = 0; j < row.length; j++) {
row[j] = random.nextInt(5);
}
model.addRow(row);
}
}
public JComponent getComponent() {
return mainPanel;
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("DefaultTableModelDemo");
frame.getContentPane().add(new DefaultTableModelDemo().getComponent());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
答案 1 :(得分:1)
我知道它已经老了,但我遇到了同样的问题,解决办法是添加
jTable1.setAutoCreateColumnsFromModel(true);
答案 2 :(得分:1)
1.填充jtable数据:
void update_table() throws SQLException {
rs=statement.executeQuery("select * from details");
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}
2。然后调用更新功能按钮动作事件并刷新jtable
有关更多信息,您可以查看本教程: How to refresh JTable after insert delete or update the data in java Netbeans ID
答案 3 :(得分:0)
2.在后端创建一个表...
右键单击项目&gt;属性&gt;库&gt;添加Jar /文件夹&gt;选择jar&gt;确定
5.然后使用以下代码连接mysql
public class DBConnect {
Connection connection = null;
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
System.out.println("connection successfully");
} catch (Exception ex) {
ex.printStackTrace();
}
return connection;
}
}
使用以下代码
调用页面中的函数con=new DBConnect();
statement=con.getConnection().createStatement();
7.然后创建更新功能,输入以下代码......
void update_table() throws SQLException{
rs=statement.executeQuery("select * from details");
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}
8.在按钮操作事件中,键入以下内容并调用更新功能
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Object name=jTextField1.getText();
String sql="insert into details (name) values('"+name+"')";
int resultset=statement.executeUpdate(sql);
update_table();
} catch (SQLException ex) {
Logger.getLogger(update.class.getName()).log(Level.SEVERE, null, ex);
}
}
9.然后运行项目......
您也可以按照本教程: -
How to refresh JTable after insert delete or update the data in java Netbeans ID
答案 4 :(得分:-1)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
class JTableDemo extends JFrame implements ActionListener
{
JTable jt;
DefaultTableModel model;
JButton btn;
String[] cols={"Dno","DName","Loc"};
private JPanel mainPanel = new JPanel(new BorderLayout());
public JTableDemo()
{
model=new DefaultTableModel(cols,0);
btn=new JButton("Add Record");
btn.addActionListener(this);
jt=new JTable(model);
btn.setBounds(10, 10, 150, 40);
mainPanel.setBounds(10,200,300,200);
add(btn);
mainPanel.add(new JScrollPane(jt));
add(mainPanel);
setLayout(null);
setSize(500, 600);
setVisible(true);
}
public JComponent getComponent() {
return mainPanel;
}
public void actionPerformed(ActionEvent e) {
Object[] row = new Object[3];
row[0]="10";
row[1]="SW";
row[2]="PKL";
model.addRow(row);
}
public static void main(String[] args)
{
new JTableDemo();
}
}