Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Calling a non-static method out of a package

  1. #1
    Kaffee is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Calling a non-static method out of a package

    Hello,
    my problem is that i cant call the "setCard"-method in the "HauptPanel"-class. I would apreciate every suggestion

    You propably wont need to look at this class:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import panels.*;
    
    public class JFrameWindow extends JFrame
    {
      HauptPanel hauptPanel;
      JFrameWindow()
      {
        super("TITEL");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setMinimumSize(new Dimension(100,100));
        setSize(500,500);
        setResizable(false);
        setLocationRelativeTo(null);
        
        hauptPanel = new HauptPanel();
    
        Container con = getContentPane();
        con.add(hauptPanel);
        hauptPanel.setCard("Hauptmenü");
    
        setVisible(true);
      }
    
      private void resizeWindow(int newWidth, int newHeight)
      {
        this.setSize(newWidth,newHeight);
      }
    
      public static void main(String args[])
      {
        JFrameWindow frame = new JFrameWindow();
      }
    }
    Java Code:
    package panels;
    
    import java.awt.CardLayout;
    import javax.swing.*;
    import java.awt.*;
    import panels.*;
    
    public class HauptPanel extends JPanel
    {
    
        private CardLayout cardLayout;
    
        public HauptPanel()
        {
    
            cardLayout = new CardLayout();
            setLayout( cardLayout );
    
            // Panel zum CardLayout hinzufügen
            add( new panels.HauptmenuePanel(), "Hauptmenü" );
            add( new panels.OptionenPanel(), "Optionen" );
            //add( new Panel3(), "Panel3" );
            //add( new Panel4(), "Panel4" );
            //add( new Panel5(), "Panel5" );
    
        }
    
    
        // Methode zeigt die gewünschte Seite (Panel) im Frame an
        public void setCard( String cardname ) {                                     <-- METHOD THAT HAS TO BE CALLED
            cardLayout.show( this, cardname );
        }
    
    }
    Here is the problem (at the very bottom):
    Java Code:
    package panels;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class HauptmenuePanel extends JPanel implements ActionListener {
        JButton b1;
        JButton b2;
    
        public HauptmenuePanel() {
          this.setBackground(Color.WHITE);
          b1 = new JButton("Start");
          this.add(b1);
          b1.addActionListener(this);
          
          b2 = new JButton("Optionen");
          this.add(b2);
          b2.addActionListener(this);
        }
        
        public void actionPerformed(ActionEvent e)
        {
          if(e.getSource()==b1){
            System.out.println("Start-button was pressed!");
            //hauptPanel.setCard("DrawingPanel");
          }
        
          if(e.getSource()==b2){
            System.out.println("Optionen-button was pressed!");
            //hauptPanel.setCard("Optionen");              <-- PROBLEM
          }
        }
    
    }
    Sorry for the wall of code, but you dont really need to understand it. I just need to call that one method.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Calling a non-static method out of a package

    You need to have a reference to the HauptPanel object in the HauptmenuePanel object. One way to do this is to pass this reference in the HauptmenuePanel class's constructor. For example (note changes indicated by the //!! comment):

    Java Code:
    class HauptmenuePanel extends JPanel implements ActionListener {
       JButton b1;
       JButton b2;
       private HauptPanel hauptPanel; //!!
    
       public HauptmenuePanel(HauptPanel hauptPanel) { //!!
          this.hauptPanel = hauptPanel; //!! 
          this.setBackground(Color.WHITE);
          b1 = new JButton("Start");
          this.add(b1);
          b1.addActionListener(this);
    
          b2 = new JButton("Optionen");
          this.add(b2);
          b2.addActionListener(this);
       }
    
       public void actionPerformed(ActionEvent e) {
          if (e.getSource() == b1) {
             System.out.println("Start-button was pressed!");
             hauptPanel.setCard("DrawingPanel");
             // hauptPanel.setCard("DrawingPanel");
          }
    
          if (e.getSource() == b2) {
             System.out.println("Optionen-button was pressed!");
             hauptPanel.setCard("Optionen"); //!!<-- PROBLEM
          }
       }
    
    }

    and

    Java Code:
       public HauptPanel() {
          cardLayout = new CardLayout();
          setLayout(cardLayout);
          add(new HauptmenuePanel(this), "Hauptmenu"); //!!
    Kaffee likes this.

  3. #3
    Kaffee is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Re: Calling a non-static method out of a package

    Hi,
    thank you a lot, you really helped me :)
    kindly regards, kaffee
    Last edited by Kaffee; 10-03-2011 at 12:16 AM.

Similar Threads

  1. Need help with calling a static method please.
    By toppcon in forum New To Java
    Replies: 12
    Last Post: 07-07-2011, 05:20 AM
  2. calling method of different project package class
    By oneofthelions in forum New To Java
    Replies: 3
    Last Post: 02-16-2010, 05:36 PM
  3. Replies: 3
    Last Post: 02-09-2010, 05:22 AM
  4. static method within same package not read
    By jon80 in forum New To Java
    Replies: 11
    Last Post: 06-15-2009, 09:47 AM
  5. Replies: 1
    Last Post: 02-10-2009, 10:03 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
  •