I am new to swing and I have just started working on it as apart of my class work. Do forgive me if the question is irrelevant.
I am trying to put a table with some data from data base on a frame. What i have done is that I have created a JFrame Form. This form is blank. Now i am trying to populate a table, put it on a scroll Pane and finally add this scroll pane to my frame.
I am able to display the table by creating a new frame from the current frame. But what i want is add the table to the current Jframe form.
Here is what I have done:
package com.air.form;
import com.air.db.Transaction;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class FlightDetails extends javax.swing.JFrame {
/** Creates new form FlightDetails */
public FlightDetails() {
initComponents();
}
FlightDetails(String src,String dest) {
initComponents();
initialise(src,dest);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 603, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FlightDetails().setVisible(true);
}
});
}
private void initialise(String src, String dest)
{
Transaction tn=new Transaction();
try
{
ResultSet result = tn.ReturnFlightDetails(src,dest);
ResultSetMetaData md = result.getMetaData();
int columnCount = md.getColumnCount();
Vector columns = new Vector(columnCount);
//store column names
for(int i=1; i<=columnCount; i++)
columns.add(md.getColumnName(i));
Vector data = new Vector();
Vector row;
//store row data
while(result.next())
{
row = new Vector(columnCount);
for(int i=1; i<=columnCount; i++)
{
row.add(result.getString(i));
}
data.add(row);
}
JTable table = new JTable(data, columns);
JScrollPane scrollPane = new JScrollPane(table);
}
catch(Exception e)
{
}
}
}
JFrameis aContainerwhich has theaddmethod to addComponents to it. Depending on the layout manager, you need to pass some constraints as well. I do not see any of such calls in your code. You just create a table, but does not add it to any container.As this is homework, I just going to include some extra links: