Re: Applet isn't showing up. Perhaps it's a browser fault.
I know that applet is extended from Panel. However, it had me confused when I went to test it on my IDE and it showed a window like thing in the test part.
I'd never managed to get it in a browser until yesterday.
However, considering that I might need nested classes, and that might mean several applets within applets opening. However, I can't have some of them in separate html files and applets. That wouldn't work.
Re: Applet isn't showing up. Perhaps it's a browser fault.
Time for a redesign of the project.
Re: Applet isn't showing up. Perhaps it's a browser fault.
What happens if a JApplet calls a JFrame?
Will it open the JFrame?
Re: Applet isn't showing up. Perhaps it's a browser fault.
Try it and see what happens.
Re: Applet isn't showing up. Perhaps it's a browser fault.
That's because you viewed the applet in an Applet Viewer, not embedded on an html page. Applet Viewers are strictly for debugging and testing purposes, not for actual deployment. Depending on your design, you could use a CardLayout, combine all of your Applets into one, and swap between them by putting them on separate Cards. That's always been my solution for "changing windows" in applets.
Re: Applet isn't showing up. Perhaps it's a browser fault.
Almost would work, though I'd have to add lots of extra buttons to make it go back and forth. Also, I can't figure out how to work Card Layout. I tried it once and it kept throwing some weird exception.
I thought that it wouldn't take much to change something from JFrame to JApplet.
Also, can I have all of the applets on the page, very painful but doable, assuming that I guess I make them all public Applets, even the inner classes and stuff, and just call start() in their constructors as they are needed?
I did have a brainwave that I could simply add other applets to other pages for when it was feasible to have a separate applet, and use the
Desktop.getDesktop().browse() method to make the browser go to the applet page.
The problem is where applets are passing data to each other, can I keep it like I had it when I was using JFrames and just pass it or do I need to add lots of new java and html code to make that continue to work?
Re: Applet isn't showing up. Perhaps it's a browser fault.
CardLayout isn't that bad when you get used to it. Depending on your design, you may be able to create a sub-panel that contains the Cards, while your main panel contains buttons and whatnot to swap between the Cards (meaning you wouldn't have to put the buttons on each Card). Kinda like how html frames work (if you are familiar with how html frames work). The Applets that I've made that uses CardLayout usually has a navigation list on the left-hand side, some sort of general tools along the top, and then the Cards in its own panel in the middle taking up most of the applet window. It allows for a little bit of organized chaos (which we all know is the best kind of chaos).
Re: Applet isn't showing up. Perhaps it's a browser fault.
How do you word CardLayout? As I said, I tried it once and it wouldn't work. I'm going to try it again.
Re: Applet isn't showing up. Perhaps it's a browser fault.
Here is a small program I tossed together real quick. It should run perfectly. I added some comments so you can understand what is happening. As you will see, it is very simple. If you have any questions, feel free to ask.
You can selected the Card using the JList on the left-hand side, or by using the Next and Previous buttons at the top.
Code java:
/**
* @(#)CardLayoutExample.java
*
*
* @author
* @version 1.00 2012/5/14
*/
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class CardLayoutExample extends JFrame {
JList menuOptions;
JButton next;
JButton previous;
Vector<ContentPanel> panels;
//Create Card Holder Panel as global variable so you can use it inside listeners
JPanel cardHolder;
public CardLayoutExample() {
super("Example");
setSize(500,500);
panels = new Vector<ContentPanel>();
//Create x number of ContentPanels
int number = 5;
for(int i=0;i<number;i++) {
panels.add(new ContentPanel(i+1));
}
//Initialize Card Holder and send it a new CardLayout() as the constructor. Set the Preferred Size, and set the Border for clarity
cardHolder = new JPanel(new CardLayout());
cardHolder.setPreferredSize(new Dimension(200,200));
cardHolder.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
//Initialize button that moves to the next Card
next = new JButton("Next");
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Call the next card method
((CardLayout)cardHolder.getLayout()).next(cardHolder);
}
});
//Initialize button that moves to the previous Card
previous = new JButton("Previous");
previous.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Call the next card method
((CardLayout)cardHolder.getLayout()).previous(cardHolder);
}
});
//Create Menu List that allows user to select the Card, set the size of the Menu List, and add a List Selection Listener
menuOptions = new JList(panels);
menuOptions.setPreferredSize(new Dimension(50,200));
menuOptions.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e)
{
//Return if Event is adjusting
if(e.getValueIsAdjusting())
return;
//Get the selected index and change Cards
int selectedIndex = menuOptions.getSelectedIndex();
((CardLayout)cardHolder.getLayout()).show(cardHolder,menuOptions.getSelectedValue().toString());
}
});
//Add all the Cards to the Card Holder
for(ContentPanel panel : panels) {
cardHolder.add(panel,panel.toString());
}
//Panel that holds the Next and Previous Buttons
JPanel top = new JPanel();
top.setPreferredSize(new Dimension(200,50));
top.add(next);
top.add(previous);
//Add Buttons, the Menu, and the Cards to the JFrame
getContentPane().add(top,BorderLayout.PAGE_START);
getContentPane().add(menuOptions,BorderLayout.LINE_START);
getContentPane().add(cardHolder,BorderLayout.CENTER);
setVisible(true);
}
public class ContentPanel extends JPanel {
JLabel label;
int num;
public ContentPanel(int n) {
super();
setPreferredSize(new Dimension(200,200));
num = n;
label = new JLabel("This is Panel "+n);
add(label);
}
public String toString() {
return "Panel "+num;
}
}
public static void main(String[] args) {
new CardLayoutExample();
}
}