
hi, i've to query the table thousand of times...m table display is not getting stable/proper,especially after getting the empty table...
for removing the table i'm doing this::
numrows = tableModel.getRowCount();
if(numrows>0)
{
for(int i = numrows - 1;i>=0; i--) { tableModel.removeRow(i); }
pane.remove(table); panel.remove(pane);
for getting the table:: String[] head={"VISITOR NO","VISITOR NAME","VISIT TO PERSON","PURPOSE","IN TIME","DATE"}; colname=new Vector(Arrays.asList(head)); tableModel =new DefaultTableModel(rowdata, colname){public boolean isCellEditable(int row, int column) { return false;}}; table = new JTable(tableModel); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true);
settabWidth(table, head.length);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
header = table.getTableHeader();
pane = new JScrollPane(table);
pane.setAutoscrolls(true);
pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
pane.getVerticalScrollBar().setUnitIncrement(10);
pane.setSize(600,300);
pane.setVisible(true);
MAIN PROBLEM HERE'S M TABLE IS NOT PROPERLY SETTING IN SCROLLPANE...REPLY SOON

change the size of pane so that both the bars will get visible.
import java.sql.*;
import java.util.*;
import javax.swing.*;
class JTableExample extends JFrame {
private static int offset = 50;
public JTableExample(int horizPolicy, int vertPolicy, String title) {
final Vector columnNames = new Vector();
final Vector data = new Vector();
JPanel panel=new JPanel();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st = con.createStatement();
ResultSet rs= st.executeQuery("Select * from employee");
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++){
columnNames.addElement(md.getColumnName(i));
}
while(rs.next()){
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++){
row.addElement(rs.getObject(i));
}
data.addElement(row);
}
}
catch(Exception e){}
JTable table = new JTable(data, columnNames);
setTitle(title);
offset += 50;
setLocation(offset,offset);
setSize(300,150);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scroll = new JScrollPane(table);
scroll.setHorizontalScrollBarPolicy(horizPolicy);
scroll.setVerticalScrollBarPolicy(vertPolicy);
getContentPane().add(scroll);
}
public static void main(String[] args) {
JTableExample f=new JTableExample(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED,JScrollPane.
VERTICAL_SCROLLBAR_AS_NEEDED,"HORIZONTAL_SCROLLBAR_AS_NEEDED");
f.setVisible(true);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.