enchances
asked on
Code Fix - JTabbed pane
hi, i get the following error when i run this piece of code (below).
java.lang.ClassCastExcepti on: insertapplet
java.lang.ClassCastExcepti on: 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);
}
}
java.lang.ClassCastExcepti
java.lang.ClassCastExcepti
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]
makePanel((Class)q[i][1]))
cp.add(tabbed, BorderLayout.CENTER);
tabbed.setSelectedIndex(q.
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The q array contains Strings, not Classes.
You need to create Class objects.
try
{
tabbed.addTab((String)q[i] [0],
makePanel(Class.forName((S tring)q[i] [1])));
}
catch (Exception ex)
{
ex.printStackTrace();
}
Tim's comments are most probably also valid.
You need to create Class objects.
try
{
tabbed.addTab((String)q[i]
makePanel(Class.forName((S
}
catch (Exception ex)
{
ex.printStackTrace();
}
Tim's comments are most probably also valid.
ASKER
Hi Tim, I no longer the error messages but it doesn't display the class objects in the panes.
ASKER
hi objects,
with your code i get the following error:
java.lang.ClassCastExcepti on: 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.
with your code i get the following error:
java.lang.ClassCastExcepti
at Tabbed.init(Tabbed.java:37
this is at the line:
tabbed.addTab((String)q[i]
that was working fine before, thats strange.
Are you sure you are running the same code you posted above?
ASKER
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((S tring)q[i] [1])));
makePanel(Class.forName((S
ASKER
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.ClassNotFoundExc eption: java.io.FileNotFoundExcept ion: /users/ug/sewalsh/LexSyste m/www/inse rtapplet/c lass.class (No such file or directory)
it thinks the class i tell it are directories and it looking for some class.class. hmph.
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.ClassNotFoundExc
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.
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...
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);
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);
ASKER
i'm a little confused by the .setStub method.
could you give me an example of it in my code?
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);
JApplet child = (JApplet)c.newInstance() ;
child.setStub(this);
ASKER
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.Applet Stub) in java.applet.Applet cannot be applied to (Tabbed)
child.setStub(this);
^
2 errors
almost there. hopefully.
Tabbed.java:27: non-static variable this cannot be referenced from a static context
child.setStub(this);
^
Tabbed.java:27: setStub(java.applet.Applet
child.setStub(this);
^
2 errors
almost there. hopefully.
ASKER
objects, did you mean for the .setstub to go like:
child.setStub(this.JApplet ); ?
child.setStub(this.JApplet
change
static JPanel makePanel(Class c)
{
to
JPanel makePanel(Class c)
{
static JPanel makePanel(Class c)
{
to
JPanel makePanel(Class c)
{
ASKER
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).
(btw, this may not be necessary and is not the original reason for your applet not displaying).
ASKER
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.
java! it'll be the death of me.
where is the class file located?
What exactly is the error message?
What exactly is the error message?
ASKER
this .class files are in the same directory as the file i'm working with. here is error:
java.lang.ClassNotFoundExc eption: /users/ug/sewalsh/LexSyste m/www/inse rtapplet.c lass
at java.lang.Class.forName0(N ative Method)
at java.lang.Class.forName(Cl ass.java:1 20)
at Tabbed.init(Tabbed.java:51 )
at sun.applet.AppletPanel.run (AppletPan el.java:34 4)
at java.lang.Thread.run(Threa d.java:484 )
i even explicity give it the full file path in the forName method call.
java.lang.ClassNotFoundExc
at java.lang.Class.forName0(N
at java.lang.Class.forName(Cl
at Tabbed.init(Tabbed.java:51
at sun.applet.AppletPanel.run
at java.lang.Thread.run(Threa
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.
That is incorrect.
The original code you had was fine.
ASKER
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***
***The original code you had was fine***
ASKER
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((S tring)q[i] [1])));
}catch(Exception e)
{e.printStackTrace();
}
cp.add(tabbed, BorderLayout.CENTER);
tabbed.setSelectedIndex(q. length/2);
}
}
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]
makePanel(Class.forName((S
}catch(Exception e)
{e.printStackTrace();
}
cp.add(tabbed, BorderLayout.CENTER);
tabbed.setSelectedIndex(q.
}
}
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;
}