Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Help with JTable and JButton both in separated class file

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with JTable and JButton both in separated class file

    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
    Last edited by Azriq007; October 7th, 2010 at 10:05 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Help with JTable and JButton both in separated class file

    First off, this is something that is generally frowned upon in OOP:
    class JPanelOne extends JPanel{
     
              JPanel one;
              JTable jTbl;
     
    public JPanelOne()
    {
        one = new JPanel();
        add(getJPanel1());
    }
    What you are doing is extending JPanel, but creating a JPanel inside the class that extends the JPanel. When you extend a JPanel, your class IS a JPanel. Since you are doing databasing, I would hope you have an understanding of inheritance. It doesn't hurt to have a look back at some previous inheritance work to refresh yourself.

    Now, I don't have much experience with Databasing, but I have extensively played around with the hell that is JTables. You would think they could make them easier to work with. I believe the issue you are facing is that the JTable doesn't automatically update itself when you change its data vector. You have to actually reset the vectors using the setDataVectors (or whatever it is) method. I am not entirely sure if this is your issue however. I am having trouble reading your code when you have it in php highlights.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with JTable and JButton both in separated class file

    a) You have 2 instances of JPanelOne. Make a single instance so only this instance gets updated
    b) Adding components dynamically at runtime (eg 'one.add(ScrollPane);') requires one to let the JVM know you've done so. Call revalidate and repaint on the parent.

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with JTable and JButton both in separated class file

    Quote Originally Posted by aussiemcgr View Post
    First off, this is something that is generally frowned upon in OOP:
    class JPanelOne extends JPanel{
     
              JPanel one;
              JTable jTbl;
     
    public JPanelOne()
    {
        one = new JPanel();
        add(getJPanel1());
    }
    What you are doing is extending JPanel, but creating a JPanel inside the class that extends the JPanel. When you extend a JPanel, your class IS a JPanel. Since you are doing databasing, I would hope you have an understanding of inheritance. It doesn't hurt to have a look back at some previous inheritance work to refresh yourself.

    Now, I don't have much experience with Databasing, but I have extensively played around with the hell that is JTables. You would think they could make them easier to work with. I believe the issue you are facing is that the JTable doesn't automatically update itself when you change its data vector. You have to actually reset the vectors using the setDataVectors (or whatever it is) method. I am not entirely sure if this is your issue however. I am having trouble reading your code when you have it in php highlights.
    what i mean is.. i wanted to add data into JTable dynamically.. where when i click on the button on JPanelTwo class , the data from database will appear in JTable which in JPanelOne class..

    Quote Originally Posted by copeg View Post
    a) You have 2 instances of JPanelOne. Make a single instance so only this instance gets updated
    b) Adding components dynamically at runtime (eg 'one.add(ScrollPane);') requires one to let the JVM know you've done so. Call revalidate and repaint on the parent.
    so about the method revalidate and repaint method.. is it something like this??

     
    public JPanelOne()
    {
       // one = new JPanel();
        add(getJPanel1());
        super.revalidate();
    }

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with JTable and JButton both in separated class file

    Quote Originally Posted by Azriq007 View Post
    so about the method revalidate and repaint method.. is it something like this??
    No. See my post above, and place the call of those methods directly after the call to the method I referred to. This is where you add components to a container after it has been validated, and must revalidate.

  6. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with JTable and JButton both in separated class file

    Quote Originally Posted by copeg View Post
    No. See my post above, and place the call of those methods directly after the call to the method I referred to. This is where you add components to a container after it has been validated, and must revalidate.
    have you been saying about his

      jTbl = new JTable(Mv_S.getRowData(), Mv_S.getColumnName());
      JScrollPane scrollPane = new JScrollPane(jTbl);
      one.add(ScrollPane);
      one.revalidate();
      one.repaint();

    is it something like that???

  7. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with JTable and JButton both in separated class file

    maybe this code can make you all understand what i am trying to do here... which right now i am in failed status...

    class jPanelButton
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    public class jPanelButton extends JPanel {
     
        private JButton jbtnOne;
        private jPanelTable jPTblOne ;
        public jPanelButton()
        {
     
            jbtnOne = new JButton("Click It");
            jbtnOne.addActionListener(new ActionListener(){
     
                public void actionPerformed(ActionEvent e) {
                    String[] columnName = {"School Name","Teachers Name"};
                    String[][] rowData = {{"University 1","Mr Larry"},{"Universiti 2","Mr Harrold"}};
                    jPTblOne = new jPanelTable(rowData,columnName);
                    jPTblOne.revalidate();
                }
     
            });
            setLayout(new FlowLayout());
            add(jbtnOne);
        }
    }

    class jPanelTable
    import javax.swing.*;
    import java.awt.*;
    public class jPanelTable extends JPanel {
     
        private JTable tblOne;
        private String rowData[][];
        private String columnData[];
        private JScrollPane sp1;
        public jPanelTable(String rowData[][],String columnData[])
        {
            this.rowData = rowData;
            this.columnData = columnData;
            add(getScrollPane());
            revalidate();
        }
        public JTable getTable()
        {
            tblOne = new JTable(rowData,columnData);
            return tblOne;
        }
        public JScrollPane getScrollPane()
        {
            sp1 = new JScrollPane(getTable());
            sp1.setPreferredSize(new Dimension(750,180));
            return sp1;
        }
     
    }
     
    class testClass

    import javax.swing.*;
    import java.awt.*;
    public class testClass {
     
        public static void main(String[] args) {
            JFrame one = new JFrame("Test");
            String columnName[] = {"Name", "Address", "Location"};
            String[][] data = {
                {"Kathy", "Smith", "Snowboarding"},
                {"John", "Doe", "Rowing"},
                {"Sue", "Black", "Knitting"},
                {"Jane", "White", "Speed reading"},
                {"Joe", "Brown", "Pool"}};
            one.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Container con = one.getContentPane();
            con.setLayout(new GridLayout(2,1,5,5));
            con.add(new jPanelTable(data,columnName));
            con.add(new jPanelButton());
            one.setSize(new Dimension(800,600));
            one.setVisible(true);
        }
    }

    so as i have said before.. i want the new data from the array of string into my JTable which mean replacing the old data in the JTable.. if there any mistakes in my code.. it run successfully but the new data cannot replace the old one in JTable.. help me out please...

  8. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Help with JTable and JButton both in separated class file

    So basically, a button is pressed to add data to the table.

    Why are they on separate JPanels? Why not just a JButton and a JTable? Where the button, when pressed, will add a row to the JTable. I can give you a piece of code once I get to work (where I have all my JTable stuff) later today.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  9. #9
    Junior Member
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with JTable and JButton both in separated class file

    Quote Originally Posted by aussiemcgr View Post
    So basically, a button is pressed to add data to the table.

    Why are they on separate JPanels? Why not just a JButton and a JTable? Where the button, when pressed, will add a row to the JTable. I can give you a piece of code once I get to work (where I have all my JTable stuff) later today.
    First why i make it in separate JPanel class is because my real system is quite biq one.. so if the long code in one class can divided into a few class , that's make easier to manage my code and easier to find error if occur..

    and if you want to give a code i would really glad to learn it...

Similar Threads

  1. Calling method from .class file
    By alexx_88 in forum Java Theory & Questions
    Replies: 6
    Last Post: April 24th, 2012, 02:14 AM
  2. netbeans JTable Jbutton
    By Mr.President in forum AWT / Java Swing
    Replies: 6
    Last Post: July 19th, 2010, 08:37 AM
  3. run java class file from independent of OS
    By starsoheil in forum Java Theory & Questions
    Replies: 6
    Last Post: March 16th, 2010, 08:05 AM
  4. Creating a class file
    By ipatch in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 8th, 2009, 07:19 PM
  5. Truncated class file error
    By Koâk in forum Exceptions
    Replies: 4
    Last Post: June 23rd, 2009, 11:23 AM