Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Unable to get JTable to display in applet

Posted on 2005-04-07
14
Medium Priority
?
279 Views
Last Modified: 2010-03-31
I wrote a applet with Jtable, but when I run,  JTable does not apper.  I have attached code.

import javax.swing.JApplet;
import javax.swing.JFrame;

import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.AbstractTableModel;

/*
 * Created on Apr 5, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 *
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class RSWPLAN extends JApplet {

      private javax.swing.JPanel jContentPane = null;

      private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="13,9"
      private JTable jTable = null;
      private JScrollPane scrollPane = null;
      /**
       * This is the default constructor
       */
      public RSWPLAN() {
            super();
            init();
      }
      /**
       * This method initializes this
       *
       * @return void
       */
      public void init() {
            this.setSize(1000,700);
            this.setContentPane(getJContentPane());
      }
      
      /**
       * This method initializes jContentPane
       *
       * @return javax.swing.JPanel
       */
      public javax.swing.JPanel getJContentPane() {
            if(jContentPane == null) {
                  jContentPane = new javax.swing.JPanel();
                  jContentPane.setLayout(new java.awt.BorderLayout());
                  
            }
            
            return jContentPane;
      }
      /**
       * This method initializes jFrame      
       *       
       * @return javax.swing.JFrame      
       */    
      //private JFrame getJFrame() {
      ///      if (jFrame == null) {
      //            jFrame = new JFrame();
      //            jFrame.setContentPane(new JPanel());
      //            jFrame.setSize(494, 364);
      //            jFrame.setTitle("jFrame");
      //      }
      //      return jFrame;
      //}
      /**
       * This method initializes jTable      
       *       
       * @return javax.swing.JTable      
       */
      JTable table = new JTable(new MyTableModel());
   // table.setPreferredScrollableViewportSize(new Dimension(1000, 700));
      public JTable getJTable() {
            if (jTable == null) {
                  JTable table = new JTable(new MyTableModel());
                  //jTable = new JTable();
            }
            
            return jTable;
            
//            Create the scroll pane and add the table to it.
      //      private JScrollPane ScrollPane = null;
     //   JScrollPane scrollPane = new JScrollPane(jTable);
      }
       class MyTableModel extends AbstractTableModel {
        private String[] columnNames = {"First Name",
                                        "Last Name",
                                        "Sport",
                                        "# of Years",
                                        "Vegetarian",
                                                            "Vegetarian"
                                                            };
        private Object[][] data = {
                {"Mary", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Integer(6)},
                {"Alison", "Huml",
                 "Rowing", new Integer(3), new Boolean(true), new Integer(6)},
                {"Kathy", "Walrath",
                 "Knitting", new Integer(2), new Boolean(false), new Integer(6)},
                {"Sharon", "Zakhour",
                 "Speed reading", new Integer(5), new Boolean(true), new Integer(6)},
                {"Philip", "Milne",
                 "Pool", new Integer(5), new Boolean(false), new Integer(6)},
                 {"Philip", "Milne",
                    "Pool", new Integer(2), new Boolean(true), new Integer(6)},
                    {"Mary", "Campione",
                        "Snowboarding", new Integer(5), new Boolean(false), new Integer(6)},
                       {"Alison", "Huml",
                        "Rowing", new Integer(3), new Boolean(true), new Integer(6)},
                       {"Kathy", "Walrath",
                        "Knitting", new Integer(2), new Boolean(false), new Integer(6)},
                       {"Sharon", "Zakhour",
                        "Speed reading", new Integer(5), new Boolean(true), new Integer(6)},
                       {"Philip", "Milne",
                        "Pool", new Integer(5), new Boolean(false), new Integer(6)},
                        {"Philip", "Milne",
                           "Pool", new Integer(2), new Boolean(true), new Integer(6)}
            };

            public int getColumnCount() {
                return columnNames.length;
            }

            public int getRowCount() {
                return data.length;
            }

            public String getColumnName(int col) {
                return columnNames[col];
            }

            public Object getValueAt(int row, int col) {
                return data[row][col];
            }
            {
                  JScrollPane sp = new JScrollPane(jTable);
                  getJContentPane().add(sp);}
          //private static void createAndShowGUI() {
            //Make sure we have nice window decorations.
         //   JFrame.setDefaultLookAndFeelDecorated(true);

            //Create and set up the window.
        //    JFrame frame = new JFrame("TableDemo");
        //    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //Create and set up the content pane.
         //   TableDemo newContentPane = new TableDemo();
         //   newContentPane.setOpaque(true); //content panes must be opaque
         //   frame.setContentPane(newContentPane);

            //Display the window.
          //  frame.pack();
         //   frame.setVisible(true);
       // }
  //  }
      /**
       * This method initializes jScrollPane      
       *       
       * @return javax.swing.JScrollPane      
       */    
      //private JScrollPane getJScrollPane() {
      //      if (jScrollPane == null) {
      //            jScrollPane = new JScrollPane();
      //            jScrollPane.setViewportView(getJTable());
                  //JScrollPane sp = new JScrollPane(jTable);
                  //getContentPane().add(sp);
      //      }
      //      JScrollPane sp = new JScrollPane(jTable);
      //      getJContentPane().add(sp);
      //      return jScrollPane;
      }
       private static void createAndShowGUI() {
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);

        //Create and set up the window.
        JFrame frame = new JFrame("TableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        TableDemo newContentPane = new TableDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
      }
