可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a JTable inside a JScrollPane. After calling my function which should populate the table I can't see any changes! I'v tried with table.repaint()
, model.fireTableDataChanged()
and probably some other stuff that I can't remember anymore - nothing worked. What am I doing wrong??
My function:
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);
}
回答1:
1) there no reason call for model.fireTableDataChanged()
for DefaultTableModel
2) there no reason call for table.repaint()
, is useless
3) what's for(Jezik j : v.getTuji_jeziki()) row[5]+=", "+j.toString();
there could be more than 100% ** of your issues
4) DefaultTableModel
works in all cases, but requierd only update on EDT, in other hands this is common issue for all Swing JComponents together
5) for better help sooner edit yout question with a SSCCE
6) EDT == Event Dispatch Thread
7) SSCCE
DefaultTableModel
model.addRow(row);
Short
Self Contained
Correct
Example
then
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);
}
});
}
}
回答2:
I know its old but, i had the same problem and the solution was to add
jTable1.setAutoCreateColumnsFromModel(true);
回答3:
1.To populate jtable data:
void update_table() throws SQLException {
rs=statement.executeQuery("select * from details");
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}
2. Then call the update function to buttons action event and refresh the jtable
And for more information you can view this tutorial:
How to refresh JTable after insert delete or update the data in java Netbeans ID
回答4:
I know this is old, but I recently had the same problem and the solution was to call
model.fireTableDataChanged();
right after adding the new row to the model.
The catch was this: I had a table on which I allowed sorting. Without clicking the column headers so that the rows would sort accordingly, I was able to add data to the model and see the changes in the table by calling table.revalidate();
However, if, at any time, I clicked the column headers, any row added afterwards wouldn't be shown, although the model data was updating properly. By only calling model.fireTableDataChanged();
after adding the data to the model, it works as a charm.
回答5:
- I have made a new project in that I have selected a new frame and drag a table, a label, a text field and a button from the swing controls to your frame.
2.create a table in back-end…
- Download rs2xml jar file from this link
Download rs2xml.jar
- Add it to the project library…
Right click on the project > properties > library > add Jar/folder > select jar > ok
5.Then connect mysql using following code
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;
}
}
Call the function in the page using following code
con=new DBConnect();
statement=con.getConnection().createStatement();
7.Then create a update function type the following code…
void update_table() throws SQLException{
rs=statement.executeQuery("select * from details");
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}
8.In the buttons action event type the following and call the update function
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.Then run the project…
YOU can also Follow this Tutorial:--
How to refresh JTable after insert delete or update the data in java Netbeans ID
回答6:
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();
}
}