Results 1 to 2 of 2
  1. #1
    elenora is offline Member
    Join Date
    Apr 2011
    Rep Power

    Unhappy How to use abstract class?

    Hi, I implemented some code that has an alert class which is type of Canvas, that is like JFrame in Java and it has a keyPress method which must work different for each class that using alert class. For example the keyPress method has 2 buttons that the function of each button is different for each class! Could anyone tell me How can I implement that ?
    below is part of my code:

    public class myAlert extends Canvas implements CommandListener{
    public myAlert(int width,int height,String str,String left_str,String right_str){
    protected void paint(Graphics g) {
    g.setColor(0, 0,0);
    g.drawRoundRect(5, 180, width-10, height, 8, 8);
    g.drawRoundRect(15, 185, width-15, height-5, 7, 7);
    g.fillRoundRect(0, 180, width, height, 8, 8);
    protected void keyPressed(int keyCode)
    case -6: // select button
    //here is about my question!
    case -7: //close button
    //here is about my question!

    Public class M(){
    protected void keyPressed(int keyCode) {

    switch (state) {
    // do work for focus on the left
    if (keyCode >= KEY_NUM0 && keyCode <= KEY_NUM9 && digitsEntered < 10) {
    digits_left[digitsEntered] = (char) ('0' + (keyCode - KEY_NUM0));
    m= new myAlertw, 150,"Ok " ,"exit",”system”);
    //I want that if user click on Ok button the main page must be shown to user
    Display.getDisplay(midlet).setCurrent( m); // Midlet is a like Main class in Java
    Public class N(){
    public void commandAction(Command c, Displayable d) {
    String label = c.getLabel();
    else if(label.equals("Enter")){
    m= new myAlert(w, 150,”yes”,”No”,”Print” );
    // I want here that if user clicks on yes another message be shown to user
    Display.getDisplay(midlet).setCurrent( m);

    here is part of my code! as you see in M class and N class left button's function of Alert class is diffrent from each other! Now Could any body help me?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power


    Can you please use code tags?
    Unformatted code is hard to follow.

    Also, I'm not entirely clear on your question. Are you asking how to write two subclasses of myAlert, each with different keyPressed() methods?

Similar Threads

  1. Abstract class
    By DeepakZenTech in forum New To Java
    Replies: 8
    Last Post: 03-22-2011, 07:07 AM
  2. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 03:33 PM
  3. Can this be done using abstract class??
    By romina in forum New To Java
    Replies: 6
    Last Post: 03-02-2009, 01:46 PM
  4. Replies: 6
    Last Post: 02-11-2009, 11:51 AM
  5. Replies: 0
    Last Post: 04-02-2008, 07:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts