Solved

Buttons/Events/Colors in 1.0 Model

Posted on 1998-05-05
6
152 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 95
VB Script to add site to Java Exception List 4 76
Apps blocked by Java 9 79
hibernate insert example 13 28
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

785 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