try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/BOA","root","");
st=con.createStatement();
String sql=("select gbal from GeneralLedger where group='Capital'");
rs=st.executeQuery(sql);
while(rs.next())
{
Vector row=new Vector();
row.add("Capital");
row.add("");
row.add("1");
row.add("Fixed Assets");
row.add("");
row.add("7");
data.add(row);
}
}
catch(Exception e)
{
System.out.println(e);
}
Successfully I have fetch record from database and stored in my JTable
but don't know how to add empty row after each record in JTable
?
Can any one please suggest me?
How about defaultTableModel.addRow(new Object[] {});
, assuming you are using a DefaultTableModel
. You should be using some kind of model. If you have a custom model, then show it to us.
Here's a simple example with DefaultTableModel
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class TestTable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
String[][] data = { { "Data", "Data" } };
String[] col = { "Col", "Col" };
final DefaultTableModel model = new DefaultTableModel(data, col);
JTable table = new JTable(model);
JButton addRow = new JButton("Add Empty Row");
addRow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.addRow(new Object[] {});
}
});
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JScrollPane(table));
panel.add(addRow, BorderLayout.SOUTH);
JOptionPane.showMessageDialog(null, panel);
}
});
}
}
If by chance you have no experience working with models, see How to Use Tables: Creating a TableModel
EDIT to your new comments and edit
All you need to do is add an emtpy Vector
every time you are adding a new row.