?
Solved

Code Fix - JTabbed pane

Posted on 2003-03-27
26
Medium Priority
?
245 Views
Last Modified: 2010-03-31
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);
  }
}


0
Comment
Question by:enchances
[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
  • 12
  • 9
  • 5
26 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 100 total points
ID: 8221340
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
 
LVL 35

Expert Comment

by:TimYates
ID: 8221346
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
 
LVL 92

Expert Comment

by:objects
ID: 8221392
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:enchances
ID: 8221410
Hi Tim, I no longer the error messages but it doesn't display the class objects in the panes.
0
 

Author Comment

by:enchances
ID: 8221477
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
 
LVL 92

Expert Comment

by:objects
ID: 8221503
Are you sure you are running the same code you posted above?
0
 

Author Comment

by:enchances
ID: 8221527
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
 

Author Comment

by:enchances
ID: 8221557
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
 
LVL 92

Expert Comment

by:objects
ID: 8221621
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
 
LVL 35

Expert Comment

by:TimYates
ID: 8221645
Hee hee, between us we'll get there in the end :-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8221655
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
 
LVL 92

Expert Comment

by:objects
ID: 8221705
> 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
 

Author Comment

by:enchances
ID: 8221760
i'm a little confused by the .setStub method.

could you give me an example of it in my code?
0
 
LVL 92

Expert Comment

by:objects
ID: 8221787
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
 

Author Comment

by:enchances
ID: 8221811
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
 

Author Comment

by:enchances
ID: 8221919
objects, did you mean for the .setstub to go like:

child.setStub(this.JApplet); ?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8221935
change

static JPanel makePanel(Class c)
{

to

JPanel makePanel(Class c)
{
0
 

Author Comment

by:enchances
ID: 8221987
it's still complaining about the second of my previous errors. appletstub cannot be applied to the Tabbed class.
0
 
LVL 92

Expert Comment

by:objects
ID: 8222002
Tabbed needs to implement AppletStub.

(btw, this may not be necessary and is not the original reason for your applet not displaying).
0
 

Author Comment

by:enchances
ID: 8222101
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
 
LVL 92

Expert Comment

by:objects
ID: 8222126
where is the class file located?
What exactly is the error message?
0
 

Author Comment

by:enchances
ID: 8222140
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
 
LVL 92

Expert Comment

by:objects
ID: 8222171
> 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
 

Author Comment

by:enchances
ID: 8222198
with or without full file path, it still gives the same error.
0
 
LVL 92

Expert Comment

by:objects
ID: 8222249
you shouldn't be specify the filename.
***The original code you had was fine***
0
 

Author Comment

by:enchances
ID: 8222603
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month12 days, 1 hour left to enroll

752 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