Results 1 to 6 of 6
  1. #1
    icedcougar is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default calling buttons from diffeent class

    Hey, first time posting:

    I have the following code:

    MyFrame.java
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import java.awt.*;
    
    
    public class MyFrame extends JFrame {
    
    
        public MyFrame() {
    
    		setTitle("My graphic frame");
    		setSize(1000, 800);
    
    		// Creates a menubar for a JFrame
    		JMenuBar menuBar = new JMenuBar();
    
    		// Add the menubar to the frame
    		setJMenuBar(menuBar);
    
    		// Define and add two drop down menu to the menubar
    		JMenu fileMenu = new JMenu("File");
    		JMenu editMenu = new JMenu("Edit");
                    JMenu helpMenu = new JMenu("Help");
    		menuBar.add(fileMenu);
    		menuBar.add(editMenu);
                    menuBar.add(helpMenu);
    
                    //JMenuItem exit = new JMenuItem("Exit");
                    //fileMenu.add(exit);
    
    		// Create and add simple menu item to one of the drop down menu
    		//JMenuItem menuItem;
    		JMenuItem  New = new JMenuItem("New");
    		fileMenu.add(New);
    		JMenuItem Open = new JMenuItem("Open");
    		fileMenu.add(Open);
    		JMenuItem Save = new JMenuItem("Save");
    		fileMenu.add(Save);
    		fileMenu.addSeparator();
    		JMenuItem Exit = new JMenuItem("Exit");
    		fileMenu.add(Exit);
    
                    //Exits window if exit is clicked
                    class exitaction implements ActionListener{
                        public void actionPerformed(ActionEvent e){
                            System.exit(0);
                        }
                    }
    
                    Exit.addActionListener(new exitaction());
                    //-------------------------------------------
    
    		JMenuItem Cut = new JMenuItem("Cut");
    		editMenu.add(Cut);
    		JMenuItem Copy = new JMenuItem("Copy");
    		editMenu.add(Copy);
    		JMenuItem Paste = new JMenuItem("Paste");
    		editMenu.add(Paste);
    
                    JMenuItem Help = new JMenuItem("Help contents");
                    helpMenu.add(Help);
                    JMenuItem About = new JMenuItem("About");
                    helpMenu.add(About);
    
    	}
    
    	public static void main(String[] args) {
    	
    		MyFrame me = new MyFrame();
                    //me.setSize(400,400);
                    me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    me.setVisible(true);
    
    	}
    }
    and another class:

    MyButtonPanel.java
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import  javax.swing.*;
    
    
    public class MyButtonPanel extends JFrame{
            
            public JButtons(){
               Panel p = new Panel();
               add(p, "South");
               ImageIcon lineGif = new ImageIcon("line.gif");
               JButton line = new JButton(lineGif);
               p.add(line);
            }
    }
    and i'm getting an error on the following line:

    Java Code:
     public JButtons(){
    Error: invalid method declaration; return type required.

    What would i put this as to be able to call it in my other file? (basically MyButtonPanel.java is creating buttons to put on the left with images as the button)

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

    Default

    Methods must have a return type, even if it's just void. The only "method-like" creature without a return type is a constructor, and that's no constructor since it's name doesn't match that of the class's.

    By the way, why is that class extending JPanel when it's called "ButtonPanel"? It seems to me that it would be better off extending JPanel.

  3. #3
    icedcougar is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    i changed it so its now:

    Java Code:
            public MyButtonPanel(){
               Panel p = new Panel();
               add(p, "South");
               ImageIcon lineGif = new ImageIcon("line.gif");
               JButton line = new JButton(lineGif);
               p.add(line);
            }
    so that gets rid of the error, but now how can i run this in MyFrame.java, so that the button(s) appear?

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

    Default

    Again, your MyButtonPanel needs to extend JPanel not JFrame since you can't add a JFrame to a JFrame. Then in your class with the JFrame, add a MyButtonPanel instance to the JFrame. Most importantly however, you need to read the Swing tutorials on JFrames, JPanels, Layout Managers, and shoot... Swing in general, since you don't want to guess when it comes to coding in Swing.

    Much luck and please come back if you get stuck.

  5. #5
    icedcougar is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    it works, current code:
    Java Code:
    public class MyButtonPanel extends JPanel{
            
            public MyButtonPanel(){
               
    
               Panel p = new Panel();
               add(p, "South");
               ImageIcon lineGif = new ImageIcon("line.gif");
               JButton line = new JButton(lineGif);
               ImageIcon rectGif = new ImageIcon("rect.gif");
               JButton rect = new JButton(rectGif);
               ImageIcon ovalGif = new ImageIcon("oval.gif");
               JButton oval = new JButton(ovalGif);
               p.add(line);
               p.add(rect);
               p.add(oval);
    
    
               
            }
    
            public static void main(String [] args){
                MyButtonPanel panel = new MyButtonPanel();
                panel.setVisible(true);
                
            }
    }
    the problem is now it displays up the top of the frame. How do i get it to display on the left side or 'west' side?

    also the buttons are very small in the position there in.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default

    Quote Originally Posted by icedcougar View Post
    the problem is now it displays up the top of the frame. How do i get it to display on the left side or 'west' side?

    also the buttons are very small in the position there in.
    Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

Similar Threads

  1. Calling on a class
    By gandalf5166 in forum New To Java
    Replies: 12
    Last Post: 03-24-2010, 10:58 PM
  2. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  3. Calling a class method from another class
    By caro in forum New To Java
    Replies: 4
    Last Post: 06-10-2009, 01:12 AM
  4. problem calling function from class to class
    By alin_ms in forum New To Java
    Replies: 3
    Last Post: 12-19-2008, 07:35 PM
  5. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 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
  •