Link to home
Start Free TrialLog in
Avatar of enchances
enchances

asked on

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);
  }
}


ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;
 }
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.


Avatar of enchances
enchances

ASKER

Hi Tim, I no longer the error messages but it doesn't display the class objects in the panes.
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.



Are you sure you are running the same code you posted above?
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])));

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.
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.
Hee hee, between us we'll get there in the end :-)
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...
> 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);


i'm a little confused by the .setStub method.

could you give me an example of it in my code?
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);
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.
objects, did you mean for the .setstub to go like:

child.setStub(this.JApplet); ?
change

static JPanel makePanel(Class c)
{

to

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

(btw, this may not be necessary and is not the original reason for your applet not displaying).
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.
where is the class file located?
What exactly is the error message?
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.
> i even explicity give it the full file path in the forName method call.

That is incorrect.
The original code you had was fine.
with or without full file path, it still gives the same error.
you shouldn't be specify the filename.
***The original code you had was fine***
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);
 }
}