Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Are menus and forms incompatible in JApplets?

Status
Not open for further replies.

fldbryan

Programmer
Jan 22, 2004
46
US
I've been trying to figure out this code for several days on using menus within an applet Jpanel. In my searching I've found no examples of combined use. In the following code if you switch the order in what gets added first thats all you see. If you add form information first the menubar doesn't show and the same for the reverse order.

Are these incompatible or what am I missing here. I've chopped down the code size to make things fit easier here.

TIA

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

public class myIcon extends JApplet
{
private void createGUI() {
JMenuBar menubar;
JMenu mnuPopup_421;
JMenuItem mnuPopupMonth_423;
JMenuItem mnuPopupMonth_427;
JMenuItem mnuPopupMonth_431;

JPanel con;

ButtonGroup cbg;
JList picCalendar_15;
JList picDays_30;
JButton cmdChangeDate_46;
JButton cmdChangeDate_58;
JButton cmdChangeDate_70;
JButton cmdChangeDate_84;
JButton cmdToday_96;
JCheckBox chkStayOnTop_105;
JButton cmdToday_113;
JButton cmdChangeDate_122;
JButton cmdChangeDate_134;
JButton cmdChangeDate_148;
JButton cmdChangeDate_160;
JButton cmdClose_172;
JList picDays_181;
JList picCalendar_197;

con = new JPanel(new BorderLayout());
con.setOpaque(true);
con.setFont(new Font("Helvetica", Font.PLAIN, 12));
con.setLayout(null);
con.setSize(526, 322);
cbg = new ButtonGroup();


//create the menubar
menubar = new JMenuBar();
// Menu Parent mnuPopup
//setting up Menu - mnuPopup_421
mnuPopup_421 = new JMenu("Month");
//setting up MenuItem - mnuPopupMonth_423
mnuPopupMonth_423 = new JMenuItem("January");
mnuPopup_421.add(mnuPopupMonth_423);
//setting up MenuItem - mnuPopupMonth_427
mnuPopupMonth_427 = new JMenuItem("February");
mnuPopup_421.add(mnuPopupMonth_427);
//setting up MenuItem - mnuPopupMonth_431
mnuPopupMonth_431 = new JMenuItem("March");
mnuPopup_421.add(mnuPopupMonth_431);

// Setting up Applet - appname

//setting up PictureBox - picDays_30
picDays_30 = new JList();
JScrollPane sppicDays_30 = new JScrollPane(picDays_30);
sppicDays_30.setLocation(280, 80);
sppicDays_30.setSize(224, 16);
con.add(sppicDays_30);



//setting up JButton - cmdToday_96
cmdToday_96 = new JButton("Today");
cmdToday_96.setBounds(280, 250, 66, 21);
con.add(cmdToday_96);

//setting up JCheckBox - chkStayOnTop_105
chkStayOnTop_105 = new JCheckBox("Stay on top of other windows.");
chkStayOnTop_105.setBounds(20, 290, 246, 18);
con.add(chkStayOnTop_105);

//setting up JButton - cmdClose_172
cmdClose_172 = new JButton("&Close");
cmdClose_172.setBounds(415, 290, 97, 25);
con.add(cmdClose_172);

//setting up PictureBox - picDays_181
picDays_181 = new JList();
JScrollPane sppicDays_181 = new JScrollPane(picDays_181);
sppicDays_181.setLocation(25, 80);
sppicDays_181.setSize(224, 16);
con.add(sppicDays_181);

//setting up PictureBox - picCalendar_197
picCalendar_197 = new JList();
JScrollPane sppicCalendar_197 = new JScrollPane(picCalendar_197);
sppicCalendar_197.setLocation(25, 105);
sppicCalendar_197.setSize(225, 138);
con.add(sppicCalendar_197);

menubar.add(mnuPopup_421);

getContentPane().add(con);
getContentPane().add(menubar);
}

public void init()
{

try {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't successfully complete");
}
}

public static void main(String args[])
{
myIcon applet = new myIcon();
Frame window = new Frame("myIcon");
window.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
applet.init();
window.add("Center", applet);
window.setTitle("ICD Calendar");
window.pack();
window.setVisible(true);
}
}
 
What about JApplet.setMenubar()?

Anyway, I remember something about menus in applets that said that they only work if the applet doesn't show embedded in the HTML.

Cheers,

Dian
 
I've tried various different ways, but the documentation does states it has to be set to the root pane. I'm hoping someone from Sun may know the answer to this. It's been very frustrating because I feel the answer is probably something very simple.
 
Thanks for trying to help. the silverstream example only gives a few lines and not a complete applet so I can't get a feel for it.

Who would have thought finding a code example for both would be so hard?
 
The first two links had the same problem, but the third gave a clue to a work around for what looks like a bug.

There seems to be a bug in swing that makes it hard to get menus and forms to work together. The fix was to create a second JPanel and insert it into the container.

Here's working code that would be easy to modify.

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

