Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Buttons/Events/Colors in 1.0 Model

Posted on 1998-05-05
6
Medium Priority
?
181 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 200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…
Suggested Courses

670 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