Code Fix - JTabbed pane

hi, i get the following error when i run this piece of code (below).

java.lang.ClassCastException: insertapplet
java.lang.ClassCastException: editapplet

i have the .class in the same directory. the applet runs, shows the tabs but doesn't display the embedded applets. what's wrong?



import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;

public class Tabbed extends JApplet {
  static Object[][] q = {
    // { "Query", "queryapplet.class" },
      { "Insert", insertapplet.class },
    { "Edit", editapplet.class },
  };
  static JPanel makePanel(Class c) {
    String title = c.getName();
    System.out.println(title);
    title = title.substring(
      title.lastIndexOf('.') + 1);
    JPanel sp = null;
    try {
      sp = (JPanel)c.newInstance();
     
    } catch(Exception e) {
      System.out.println(e);
    }
    //   sp.setBorder(new TitledBorder(title));
    return sp;
  }

  public void init() {
    Container cp = getContentPane();
    cp.setLayout(new BorderLayout());
    JTabbedPane tabbed = new JTabbedPane();
    for(int i = 0; i < q.length; i++)
      tabbed.addTab((String)q[i][0],
        makePanel((Class)q[i][1]));
    cp.add(tabbed, BorderLayout.CENTER);
    tabbed.setSelectedIndex(q.length/2);
  }
}


enchancesAsked:
Who is Participating?
 
TimYatesConnect With a Mentor Commented:
I'm guessing that it's this line:

sp = (JPanel)c.newInstance();

does "insertapplet" extend JPanel?

Maybe:

sp = (JApplet)c.newInstance();

Is more correct?
0
 
TimYatesCommented:
Or:  (not sure if this will work)

static JPanel makePanel(Class c)
{
   String title = c.getName();
   System.out.println(title);
   title = title.substring( title.lastIndexOf('.') + 1 );
   JPanel sp = new JPanel() ;
   BorderLayout b = new BorderLayout() ;
   try
   {
     sp.add( (JApplet)c.newInstance(), BorderLayout.CENTER ) ;
   } catch(Exception e) {
     System.out.println(e);
   }
   return sp;
 }
0
 
objectsCommented:
The q array contains Strings, not Classes.
You need to create Class objects.

try
{
  tabbed.addTab((String)q[i][0],
    makePanel(Class.forName((String)q[i][1])));
}
catch (Exception ex)
{
  ex.printStackTrace();
}

Tim's comments are most probably also valid.


0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
enchancesAuthor Commented:
Hi Tim, I no longer the error messages but it doesn't display the class objects in the panes.
0
 
enchancesAuthor Commented:
hi objects,

with your code i get the following error:


java.lang.ClassCastException: java.lang.Class
        at Tabbed.init(Tabbed.java:37)

this is at the line:
  tabbed.addTab((String)q[i][0],

that was working fine before, thats strange.



0
 
objectsCommented:
Are you sure you are running the same code you posted above?
0
 
enchancesAuthor Commented:
hi objects, yeah i'm running the same code as before. I made a mistake(due to line wrapping in my editor), the error is

makePanel(Class.forName((String)q[i][1])));

0
 
enchancesAuthor Commented:
think i may have found out why...

the second column in the object array (at start of code), all these elements editapplet.class, etc should be strings (!). i  now get the following error:

java.lang.ClassNotFoundException: java.io.FileNotFoundException: /users/ug/sewalsh/LexSystem/www/insertapplet/class.class (No such file or directory)


it thinks the class i tell it are directories and it looking for some class.class. hmph.
0
 
objectsCommented:
Woops I got confused by the first (commented out line) which was a string. Ignore my comment above.
What you had was fine :)

Your problem is that you don't 'start' your applets. You need to call their init() methods, and also call their start() methods once the parent applets start() is called.
Will possibly also need to set their context.
0
 
TimYatesCommented:
Hee hee, between us we'll get there in the end :-)
0
 
TimYatesCommented:
So, with a slight modification to my code:

static JPanel makePanel(Class c)
{
  String title = c.getName();
  System.out.println(title);
  title = title.substring( title.lastIndexOf('.') + 1 );
  JPanel sp = new JPanel() ;
  BorderLayout b = new BorderLayout() ;
  try
  {
    JApplet child = (JApplet)c.newInstance() ;
    sp.add( child, BorderLayout.CENTER ) ;
    child.init() ;
    child.start() ;
  } catch(Exception e) {
    System.out.println(e);
  }
  return sp;
}

