8 Replies - 220 Views - Last Post: 06 December 2012 - 03:53 AM Rate Topic: -----

#1 balraj8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-October 12

dont know where i went wrong, unable to grenerate table data in jtable

Posted 05 December 2012 - 09:18 AM


import java.awt.*;
import java.awt.GridLayout.*;
import java.awt.BorderLayout.*;
import javax.swing.*;
import javax.swing.JPanel.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.*;
import java.util.*;


//the Student main screen
public class Screen 
{
private JPanel listItemsPane1, buttonsPane2,displayPane3; 
private JLabel l[];
private JButton jbutton1,jbutton2,jbutton3,jbutton4;
public TextField tf;
private okbuttonhandler okbutton;


//other class
private int i;
Connection con;
PreparedStatement st;
ResultSet rs;

//initialize components
public Screen()
{
JFrame sf1= new JFrame();

// set up panel
listItemsPane1= new JPanel();
buttonsPane2= new JPanel();
displayPane3= new JPanel();
//displayPane3=new table1();
displayPane3= new okbuttonhandler();
listItemsPane1.setBorder(BorderFactory.createTitledBorder("Top"));
buttonsPane2.setBorder(BorderFactory.createTitledBorder("Bottom"));
displayPane3.setBorder(BorderFactory.createTitledBorder("Display"));

l= new JLabel[4];
l[0]= new JLabel("HT.No");

jbutton1= new JButton("OK");
//private okbuttonhandler okbutton;
okbutton=new okbuttonhandler();
jbutton1.addActionListener(okbutton);


jbutton2= new JButton("New");
jbutton3= new JButton("Edit");
jbutton4= new JButton("Delete");

tf= new TextField(12);

//add listeners to components
jbutton2.addActionListener(
new ActionListener() // listener for new event
{
public void actionPerformed(ActionEvent ae)
{
new New(); // calling the new class
}
}
);

jbutton3.addActionListener(
new ActionListener() // listener for edit event
{
public void actionPerformed(ActionEvent ae)
{
new Edit();// calling the edit class
//new New();
}
}
);

jbutton4.addActionListener(
new ActionListener() // listener for delete event
{
public void actionPerformed(ActionEvent ae)
{
//new table();
}
}
);


//set layout and add compnents to panel 1

listItemsPane1.setLayout(new FlowLayout());
listItemsPane1.add(l[0]);
listItemsPane1.add(tf);
listItemsPane1.add(jbutton1);

//set layout and add compnents to panel 2
buttonsPane2.setLayout(new FlowLayout());
buttonsPane2.add(jbutton2);
buttonsPane2.add(jbutton3);
buttonsPane2.add(jbutton4);

sf1.getContentPane();
sf1.setLayout(new GridLayout(3,1));
sf1.add(listItemsPane1);
sf1.add(displayPane3);
sf1.add(buttonsPane2);
sf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//getContentPane.add(sf1);
sf1.setSize(600,300);
sf1.setVisible(true);

}

public static void main(String[] ar)
{
Screen scr= new Screen();
}

class okbuttonhandler extends JPanel implements ActionListener
{
private Vector columnNames = new Vector();
private Vector data = new Vector();
JTable table;
JScrollPane scrollpane;
int i;
public void actionPerformed(ActionEvent e)
{
String ht=tf.getText();
System.out.println(ht);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
Connection con=DriverManager.getConnection("jdbc:odbc:oradsn","system","kumar");
String query="SELECT sname,mob from std_details where HTNO=?";
PreparedStatement st=con.prepareStatement(query);
st.setString(1,ht);
ResultSet rs=st.executeQuery();
ResultSetMetaData rsmd=rs.getMetaData();
int columns=rsmd.getColumnCount();

for(int i=1;i<=columns;i++){
columnNames.addElement(rsmd.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);
}

rs.close();
st.close();
}
//try closed

/*
while(rs.next()){
String sname=rs.getString(1);
String mob=rs.getString(2);
System.out.println(sname);
System.out.println(mob);
}
*/
//}

catch(Exception xe){System.out.println("error while connecting to db");}

JTable table=new JTable(data, columnNames);
TableColumn column;
for(int i=0;i<table.getColumnCount();i++){
column=table.getColumnModel().getColumn(i);
column.setMaxWidth(150);
}
JScrollPane scrollpane=new JScrollPane(table);
displayPane3.add(scrollpane);


}
}
//main class
}








Is This A Good Question/Topic? 0
  • +

Replies To: dont know where i went wrong, unable to grenerate table data in jtable

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: dont know where i went wrong, unable to grenerate table data in jtable

Posted 05 December 2012 - 09:24 AM

I have a cute one here that you may find useful :)

http://www.dreaminco...snippet6437.htm
Was This Post Helpful? 1
  • +
  • -

#3 balraj8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-October 12

Re: dont know where i went wrong, unable to grenerate table data in jtable

Posted 05 December 2012 - 09:28 AM

sir, can u please modify the code and post where i went wrong, i dont want to use that code, as i did it on my own, i want it to be corrected

thax for help! please if u can do this 4r me i'll be thankful to u
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: dont know where i went wrong, unable to grenerate table data in jtable

Posted 05 December 2012 - 09:49 AM

First indent your code properly, it is a nighmare to read

    while(rs.next()){  
        Vector row=new Vector(columns);  
        for(int i=1;i<=columns;i++){  
            row.addElement(rs.getObject(i));  
        }  

        data.addElement(row);  
    }  


I your actionPerformed() you create the Vector and the JTable and you add it to the JPanel
so if your actionPerformed is called 10 times you will add 10 JTables ?

This is not the way it should work
Create the JTable with an emty Vector, add the JTable to the Panel
when the actionPerformed is called, simply fill already existing Vector

or use my code snippet to replace your JTable Model by a newly created one
Was This Post Helpful? 0
  • +
  • -

#5 balraj8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-October 12

Re: dont know where i went wrong, unable to grenerate table data in jtable

Posted 05 December 2012 - 09:51 AM

thank you sir, i'll try it. and thanks for ur assistance.
Was This Post Helpful? 0
  • +
  • -

#6 balraj8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-October 12

Re: dont know where i went wrong, unable to grenerate table data in jtable

Posted 05 December 2012 - 10:38 AM

sir, feeling very happy for it..thanks i got it. ur awesome
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: dont know where i went wrong, unable to grenerate table data in jtable

Posted 05 December 2012 - 10:45 AM

Good :)
How did you make it ?
With an empty Vector that you empty and re-filled every time the actionPerformed is called
or with my TbaleModel generator ?
Was This Post Helpful? 1
  • +
  • -

#8 balraj8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-October 12

Re: dont know where i went wrong, unable to grenerate table data in jtable

Posted 05 December 2012 - 10:47 AM

as u told i first created a empty table, and then with ur TableModel generator.

now working on how to set the size of the table and other beautification work..thax pbl
Was This Post Helpful? 0
  • +
  • -

#9 balraj8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-October 12

Re: dont know where i went wrong, unable to grenerate table data in jtable

Posted 06 December 2012 - 03:53 AM

sir, now how to set different column width to the jtable for different columns, i tried but in vain.and background color

thanks in advance
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1