repainting due to a menu

O

Oxnard

I am extending JApplet. I want the background color of the JApplet to match
the background color of the web page. I found if I:

javax.swing.JLayeredPane jlp = new javax.swing.JLayeredPane();
jlp.setBackground(new java.awt.Color(0xfe,0xfe,0xf2));
setLayeredPane(jlp);

in the init() function I get the background color I'm looking for. However,
do this seems to cause repaint or paint not to be called. What is occuring
is within my JApplet I am creating a JMenuBar with a JMenu and the JMenu has
a couple of JMenuItems. If I select the Menu the MenuItems display however
if I do not select an Item from the Menu the Items do not go away. I would
like a repaint or paint to occur to repaint the JLayeredPane I am using. I
am pretty sure the issue is with the JLayeredPane as if I comment out the
creation and adding of jlp the JApplet repaints as one would expect, however
in this case the color is the default gary color I am trying to get away
from. My question is how can I get the JLayeredPane to redraw?

Here is the current code:



public class t4 extends javax.swing.JApplet {

/** Initializes the applet t2 This function Must be here*/
public void init() {

initComponents();
}

/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
setBackground(new java.awt.Color(0xfe,0xfe,0xf2)); // sets the background of
the JApplet

jlp.setBackground(new java.awt.Color(0xfe,0xfe,0xf2)); // changes the
background color
setLayeredPane(jlp); // makes the JLayeredPane created to the current
JLayeredPane of the JApplet

// Instanisate the objects
jMenuBar1 = new javax.swing.JMenuBar();
jMenuBar1.setBackground(new java.awt.Color(0xfe,0xfe,0xf2));

jMenu1 = new javax.swing.JMenu();
jMenu1.setText("t1");
jMenu1.setBackground(new java.awt.Color(0xfe,0xfe,0xf2));

jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem1.setBackground(new java.awt.Color(0xfe,0xfe,0xf2));
jMenuItem1.setText("Go to java.sun.com");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() { // An
Anonymous Inner Class
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});

jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem2.setBackground(new java.awt.Color(0xfe,0xfe,0xf2));
jMenuItem2.setText("Go to Oracle");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() { // An
Anonymous Inner Class
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});


// add the Items to the Menu jMenu1
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem2);

jMenuBar1.add(jMenu1);

// jMenu2.setText("Menu");
// jMenuBar1.add(jMenu2);

setJMenuBar(jMenuBar1);

}

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.out.println("here I am");
showStatus("test ");
urlString = getParameter("link_1");
try{
final java.net.URL url = new java.net.URL(urlString);
java.applet.AppletContext context = getAppletContext();
context.showDocument(url,"right");
}catch(java.net.MalformedURLException e){
e.printStackTrace();
}

}

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.out.println("here I am");
showStatus("test ");
urlString = getParameter("link_2");
try{
final java.net.URL url = new java.net.URL(urlString);
java.applet.AppletContext context = getAppletContext();
context.showDocument(url,"right");
}catch(java.net.MalformedURLException e){
e.printStackTrace();
}
}

private javax.swing.JLayeredPane jlp = new javax.swing.JLayeredPane(); //
creates a JLayeredPane so that the background color can be changed
// Variables declaration - do not modify
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private String urlString;
// End of variables declaration
}

Thanks for your time!
Ox
 
A

Andrew Thompson

I am extending JApplet. I want the background color of the JApplet to match
the background color of the web page. I found if I: ...
...However, do this seems to cause repaint or paint not to be called.

