?
Solved

Problem adding JTable to ScrollPane

Posted on 2003-02-21
3
Medium Priority
?
707 Views
Last Modified: 2007-12-19
Hi:

   I 've created a Jtable but I am having problem putting it inside a JScrollPane.  I have no problem compiling it but I got null pointer exception at run time.

Here 's a snippet of my code:

public class DialogFrame extends JFrame
{     //===================
   public DialogFrame()
      {
          createTable();      
       }
   public void createTable()
      {
          columnNames = new Vector();
              columnNames.add("Name");
              columnNames.add("Type");
              columnNames.add("Interface");
              columnNames.add("Description");
              columnNames.add("Status");
              columnNames.add("Queue");
              columnNames.add("BannerPage");
              Vector tableRow = new Vector();
              //tableRow = executeScript("perl garb.pl");
            //  tableRow = executeScript("perl c:\\textx.pl");
              model = new DefaultTableModel( tableRow, columnNames ){
                      public boolean isCellEditable(int row,int col) {
                      return false;
                      }
                      public Dimension getPreferredScrollableViewportSize() {
                          return getPreferredSize();
                      }
                };
              table = new JTable(model);
              JTableHeader header = table.getTableHeader();
              TableColumnModel colmod = table.getColumnModel();
              for (int i=0; i < table.getColumnCount(); i++)
              {
                 DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
                 renderer.setHorizontalAlignment(SwingConstants.CENTER);
                 colmod.getColumn(i).setCellRenderer(renderer);
              }
              table.setPreferredScrollableViewportSize(new Dimension(500, 70));
              table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
              table.setShowGrid( false );
              table.getTableHeader().setReorderingAllowed( false );
              table.setIntercellSpacing(new Dimension(0,0) );
              table.addMouseListener( new MouseAdapter () {
                    public void mouseClicked( MouseEvent e) {
                      if ((e.getModifiers() & InputEvent.BUTTON1_MASK) !=0)
                         {
                           printSelectCell(table);
                         }
                        }
                       });
              this.setSize(900,900);

tabScrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
               tabScrollPane.getViewport().add(table);
               JPanel scrollPanel = new JPanel();
               scrollPanel.add(tabScrollPane);
               getContentPane().setLayout(
                  new BoxLayout( getContentPane(), BoxLayout.Y_AXIS ) );
               getContentPane().add(firstTopPanel);
               getContentPane().add(panelForLabel);
               getContentPane().add(header);
               getContentPane().add( scrollPanel );//contain table
               getContentPane().add(SecBotPanel);

}

 private Vector executeScript(String str)
          {  
              Vector tableOfVectors = new Vector();
             try{          
                    String line;  
                    Process ls_proc = Runtime.getRuntime().exec(str);
   
                    //get its output (your input) stream
                    BufferedReader in = new BufferedReader( new InputStreamReader( ls_proc.getInputStream() ));
                    //readLine reads in one line of text
                    int k = 1, i = 0;
                    //LOOK FOR THE "|"
                 
                   Pattern p = Pattern.compile("  ");
           
                    while (( line = in.readLine()) != null)
                    {
                          Vector data = new Vector();
                          Matcher m = p.matcher(line);
                         
                           if(m.find() == true)//find "  "
                           {
                             line = line.replaceAll(" {2,}?", "|");
                           }    
                           //creates a new vector for each new line read in
                             StringTokenizer t = new StringTokenizer(line,"|");
                                 while ( t.hasMoreTokens() )
                                 {
                                      try
                                      {
                                        nextElement = t.nextToken().trim();
                                          //add a string to a vector
                                           data.add(nextElement.trim());                                        
                                      }
                                      catch(java.util.NoSuchElementException nsx)
                                      {
                                        System.out.println(nsx);
                                      }
                                }                
                                    tableOfVectors.add(data);
                                   //COUNT THE NUMBER OF PRINTER
                                    printerCount = k;
                                    printerCountLabel.setText("Printer: " + printerCount);
                                    k++;      
                                //}  
                         
                   }//END OF WHILE
                  in.close();
               }//END OF TRY
              catch (IOException e1) {
                   System.err.println(e1);
                   System.exit(1);
             }
             return tableOfVectors;
         }
           
 //============================//
        // MAIN
       //===========================//
         public static void main(String[] args)
         {
              DialogFrame frame = new DialogFrame();
             // frame.DefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.show();
              frame.pack();
        }
   }

Here 's the error I am getting:
ava.lang.NullPointerException
        at java.awt.Container.addNotify(Container.java:2045)
        at javax.swing.JComponent.addNotify(JComponent.java:4283)
        at java.awt.Container.addNotify(Container.java:2045)
        at javax.swing.JComponent.addNotify(JComponent.java:4283)
        at java.awt.Container.addNotify(Container.java:2045)
        at javax.swing.JComponent.addNotify(JComponent.java:4283)
        at javax.swing.JRootPane.addNotify(JRootPane.java:673)
        at java.awt.Container.addNotify(Container.java:2045)
        at java.awt.Window.addNotify(Window.java:410)
        at java.awt.Frame.addNotify(Frame.java:479)
        at java.awt.Window.show(Window.java:448)
        at DialogFrame.main(DialogFrame.java:730)

--nutless
   
0
Comment
Question by:nutless
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Expert Comment

by:allahabad
ID: 7996837
To me its looks like you will have to modify :
----code part----
  this.setSize(900,900);

tabScrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
              tabScrollPane.getViewport().add(table);
              JPanel scrollPanel = new JPanel();
              scrollPanel.add(tabScrollPane);
              getContentPane().setLayout(
                 new BoxLayout( getContentPane(), BoxLayout.Y_AXIS ) );
              getContentPane().add(firstTopPanel);
              getContentPane().add(panelForLabel);
              getContentPane().add(header);
              getContentPane().add( scrollPanel );//contain table
              getContentPane().add(SecBotPanel);
-----code part------

----with may be solution part----
   JFrame jf = new JFrame();
             jf.setSize(900,900);

tabScrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
              tabScrollPane.getViewport().add(table);
              JPanel scrollPanel = new JPanel();
              scrollPanel.add(tabScrollPane);
              jf.getContentPane().setLayout(
                 new BoxLayout( getContentPane(), BoxLayout.Y_AXIS ) );
              jf.getContentPane().add(firstTopPanel);
              jf.getContentPane().add(panelForLabel);
              jf.getContentPane().add(header);
              jf.getContentPane().add( scrollPanel );//contain table
              jf.getContentPane().add(SecBotPanel);
              jf.setVisible(true);
              jf.pack();
----with may be solution part----

and in main method, just create the object;
  public static void main(String[] args)
        {
             DialogFrame frame = new DialogFrame();
         }

0
 
LVL 5

Expert Comment

by:vemul
ID: 7997478
instead of
>>
tabScrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
              tabScrollPane.getViewport().add(table);

>>

try this..

tabScrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
0
 
LVL 1

Accepted Solution

by:
membersh earned 1500 total points
ID: 8007915
This is surprising , but i found that moving line         getContentPane().add(SecBotPanel);
before add(scrollpane) to contentPane doesn't result in the exception. ie., add SecBotPanel before adding scrollPanel or tabScrollPane. [tabScrollPane can be added  to contentPane directly.. worked for me]. Is it okay to change the order ?
0

Featured Post

Independent Software Vendors: 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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 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