not sure how you can set the initial context tho...
0
 
objectsCommented:
> child.start() ;

possibly should wait until start() is called on parent applet before calling start().

> not sure how you can set the initial context tho...


applet.setStub(stub);


0
 
enchancesAuthor Commented:
i'm a little confused by the .setStub method.

could you give me an example of it in my code?
0
 
objectsCommented:
Say for example you have your existing applet class Tabbed implement the AppletStub interface then you would do:

JApplet child = (JApplet)c.newInstance() ;
child.setStub(this);
0
 
enchancesAuthor Commented:
now I get these errors:

Tabbed.java:27: non-static variable this cannot be referenced from a static context
    child.setStub(this);
                  ^
Tabbed.java:27: setStub(java.applet.AppletStub) in java.applet.Applet cannot be applied to (Tabbed)
    child.setStub(this);
         ^
2 errors


almost there. hopefully.
0
 
enchancesAuthor Commented:
objects, did you mean for the .setstub to go like:

child.setStub(this.JApplet); ?
0
 
TimYatesCommented:
change

static JPanel makePanel(Class c)
{

to

JPanel makePanel(Class c)
{
0
 
enchancesAuthor Commented:
it's still complaining about the second of my previous errors. appletstub cannot be applied to the Tabbed class.
0
 
objectsCommented:
Tabbed needs to implement AppletStub.

(btw, this may not be necessary and is not the original reason for your applet not displaying).
0
 
enchancesAuthor Commented:
no objects, that doesn't sort out the prob. thanks anyways. even when i get comment of the .getStub line, it gives the error of not being able to find the actual .class files it *should* be finding.

java! it'll be the death of me.
0
 
objectsCommented:
where is the class file located?
What exactly is the error message?
0
 
enchancesAuthor Commented:
this .class files are in the same directory as the file i'm working with. here is error:

java.lang.ClassNotFoundException: /users/ug/sewalsh/LexSystem/www/insertapplet.class
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:120)
        at Tabbed.init(Tabbed.java:51)
        at sun.applet.AppletPanel.run(AppletPanel.java:344)
        at java.lang.Thread.run(Thread.java:484)


i even explicity give it the full file path in the forName method call.
0
 
objectsCommented:
> i even explicity give it the full file path in the forName method call.

That is incorrect.
The original code you had was fine.
0
 
enchancesAuthor Commented:
with or without full file path, it still gives the same error.
0
 
objectsCommented:
you shouldn't be specify the filename.
***The original code you had was fine***
0
 
enchancesAuthor Commented:
hi again. no more questions this time!! finally got it to work. I've attached as the code as it is quite handy to you want to throw a tabbed pane together from a bunch of class files. I'm giving the points to Tim just because he was the first to give an answer, although Objects, you were just as helpful. Thanks guys.


import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.applet.Applet.*;


public class Tabbed extends JApplet implements java.applet.AppletStub {
 static Object[][] q = {
{ "Tab1", "class1"},   //  <-- NOTE: DO NOT INCLUDE .class extension!
{ "Tab1", "class2" },
 };


  public void appletResize(int j, int i){}

JPanel makePanel(Class c)
{
  String title = c.getName();
  System.out.println(title);
  title = title.substring( title.lastIndexOf('.') + 1 );
  JPanel sp = new JPanel() ;
  BorderLayout b = new BorderLayout() ;
  try
  {
    JApplet child = (JApplet)c.newInstance();
    child.setStub(this);
    sp.add( child, BorderLayout.CENTER ) ;
    child.init();
    child.start();
   
  } catch(Exception e) {
    System.out.println(e);
  }
  return sp;

}


 public void init() {
   // this.init();
   this.start();
   Container cp = getContentPane();
   cp.setLayout(new BorderLayout());
   JTabbedPane tabbed = new JTabbedPane();
   for(int i = 0; i < q.length; i++)

     try{
       String h = (String)q[i][0]; System.out.println(h);
       tabbed.addTab((String)q[i][0],
       makePanel(Class.forName((String)q[i][1])));
     
   }catch(Exception e)
    {e.printStackTrace();
    }
   cp.add(tabbed, BorderLayout.CENTER);
   tabbed.setSelectedIndex(q.length/2);
 }
}
0
All Courses

From novice to tech pro — start learning today.