Results 1 to 5 of 5
  1. #1
    doobybug is offline Member
    Join Date
    Mar 2008
    Posts
    17
    Rep Power
    0

    Default Inherited actionPerformed method

    Hi,

    I have 3 classes -

    1 is called Template (it has a cancel button and a search button and an actionPerformed method along with other fields)

    2 is called Prices (it is inheriting from Template but I need to add another button and an actionPerformed method along with it)

    3 is called Quote (it is inheriting from Template but I need to add another button and an actionPerformed method along with it)

    Since when inheriting the subclass method actionPerformed is taking precedence how can I extend the functions of the superclass actionPerformed method and add new functions to it?


    Please help me!
    Thanks a lot

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    how can I extend the functions of the superclass actionPerformed method and add new functions to it?
    Not sure you can without completely overriding and replacing it with new code.
    You could call the super method with super.actionPerformed(...) to do the super class's functions and then do your own.

    Can you post the code you are having a problem with?

  3. #3
    doobybug is offline Member
    Join Date
    Mar 2008
    Posts
    17
    Rep Power
    0

    Default

    This is the template class: (superclass)

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.Scanner;
    
    public class Template extends JFrame implements ActionListener{
       
        JLabel style = new JLabel ("STYLE");
        JLabel furniture = new JLabel("FURNITURE");
        JLabel type = new JLabel("TYPE");
        JLabel size = new JLabel("SIZE");
        JLabel colour = new JLabel("COLOUR");
        JLabel fabric = new JLabel("FABRIC");
        JLabel price = new JLabel("PRICE");
           
        String[]furniturePullDown = {"beds","tables","Benches","wardrobes","mirrors","dressing tables","Chest of drawers","armchairs","chandeliers","lampshades"};
        JComboBox furnitureBox = new JComboBox(furniturePullDown);
        JComboBox typeBox = new JComboBox();
        String[]stylePullDown = {"classic","contemporary","modern"};
        JComboBox styleBox = new JComboBox(stylePullDown);
        JComboBox fabricBox = new JComboBox();
        JComboBox sizeBox = new JComboBox();
        JComboBox colourBox = new JComboBox();
        
        JTextField priceTextField = new JTextField();
        
        ImageIcon[] images = new ImageIcon[3];
        ImageIcon[] imagesColours = new ImageIcon[8];
        JPanel pane;
        
        public Template(){
            setSize(900,500);
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            
            
            pane = new JPanel(null);
            
             imagesColours[0] =  createImageIcon ("C:\\Documents and Settings\\Charlene Laptop\\Desktop\\Sarah Dossier\\Colours\\biancomiele.png");
            imagesColours[0].setDescription("biancomiele");
            imagesColours[1] = createImageIcon("C:\\Documents and Settings\\Charlene Laptop\\Desktop\\Sarah Dossier\\Colours\\carrana gold leaf.png");
            imagesColours[1].setDescription("carrana gold leaf");
            imagesColours[2] = createImageIcon("C:\\Documents and Settings\\Charlene Laptop\\Desktop\\Sarah Dossier\\Colours\\champagne.png");
            imagesColours[2].setDescription("champagne");   
            imagesColours[3] =  createImageIcon ("C:\\Documents and Settings\\Charlene Laptop\\Desktop\\Sarah Dossier\\Colours\\gesso craquele.png");
            imagesColours[3].setDescription("gesso craquele");
            imagesColours[4] = createImageIcon("C:\\Documents and Settings\\Charlene Laptop\\Desktop\\Sarah Dossier\\Colours\\marquinia silver leaf.png");
            imagesColours[4].setDescription("marquinia silver leaf");
            imagesColours[5] = createImageIcon("C:\\Documents and Settings\\Charlene Laptop\\Desktop\\Sarah Dossier\\Colours\\melange gold leaf.png");
            imagesColours[5].setDescription("melange gold leaf");  
            imagesColours[6] =  createImageIcon ("C:\\Documents and Settings\\Charlene Laptop\\Desktop\\Sarah Dossier\\Colours\\melange silver leaf.png");
            imagesColours[6].setDescription("melange silver leaf");
            imagesColours[7] = createImageIcon("C:\\Documents and Settings\\Charlene Laptop\\Desktop\\Sarah Dossier\\Colours\\moka.png");
            imagesColours[7].setDescription("moka");
            colourBox.setAutoscrolls(true);
            colourBox.revalidate();
    
            
                    
            for(int i=0; i<imagesColours.length;i++){
               colourBox.addItem(imagesColours[i]);
            }
            
               
            style.setBounds(100,50,150,30);
            furniture.setBounds(100,100,150,30);
            type.setBounds(100,150,150,30);
            size.setBounds(100,200,150,30);
            colour.setBounds(100,250,150,30);
            fabric.setBounds(100,300,150,30);
            price.setBounds(100,350,150,30);        
            styleBox.setBounds(250,50,150,30);
            furnitureBox.setBounds(250,100,150,30);
            typeBox.setBounds(250,150,150,30);
            sizeBox.setBounds(250,200,150,30);
            colourBox.setBounds(250,250,70,30);
            fabricBox.setBounds(250,300,275,30);
            priceTextField.setBounds(250,350,150,30);
            
            styleBox.addActionListener(this);
            typeBox.addActionListener(this);
            
            pane.add(style);
            pane.add(furniture);
            pane.add(type);
            pane.add(size);
            pane.add(colour);
            pane.add(fabric);
            pane.add(price);
            pane.add(furnitureBox);
            pane.add(typeBox);
            pane.add(styleBox);
          
            pane.add(sizeBox);
            pane.add(colourBox);
            pane.add(fabricBox);
            pane.add(priceTextField);
            this.add(pane);
            setVisible(true);
            
        }
        public void actionPerformed(ActionEvent ae){
                
                if(ae.getSource()==styleBox){
                    typeBox.removeAllItems();
                    fabricBox.removeAllItems();
                 if(styleBox.getSelectedItem().equals("classic")){
                    typeBox.addItem("Chiara");
                    typeBox.addItem("Martina");
                    typeBox.addItem("Silvia");
                    
                    images[0] =  createImageIcon ("C:\\Documents and Settings\\Charlene Laptop\\Desktop\\Sarah Dossier\\Fabrics\\Linen.png");
                    images[0].setDescription("Linen");
                    images[1] = createImageIcon("C:\\Documents and Settings\\Charlene Laptop\\Desktop\\Sarah Dossier\\Fabrics\\Natura.png");
                    images[1].setDescription("Natura");
                    images[2] = createImageIcon("C:\\Documents and Settings\\Charlene Laptop\\Desktop\\Sarah Dossier\\Fabrics\\Cocoa.png");
                    images[2].setDescription("Cocoa");   
                    
                    for(int i=0; i<images.length;i++){
                       fabricBox.addItem(images[i]);
                    }
                }else if (styleBox.getSelectedItem().equals("contemporary")){
                    typeBox.addItem("Giulia");
                    typeBox.addItem("Melissa");
                    typeBox.addItem("Romina");
                    
                    images[0] =  createImageIcon ("C:\\Documents and Settings\\Charlene Laptop\\Desktop\\Sarah Dossier\\Fabrics\\Turquoise.png");
                    images[0].setDescription("Turquoise");
                    images[1] = createImageIcon("C:\\Documents and Settings\\Charlene Laptop\\Desktop\\Sarah Dossier\\Fabrics\\Scarlet.png");
                    images[1].setDescription("Scarlet");
                    images[2] = createImageIcon("C:\\Documents and Settings\\Charlene Laptop\\Desktop\\Sarah Dossier\\Fabrics\\Resole.png");
                    images[2].setDescription("Resole");   
                    
                    for(int i=0; i<images.length;i++){
                       fabricBox.addItem(images[i]);
                    }
                }else{
                    typeBox.addItem("Viola");
                    typeBox.addItem("Safira");
                    typeBox.addItem("Maia");
                    
                    images[0] =  createImageIcon ("C:\\Documents and Settings\\Charlene Laptop\\Desktop\\Sarah Dossier\\Fabrics\\Porcelain.png");
                    images[0].setDescription("Porcelain");
                    images[1] = createImageIcon("C:\\Documents and Settings\\Charlene Laptop\\Desktop\\Sarah Dossier\\Fabrics\\Kan.png");
                    images[1].setDescription("Kan");
                    images[2] = createImageIcon("C:\\Documents and Settings\\Charlene Laptop\\Desktop\\Sarah Dossier\\Fabrics\\Quarenchi.png");
                    images[2].setDescription("Quarenchi");   
                    
                    for(int i=0; i<images.length;i++){
                       fabricBox.addItem(images[i]);
                    }
                }
               
                typeBox.revalidate();
                typeBox.addActionListener(this);
                
               
            }
            
            if(ae.getSource() == typeBox){
                sizeBox.removeAllItems();
                try{
                   
                    FileInputStream myData = new FileInputStream("Sizes.txt");
       
                    DataInputStream in = new DataInputStream(myData);
                    Scanner input = new Scanner(new InputStreamReader(in));
                    String line = "";
                    
                    while (line!= null){
                        
                        try{
                            line = input.nextLine();
                            String newLine = line.substring(0, line.indexOf('='));
                            
                            
                            if(newLine.equals(styleBox.getSelectedItem()+" "+furnitureBox.getSelectedItem()+" "+typeBox.getSelectedItem())){
                                String size = line.substring(line.indexOf('=')+1);
                                sizeBox.addItem(size);
                            }else if(newLine.equals(styleBox.getSelectedItem()+" "+furnitureBox.getSelectedItem())){
                                String size = line.substring(line.indexOf('=')+1);
                                sizeBox.addItem(size);
                            }
                        }catch(java.util.NoSuchElementException e){
                            line = null;
                            
                        }
                    }
      
                        in.close();
                }       catch (IOException e){
                            System.err.println("Error: " + e.getMessage());
          
                        }
            }
            
            
        }
            
           
             public static void main(String args[]){
                Template t = new Template();
             }
             
             protected static ImageIcon createImageIcon(String path) {
                return new ImageIcon(path);
    
        }
    
    }
    This is the subclass

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.Scanner;
    
    public class PriceAmends extends Template{
        JLabel title = new JLabel("Price Amendment");
        
        FileInputStream myData;
        DataInputStream in;
        Scanner input;
               
        JButton cancel = new JButton("Cancel");
        JButton amend = new JButton("Amend");
        JButton search = new JButton("Search Price");
            
        public PriceAmends(){
           
            cancel.setBounds(495,400,120,35);
            amend.setBounds(360,400,120,35);
            search.setBounds(215,400,129,35);
            
            title.setBounds(400,10,600,35);
            title.setFont(new Font("sansserif",Font.BOLD,20));
            
            cancel.addActionListener(this);
            amend.addActionListener(this);
            search.addActionListener(this);
            
            
            pane.add(cancel);
            pane.add(amend);
            pane.add(search);
            pane.add(title);
            this.add(pane);
            setVisible(true);
            
        }
            
            public void actionPerformed(ActionEvent ae){
                
                
                 if(ae.getSource() == amend){
                      try{
                            
                            myData = new FileInputStream("Database.txt");
                            in = new DataInputStream(myData);
                            input = new Scanner(new InputStreamReader(in));
                            
                            FileWriter fstream = new FileWriter("Database2.txt",true);                  
                            BufferedWriter out = new BufferedWriter(fstream);
                            
                            String line = readLines();
                            
                              
                                  String line1 = styleBox.getSelectedItem()+" "+furnitureBox.getSelectedItem()+" "+typeBox.getSelectedItem()+" "+sizeBox.getSelectedItem()+" "+colourBox.getSelectedItem();
                                  String line2 = line.substring(0,line.indexOf('€')-1);
                                  while(line1.compareTo(line2)!=0){
                                      try{
                                        out.write(line);
                                        out.newLine();
                                        line = readLines();
                                        line2 = line.substring(0,line.indexOf('€')-1);
                                       }catch(java.util.NoSuchElementException e){
                                           line = null;
                                        }
                                    }
                                                                      
                       
                                
                                                      
                                out.write(line1+" "+priceTextField.getText());                    
                                out.newLine();
                                line = readLines();
                            
                                while(line!=null){
                                try { 
                                    
                                    out.write(line);
                                    out.newLine();
                                    line = readLines();
                                }catch(java.util.NoSuchElementException e){
                                    line = null;
                                }
                            }
                                
                            input.close();                   
                            out.close(); 
                            File f = new File ("Database.txt");
                            f.delete();
                            File f2 = new File ("Database2.txt");
                            f2.renameTo(new File("Database.txt"));
            
        
        
                    }catch (Exception e){
                      System.err.println("Error: " + e.getMessage());
                
                    }
                    
                }
                           
                 if(ae.getSource()==search){
                       try{
                           myData = new FileInputStream("Database.txt");
                        }catch(Exception e){
                        }
                        in = new DataInputStream(myData);
                        input = new Scanner(new InputStreamReader(in));
                        String line = readLines();
                    
                         while (line!= null){
                        
                             String line1 = styleBox.getSelectedItem()+" "+furnitureBox.getSelectedItem()+" "+typeBox.getSelectedItem()+" "+sizeBox.getSelectedItem()+" "+colourBox.getSelectedItem();
                             String line2 = line.substring(0,line.indexOf('€')-1);
                             if(line1.equals(line2)){
                                priceTextField.setText(line.substring(line.indexOf('€')));
                             }
                             line = readLines();
                       
                            }
                 }
                 
                 if(ae.getSource()==cancel){
                     setVisible(false);
                    }
                }
                 
                 public String readLines(){
                     try{
                   
                    
                    String line = "";
                    
                    while (line!= null){
                        try{
                        line = input.nextLine();
                        return line;
                        }catch(java.util.NoSuchElementException e){
                            line = null;
                            
                        }
                    }
      
                        in.close();
                }       catch (IOException e){
                            System.err.println("Error: " + e.getMessage());
          
                        }
                        return null;
                    }
                 
            
           
             public static void main(String args[]){
                PriceAmends mydata = new PriceAmends();           
             }
             
           
    
    }

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    That's a lot of code for a simple problem with overriding one method.

    Can you describe the problem:
    Compile errors,
    execution error,
    program doesn't do what you want: Please give details on what you do and what the program does and what you want it to do.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    This is why you generally use anonymous ActionListeners, which may or may not call a method in the main class to do the work.
    That way you don't end up with a load of unrelated events (buttons) all passing through the same bit of code.
    eg (note this is an example, no guarantee I have the syntax correct):
    Java Code:
    JButton b = new JButton();
    b.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            processButtonB();
        }
    });

Similar Threads

  1. Inherited method that returns string
    By Xeal Rebad in forum New To Java
    Replies: 5
    Last Post: 05-23-2011, 02:26 PM
  2. Inherited method returning bad value
    By viking90 in forum New To Java
    Replies: 11
    Last Post: 04-07-2010, 04:53 PM
  3. Replies: 2
    Last Post: 03-26-2010, 06:12 PM
  4. Replies: 1
    Last Post: 02-27-2010, 01:35 AM
  5. inherited from the Component class
    By ronald christian in forum New To Java
    Replies: 1
    Last Post: 10-24-2008, 08:55 PM

Posting Permissions

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