0
Comment
Question by:DanFlyBoy
  • 10
  • 4
14 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 13727718
getJTable() is wrong
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13727731
public JTable getJTable() {

          if (jTable == null) {
               JTable table = new JTable(new MyTableModel());  // <<<<<<< You create a temporary table, but don't initialize jTable
               //jTable = new JTable();
          }
         
          return jTable;
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13727761
Afterwards you do:
>> JScrollPane sp = new JScrollPane(jTable);

Here jTable is still null
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 37

Expert Comment

by:zzynx
ID: 13727778
Try:

public JTable getJTable() {
          if (jTable == null)
               jTable = new JTable(new MyTableModel());
          return jTable;
}

combined with

 JScrollPane sp = new JScrollPane(getJTable());
0
 

Author Comment

by:DanFlyBoy
ID: 13727796
Where do I need to initalize at?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13727871
Try my previous comment.
That initializes your table the first time
- you need it
- you call getJTable()
0
 

Author Comment

by:DanFlyBoy
ID: 13727900
I tried that piece of code now applet does not run at all.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13727935
Can you post the complete code you have at the moment?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13728002
>> Can you post the complete code
without the commented stuff that doesn't need to be there (for clearness)
0
 

Author Comment

by:DanFlyBoy
ID: 13728012
import javax.swing.JApplet;
import javax.swing.JFrame;

import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.AbstractTableModel;

/*
 * Created on Apr 5, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 *
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class RSWPLAN extends JApplet {

      private javax.swing.JPanel jContentPane = null;

      private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="13,9"
      private JTable jTable = null;
      private JScrollPane scrollPane = null;
      /**
       * This is the default constructor
       */
      public RSWPLAN() {
            super();
            init();
      }
      /**
       * This method initializes this
       *
       * @return void
       */
      public void init() {
            this.setSize(1000,700);
            this.setContentPane(getJContentPane());
      }
      
      /**
       * This method initializes jContentPane
       *
       * @return javax.swing.JPanel
       */
      public javax.swing.JPanel getJContentPane() {
            if(jContentPane == null) {
                  jContentPane = new javax.swing.JPanel();
                  jContentPane.setLayout(new java.awt.BorderLayout());
                  
            }
            
            return jContentPane;
      }
      /**
       * This method initializes jFrame      
       *       
       * @return javax.swing.JFrame      
       */    
      //private JFrame getJFrame() {
      ///      if (jFrame == null) {
      //            jFrame = new JFrame();
      //            jFrame.setContentPane(new JPanel());
      //            jFrame.setSize(494, 364);
      //            jFrame.setTitle("jFrame");
      //      }
      //      return jFrame;
      //}
      /**
       * This method initializes jTable      
       *       
       * @return javax.swing.JTable      
       */
      JTable table = new JTable(new MyTableModel());
   // table.setPreferredScrollableViewportSize(new Dimension(1000, 700));
      public JTable getJTable() {
            if (jTable == null) {
                  JTable table = new JTable(new MyTableModel());
                  JScrollPane sp = new JScrollPane(getJTable());
      //            //jTable = new JTable();
                  
            }
            return jTable;      
            
            
            
//            Create the scroll pane and add the table to it.
      //      private JScrollPane ScrollPane = null;
     //   JScrollPane scrollPane = new JScrollPane(jTable);
      }
       class MyTableModel extends AbstractTableModel {
        private String[] columnNames = {"First Name",
                                        "Last Name",
                                        "Sport",
                                        "# of Years",
                                        "Vegetarian",
                                                            "Vegetarian"
                                                            };
        private Object[][] data = {
                {"Mary", "Campione",
                 "Snowboarding", new Integer(5), new Boolean(false), new Integer(6)},
                {"Alison", "Huml",
                 "Rowing", new Integer(3), new Boolean(true), new Integer(6)},
                {"Kathy", "Walrath",
                 "Knitting", new Integer(2), new Boolean(false), new Integer(6)},
                {"Sharon", "Zakhour",
                 "Speed reading", new Integer(5), new Boolean(true), new Integer(6)},
                {"Philip", "Milne",
                 "Pool", new Integer(5), new Boolean(false), new Integer(6)},
                 {"Philip", "Milne",
                    "Pool", new Integer(2), new Boolean(true), new Integer(6)},
                    {"Mary", "Campione",
                        "Snowboarding", new Integer(5), new Boolean(false), new Integer(6)},
                       {"Alison", "Huml",
                        "Rowing", new Integer(3), new Boolean(true), new Integer(6)},
                       {"Kathy", "Walrath",
                        "Knitting", new Integer(2), new Boolean(false), new Integer(6)},
                       {"Sharon", "Zakhour",
                        "Speed reading", new Integer(5), new Boolean(true), new Integer(6)},
                       {"Philip", "Milne",
                        "Pool", new Integer(5), new Boolean(false), new Integer(6)},
                        {"Philip", "Milne",
                           "Pool", new Integer(2), new Boolean(true), new Integer(6)}
            };

            public int getColumnCount() {
                return columnNames.length;
            }

            public int getRowCount() {
                return data.length;
            }

            public String getColumnName(int col) {
                return columnNames[col];
            }

            public Object getValueAt(int row, int col) {
                return data[row][col];
            }
           
                  
          //private static void createAndShowGUI() {
            //Make sure we have nice window decorations.
         //   JFrame.setDefaultLookAndFeelDecorated(true);

            //Create and set up the window.
        //    JFrame frame = new JFrame("TableDemo");
        //    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //Create and set up the content pane.
         //   TableDemo newContentPane = new TableDemo();
         //   newContentPane.setOpaque(true); //content panes must be opaque
         //   frame.setContentPane(newContentPane);

            //Display the window.
          //  frame.pack();
         //   frame.setVisible(true);
       // }
  //  }
      /**
       * This method initializes jScrollPane      
       *       
       * @return javax.swing.JScrollPane      
       */    
      //private JScrollPane getJScrollPane() {
      //      if (jScrollPane == null) {
      //            jScrollPane = new JScrollPane();
      //            jScrollPane.setViewportView(getJTable());
                  //JScrollPane sp = new JScrollPane(jTable);
                  //getContentPane().add(sp);
      //      }
      //      JScrollPane sp = new JScrollPane(jTable);
      //      getJContentPane().add(sp);
      //      return jScrollPane;
      }
      // private static void createAndShowGUI() {
        //Make sure we have nice window decorations.
    //    JFrame.setDefaultLookAndFeelDecorated(true);

        //Create and set up the window.
    //    JFrame frame = new JFrame("TableDemo");
    //    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
    //    TableDemo newContentPane = new TableDemo();
    //    newContentPane.setOpaque(true); //content panes must be opaque
    //    frame.setContentPane(newContentPane);

        //Display the window.
    //   frame.pack();
     //   frame.setVisible(true);
    }
      
0
 
LVL 37

Accepted Solution

by:
zzynx earned 300 total points
ID: 13728092
Remarks:

     public javax.swing.JPanel getJContentPane() {
          if(jContentPane == null) {
               jContentPane = new javax.swing.JPanel();
               jContentPane.setLayout(new java.awt.BorderLayout());
               jContentPane.add( new JScrollPane(getJTable()) );  // <<<<<<<<<< You didn't add the table
          }
          return jContentPane;
     }

     JTable table = new JTable(new MyTableModel());  // <<<<<<<<<<<<<<<<<<<  What's this line???? Remove

     public JTable getJTable() {
          if (jTable == null) {
               JTable table = new JTable(new MyTableModel());   // <<<<<< That's not what I posted!
               //       jTable = new JTable(new MyTableModel());  // This is what I posted

               JScrollPane sp = new JScrollPane(getJTable());   // <<<<<< remove that
          }
          return jTable;    
     }
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13728096
I have tp go offline. Success.
0
 

Author Comment

by:DanFlyBoy
ID: 13728151
Wow you are good!!!!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13733672
Thanks for accepting
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month21 days, 6 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question