Results 1 to 2 of 2
  1. #1
    nick2price is offline Member
    Join Date
    May 2008
    Posts
    27
    Rep Power
    0

    Default Loading a class within a different class

    I am having problems carrying out a task. I have one class which has a JPanel with a couple of buttons on it, which have action Events:
    Java Code:
    private JPanel createButtonPanel() {
        	
                JButton btnRegister = new JButton("REGISTER");
                JButton btnLogin = new JButton("LOGIN");
                btnRegister.addActionListener(new ActionListener() {
    
                    public void actionPerformed(ActionEvent evt) {
                    	new Register().setVisible(true);
                    }
                });
                btnLogin.addActionListener(new ActionListener() {
    
                    public void actionPerformed(ActionEvent evt) {
                        //Do stuff
                    }
                });
    		JPanel panel = new JPanel();
    		JPanel temp = new JPanel(new GridLayout(1,2, 5,5));
    		temp.add(btnRegister, BorderLayout.LINE_START);
    		temp.setPreferredSize(new Dimension(200, 40));
    		temp.add(btnLogin, BorderLayout.LINE_END);
    		temp.setPreferredSize(new Dimension(200, 40));
    		panel.add(temp);
    		return panel;
    	}
    Now when the register button is pressed, i want it to load up the Register application which is a different class. This is my Register class
    Java Code:
    package layout;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class Register {
    	
    	public Register(){
    		
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
    
    	}
    	
    	
        private static void createAndShowGUI() {
    	
    	String[] labels = {"Name: ", "ID No: ", "Username: ", "Password: "};
    	int numPairs = labels.length;
    
        JPanel pan = new JPanel(new SpringLayout());
        for (int i = 0; i < numPairs; i++) {
            JLabel lab = new JLabel(labels[i], JLabel.TRAILING);
            pan.add(lab);
            JTextField textField = new JTextField(10);
            lab.setLabelFor(textField);
            pan.add(textField);
        }
        
        SpringUtilities.makeCompactGrid(pan, numPairs, 2, 6, 6, 6, 6);   //(rows, columns, initx, inity, xpad, ypad    
        
    	JFrame frame = new JFrame("Ner");
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    	pan.setOpaque(true); 
    	frame.setContentPane(pan);
    	
    	//Display the window.
    	frame.pack();
    	frame.setVisible(true);
       }
    
         
    /*public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new Register().setVisible(true);
                }
            });
        }*/
    }
    For the time being i have commented out the main, and i created a Register object which runs the application (when there is a main!). Now i have tried loading this object in my other class buttons actionevent, but it says that it cannot access Register. Am i doing this the wrong way or what is going on?Cheers

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The setVisible method is a Window method. So, to compile, the object returned by new Register() would have to extend Window.
    Java Code:
    public void actionPerformed(ActionEvent evt) {
        // Instead of this
        new Register().setVisible(true);
        // try this
        new Register();

Similar Threads

  1. Replies: 0
    Last Post: 05-13-2008, 06:16 AM
  2. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:12 PM
  3. Class Reflection: Finding class modifiers
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:11 PM
  4. Replies: 0
    Last Post: 04-02-2008, 07:04 AM
  5. Help with class loading in java
    By mathias in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:51 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
  •