okay.. to all coders .. i need some help here...
i am trying to develop a java application
just combining 3 difference classes , separated each..
1st class - JPanel subclasses name JPanelOne
2nd class - Another JPanel subclasses name JPanelTwo
3rd class - JFrame subclasses name MainFrame
and the TestClass itself ( as i mentioned before each class has its own separated file)
okay my problem is
I tried to get data from database and put it into my JTable component which has in JPanelOne class. That event only executed when i click a button in
JPanelTwo class. JPanelOne and JPanelTwo will be add together in MainFrame class and a declaration of it's object will be made in TestClasses
my problem is only happened when i tried to click a button to retrieved data from database but i cannot put it into my JTable .. i tried a few method like
1. give a value to mutator method and call the accessor method which is setJPanel1() : void and getJPanel1() : JPanel which i wrote the code in JPanelTwo classes in JButton actionListener and the result.. it didn't even show the data... sad.gif
2. in the method actionPerformed which override from ActionListener class that will put as JButton actionListener, i declared a new object for JPanel1() class and try to add the method getJPanel1() : JPanel ... but this also cannot give the result i want
okay don't want to take this story to long.. i want to show you my code.. that didn't show the result
JPanelOne class
import javax.swing.*; import java.sql.*; import java.util.*; class JPanelOne extends JPanel{ JPanel one; JTable jTbl; public JPanelOne() { one = new JPanel(); add(getJPanel1()); } public JPanel getJPanel1(){ return one; } public void setJPanel1(){ Vector rowData; Vector columnData = new Vector(); try{ Class.forName("database driver here"); // assumed that i have put the driver here Connection con = DriverManager.createStatement("database name","user id","password") // assumed that i have put the URL, user ID and password Statement stmt = con.createStatement(); Resultset rs = stmt.executeQuery("select * from some_table"); ResultSetMetaData rsM = rs.getMetaData(); int column = rsM.getColumnCount(); for (int i = 1; i <= columns; i++) { columnData.addElement(rsM.getColumnName(i)); } while (rs.next()) { rowData = new Vector(columns); for (int i = 1; i <= columns; i++) { rowData.addElement(rs.getObject(i)); } } rs.close(); stmt.close(); jTbl = new JTable(Mv_S.getRowData(), Mv_S.getColumnName()); JScrollPane scrollPane = new JScrollPane(jTbl); one.add(ScrollPane); }catch(Exception e) { } } }
JPanelTwo class
import javax.swing.*; class JPanelTwo extends JPanel{ JPanel one; JButton click; JPanelOne jPnlOne = new JPanelOne(); public JPanelTwo() { click = new JButton("Click") click.addActionListener( new ActionListener() { public void ActionPerformed(ActionEvent E){ jPnlOne.setJPanel1(); jPnlOne.getJPanel1(); } }); add(getOne); } public JPanel getOne(){ one = new JPanel(); one.add(click); return one; } }
MainFrame class
import javax.swing.*; class MainFrame extends JFrame{ JPanelOne one = new JPanelOne(); JPanelTwo two = new JPanelTwo(); public MainFrame() { Container con = this.getContentPane(); con.setLayout(new GridLayout(2,1,5,5); con.add(one); con.add(two); this.setSize(new Dimension(800,600); this.setDefaultClosedOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } }
TestClass
import javax.swing.*; public class TestClass{ public static void main(String args[]) { MainFrame one = new MainFrame(); } }
so that's all my code.. so can anybody tell me why i cannot get record into my JTable when i click a button... and how's the better way to do it.. please