public class myIcon extends JApplet
{
public static JMenuBar createMenuBar(){
JMenuBar menubar;
JMenu mnuPopup_421;
JMenuItem mnuPopupMonth_423;
JMenuItem mnuPopupMonth_427;
JMenuItem mnuPopupMonth_431;
JMenuItem mnuPopupMonth_435;
JMenuItem mnuPopupMonth_439;
JMenuItem mnuPopupMonth_443;
JMenuItem mnuPopupMonth_447;
JMenuItem mnuPopupMonth_451;
JMenuItem mnuPopupMonth_455;
JMenuItem mnuPopupMonth_459;
JMenuItem mnuPopupMonth_463;
JMenuItem mnuPopupMonth_467;

menubar = new JMenuBar();
// Menu Parent mnuPopup
//setting up Menu - mnuPopup_421
mnuPopup_421 = new JMenu("mnuPopup");

//setting up MenuItem - mnuPopupMonth_423
mnuPopupMonth_423 = new JMenuItem("January");
mnuPopup_421.add(mnuPopupMonth_423);

//setting up MenuItem - mnuPopupMonth_427
mnuPopupMonth_427 = new JMenuItem("February");
mnuPopup_421.add(mnuPopupMonth_427);

//setting up MenuItem - mnuPopupMonth_431
mnuPopupMonth_431 = new JMenuItem("March");
mnuPopup_421.add(mnuPopupMonth_431);

//setting up MenuItem - mnuPopupMonth_435
mnuPopupMonth_435 = new JMenuItem("April");
mnuPopup_421.add(mnuPopupMonth_435);

//setting up MenuItem - mnuPopupMonth_439
mnuPopupMonth_439 = new JMenuItem("May");
mnuPopup_421.add(mnuPopupMonth_439);

//setting up MenuItem - mnuPopupMonth_443
mnuPopupMonth_443 = new JMenuItem("June");
mnuPopup_421.add(mnuPopupMonth_443);

//setting up MenuItem - mnuPopupMonth_447
mnuPopupMonth_447 = new JMenuItem("July");
mnuPopup_421.add(mnuPopupMonth_447);

//setting up MenuItem - mnuPopupMonth_451
mnuPopupMonth_451 = new JMenuItem("August");
mnuPopup_421.add(mnuPopupMonth_451);

//setting up MenuItem - mnuPopupMonth_455
mnuPopupMonth_455 = new JMenuItem("September");
mnuPopup_421.add(mnuPopupMonth_455);

//setting up MenuItem - mnuPopupMonth_459
mnuPopupMonth_459 = new JMenuItem("October");
mnuPopup_421.add(mnuPopupMonth_459);

//setting up MenuItem - mnuPopupMonth_463
mnuPopupMonth_463 = new JMenuItem("November");
mnuPopup_421.add(mnuPopupMonth_463);

//setting up MenuItem - mnuPopupMonth_467
mnuPopupMonth_467 = new JMenuItem("December");
mnuPopup_421.add(mnuPopupMonth_467);
menubar.add(mnuPopup_421);

return menubar;
}


public static JPanel createPanel()
{
JPanel con;
ButtonGroup cbg;
JList picCalendar_15;
JList picDays_30;
JButton cmdChangeDate_46;
JButton cmdChangeDate_58;
JButton cmdChangeDate_70;
JButton cmdChangeDate_84;
JButton cmdToday_96;
JCheckBox chkStayOnTop_105;
JButton cmdToday_113;
JButton cmdChangeDate_122;
JButton cmdChangeDate_134;
JButton cmdChangeDate_148;
JButton cmdChangeDate_160;
JButton cmdClose_172;
JList picDays_181;
JList picCalendar_197;

con = new JPanel();
con.setOpaque(true);
con.setFont(new Font("Helvetica", Font.PLAIN, 12));
con.setLayout(null);
con.setSize(526, 322);

cbg = new ButtonGroup();

//setting up PictureBox - picDays_30
picDays_30 = new JList();
JScrollPane sppicDays_30 = new JScrollPane(picDays_30);
sppicDays_30.setLocation(280, 80);
sppicDays_30.setSize(224, 16);
con.add(sppicDays_30);

//setting up JButton - cmdToday_96
cmdToday_96 = new JButton("Today");
cmdToday_96.setBounds(280, 250, 66, 21);
con.add(cmdToday_96);

//setting up JCheckBox - chkStayOnTop_105
chkStayOnTop_105 = new JCheckBox("Stay on top of other windows.");
chkStayOnTop_105.setBounds(20, 290, 246, 18);
con.add(chkStayOnTop_105);

//setting up JButton - cmdClose_172
cmdClose_172 = new JButton("&Close");
cmdClose_172.setBounds(415, 290, 97, 25);
con.add(cmdClose_172);

//setting up PictureBox - picDays_181
picDays_181 = new JList();
JScrollPane sppicDays_181 = new JScrollPane(picDays_181);
sppicDays_181.setLocation(25, 80);
sppicDays_181.setSize(224, 16);
con.add(sppicDays_181);

//setting up PictureBox - picCalendar_197
picCalendar_197 = new JList();
JScrollPane sppicCalendar_197 = new JScrollPane(picCalendar_197);
sppicCalendar_197.setLocation(25, 105);
sppicCalendar_197.setSize(225, 138);
con.add(sppicCalendar_197);

return con;
}


private void createGUI() {

getContentPane().setLayout(new BorderLayout(5,5));
getContentPane().add(createMenuBar(), BorderLayout.NORTH);
getContentPane().add(createPanel(), BorderLayout.CENTER);
}

public void init()
{

try {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't successfully complete");
}
}

public static void main(String args[])
{
myIcon applet = new myIcon();
Frame window = new Frame("myIcon");
window.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
applet.init();
window.add("Center", applet);
window.setTitle("ICD Calendar");
window.setBounds(20,20,550,400); // set location and size
window.setVisible(true);
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top