Solved

Buttons/Events/Colors in 1.0 Model

Posted on 1998-05-05
6
174 Views
Last Modified: 2006-11-17
I've been trying to make an easier roll-over applet using buttons, changing the colors of the text and button based on the movement of the mouse in 1.0 model for older browsers.  The problem is this.  When I use Netscape 3.0, the button colors don't change (I use setBackground) but the events work (and the text/Foreground color changes as it should).  When I use Netscape 4.0, the button colors change fine, but the events aren't working.  

If it would help to see what I'm talking about, go to:
 http://www.mnplan.state.mn.us/datanetweb/Martintmp/Hover/h.html (and h2.html)

h.html is the applet I have that works, but doesn't use buttons.  h2.html uses the buttons and displays the problem described above.  I think I have the code up as well.

What's going on and how do I fix it?

webslider
0
Comment
Question by:webslider
[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
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:fontaine
ID: 1220575
I have tried with N4.0 and h2.html, the Java console shows that there is a java.lang.NullPointerException in your program. If you post it, I can take a quick look.
0
 

Author Comment

by:webslider
ID: 1220576
if you just look at:
http://www.mnplan.state.mn.us/datanetweb/Martintmp/Hover/

The directory is readable.  Look at hover2.java.


0
 
LVL 5

Accepted Solution

by:
fontaine earned 50 total points
ID: 1220577
When you try to make your own component, you should rather subclass Panel for example. If you compare your implementation with Button.java, you will see that most of the methods of Button are no more valid. In fact, all that remains is...the 3D border. When you try to change the color of a component, don' forget to call its repaint() method. Also, inner classes are JDK 1.1 (your MultiButton is implemented as an inner class in hover2.java). All of this put together gives the following code:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class hover2 extends Applet {
   MultiButton mbutton;
   URL link = null;
   String line1txt = "";
   String line2txt = "";
   Color bkcolor = Color.white;
   Color bktxtcolor = Color.black;
   Color h_bkcolor = Color.white;
   Color h_bktxtcolor = Color.black;

   public void init() {
      setLayout(null);

     /*  Get Parameters */
      String param = new String();
      param = getParameter("text1");
      if (param != null)
        line1txt = param;  

      param = getParameter("text2");
      line2txt = param;   // if null, won't print on button

      param = getParameter("color");  // regular backgroundcolor
      if (param != null) {
         if (param.charAt(0) == 35) {   // 1st char is a '#'
             param = param.substring(1);  // get all chars but '#'
             bkcolor = new Color(Integer.parseInt(param,16));   // # b/t 0-255
         } else
             bkcolor = new Color(255,255,255);
      }

      param = getParameter("textcolor");  // regular textcolor
      if (param != null) {
         if (param.charAt(0) == 35) {   // 1st char is a '#'
             param = param.substring(1);  // get all chars but '#'
             bktxtcolor = new Color(Integer.parseInt(param,16));   // # b/t 0-255
         } else
             bktxtcolor = new Color(0,0,0);
      }

      mbutton = new MultiButton(line1txt,line2txt);
      mbutton.resize(size().width,size().height);
      mbutton.setBackground(bkcolor);
      mbutton.setForeground(bktxtcolor);
      add(mbutton);
  }

  public boolean mouseEnter(Event e, int x, int y) {
       mbutton.setBackground(Color.green);
       mbutton.setForeground(Color.blue);
       mbutton.repaint();
       return true;
  }

  public boolean mouseExit(Event e, int x, int y) {
       mbutton.setBackground(bkcolor);
       mbutton.setForeground(bktxtcolor);
       mbutton.repaint();
       return true;
  }

  public boolean mouseDown(Event e, int x, int y) {
       mbutton.setBackground(Color.red);
       mbutton.setForeground(Color.white);
       mbutton.repaint();
       return true;
   }

   public boolean mouseUp(Event e, int x, int y) {

      try {
        link = new URL(getDocumentBase(),"other.html");
      } catch (MalformedURLException mue) {
            System.out.println("URL error");
      }

      getAppletContext().showDocument(link,"_self");
      return true;
   }


}

   class MultiButton extends Panel {
       String label1, label2;

       MultiButton(String s1, String s2) {
           label1 = s1;
           label2 = s2;
       }

       public void paint(Graphics g) {
            super.paint(g);
            Rectangle r = bounds();

            if (label1 != null) {
                 g.drawString(label1,r.x+5, r.y+r.height/3);
                 g.drawString(label2,r.x+5, r.y+(2*r.height)/3);
            } else {
                 g.drawString(label1,r.x+5, r.y+r.height/3);
            }
       }


   }

The code does what you need and has been tested on Netscape 4.0 and IE 4.0.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:webslider
ID: 1220578
So, are you saying that there is no way I can use buttons versus panels to do the roll over?  
0
 
LVL 5

Expert Comment

by:fontaine
ID: 1220579
The Button class provided with the AWT is a heavyweight component. This means that it is implemented through platform-dependent GUI. Because of that, you can't directly change it by overriding paint(). The effect is simply not guaranteed. If you try your original code with IE, for example, you will see that the two-lines of text that serve as label are not shown. The following article will interest you:

http://www.javaworld.com/javaworld/jw-03-1997/jw-03-imagebutton.html
0
 

Author Comment

by:webslider
ID: 1220580
Thanks so much!
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

630 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