Whoa up a minute there. You should find out, rather than guess.
Put a System.out.println in both and find out which is being
called when. (don't forget to call the 'super'..)
...If I select the Menu the MenuItems display however
if I do not select an Item from the Menu the Items do not go away.

I did not observe that behaviour using Java 1.5.0 beta.
What Java version is your browser running?
Here is the current code:

Your code example had a couple of problems, please
look over this codument on creating an SSCCE..
public class t4 extends javax.swing.JApplet {

Write 'import' statements rather than reference every class
by it's fully qualified name.

/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/

....ahemm. 'Form Editor'?
<http://www.xdweb.net/~dibblego/java/faq/answers.html#Q34>

There was also a problem with line wrap.

HTH

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
 
O

Oxnard

Andrew Thompson said:
Whoa up a minute there. You should find out, rather than guess.
Put a System.out.println in both and find out which is being
called when. (don't forget to call the 'super'..)


I did not observe that behaviour using Java 1.5.0 beta.
What Java version is your browser running?


Your code example had a couple of problems, please
look over this codument on creating an SSCCE..


Write 'import' statements rather than reference every class
by it's fully qualified name.



...ahemm. 'Form Editor'?
<http://www.xdweb.net/~dibblego/java/faq/answers.html#Q34>

There was also a problem with line wrap.

HTH

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane


Java(TM) Plug-in: Version 1.4.1_02
Using JRE version 1.4.1_02 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\ox

using NetBeans 3.6 the java_home is 1.4.1_02

I'm not sure why the super needs to be called?
 
A

Andrew Thompson

...
I'm not sure why the super needs to be called?

Perhaps I should have said, "don't forget to call 'super'
if you override paint or repaint for debugging purposes"
...
Java(TM) Plug-in: Version 1.4.1_02

Early 1.4 versions were known to be buggy, try either
1.4.2_03 or 1.5.

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
 
O

Oxnard

Andrew Thompson said:
Perhaps I should have said, "don't forget to call 'super'
if you override paint or repaint for debugging purposes"


Early 1.4 versions were known to be buggy, try either
1.4.2_03 or 1.5.

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane


Got it! (Although I am a little unsure why) It seems the JLayeredPane did
not want to respond to a repaint()??
I then simply used a JPanel and the JApplet repaints as one would expect.
The menu is overwritten after selecting the item.
Here is the final code:


import java.net.URL;
import javax.swing.*;
import java.applet.AppletContext;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;

public class t4 extends javax.swing.JApplet {

/** Initializes the applet t2 This function Must be here*/
public void init() {
initComponents();
}

private void initComponents() {
jPanel = new javax.swing.JPanel();
jPanel.setBackground(new java.awt.Color(0xfe,0xfe,0xf2));
Container contentPane = getContentPane();
contentPane.add(jPanel);

// Instantiate the objects
jMenuBar1 = new javax.swing.JMenuBar();
jMenuBar1.setBackground(new java.awt.Color(0xfe,0xfe,0xf2));

jMenu1 = new javax.swing.JMenu();
jMenu1.setText("t1");
jMenu1.setBackground(new java.awt.Color(0xfe,0xfe,0xf2));

jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem1.setBackground(new java.awt.Color(0xfe,0xfe,0xf2));
jMenuItem1.setText("Go to java.sun.com");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
// An Anonymous Inner Class
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
repaint();
}
});

jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem2.setBackground(new java.awt.Color(0xfe,0xfe,0xf2));
jMenuItem2.setText("Go to Oracle");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
// An Anonymous Inner Class
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
repaint();
}
});

// add the Items to the Menu jMenu1
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem2);

jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);

}

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.out.println("here I am");
showStatus("test ");
urlString = getParameter("link_1");
try{
final java.net.URL url = new java.net.URL(urlString);
java.applet.AppletContext context = getAppletContext();
context.showDocument(url,"right");
}catch(java.net.MalformedURLException e){
e.printStackTrace();
}
}

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.out.println("here I am");
showStatus("test ");
urlString = getParameter("link_2");
try{
final java.net.URL url = new java.net.URL(urlString);
java.applet.AppletContext context = getAppletContext();
context.showDocument(url,"right");
}catch(java.net.MalformedURLException e){
e.printStackTrace();
}
}

private javax.swing.JPanel jPanel; // creates a JPanel so that the
background color can be changed
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private String urlString;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top