Jump to content

How can I call/edit the JFrame from another class ?

- - - - -

  • Please log in to reply
5 replies to this topic

#1
SandyTeo

SandyTeo

    Newbie

  • Members
  • PipPip
  • 22 posts
Here the code :
public class Main {


    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {


            private JFrame frame;


            public void run() {

                try {

                    UIManager.setLookAndFeel("com.jtattoo.plaf.smart.SmartLookAndFeel");

                } catch (ClassNotFoundException ex) {

                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

                } catch (InstantiationException ex) {

                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

                } catch (IllegalAccessException ex) {

                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

                } catch (UnsupportedLookAndFeelException ex) {

                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

                }

                Image image = Toolkit.getDefaultToolkit().getImage("\\NYP_logo.png");

                frame = new JFrame("Kit");

                frame.setIconImage(image);

                frame.setSize(698, 500);

                frame.setLocationRelativeTo(null);

                frame.setVisible(true);

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                System.setProperty("user.dir", "\\Desktop");

                TestFile_Tab descriptor = new TestFile_Tab();

                descriptor.testfile_tab();


            }

        });

    }

    private JFrame frame;


    public JFrame getFrame() {

        return frame;

    }

}


public class TestFile_Tab extends JFrame {

    private JFrame frame;


public void testfile_tab() {

        Main TestFile = new Main();

        frame = TestFile.getFrame();

        JButton Button = new JButton("super");

        Button.setBounds(10, 10, 10, 10);

        frame.add(Button);


    }


}


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You're using a different JFrame in the main method than the one your getter returns.

public class Main {


    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {


            [B][COLOR="red"]private JFrame frame;[/COLOR][/B]


           ....

        });

    }

    [B][COLOR="#9acd32"]private JFrame frame;[/COLOR][/B]


    public JFrame getFrame() {

        return frame;

    }

}


Better change your class so your code is not inside public static void main.
Try to use the main method just to instantiate classes and start up the whole system. Not to actually do stuff in.



public class Main {

     private JFrame frame;


     public Main(){

            SwingUtilities.invokeLater(new Runnable() {   

            public void run() {

                try {

                    UIManager.setLookAndFeel("com.jtattoo.plaf.smart.SmartLookAndFeel");

                } catch (ClassNotFoundException ex) {

                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

                } catch (InstantiationException ex) {

                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

                } catch (IllegalAccessException ex) {

                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

                } catch (UnsupportedLookAndFeelException ex) {

                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

                }

                Image image = Toolkit.getDefaultToolkit().getImage("\\NYP_logo.png");

                frame = new JFrame("Kit");

                frame.setIconImage(image);

                frame.setSize(698, 500);

                frame.setLocationRelativeTo(null);

                frame.setVisible(true);

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                System.setProperty("user.dir", "\\Desktop");

                TestFile_Tab descriptor = new TestFile_Tab();

                descriptor.testfile_tab();


            }

        });

     }


    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

         Main main = new Main();

         JFrame frame = main.getFrame();  //you got the correct frame here <<--

    }    


    public JFrame getFrame() {

        return frame;

    }

}


#3
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
wow people facing the same problem as me :)

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
public static void main(String[] args) {

         Main main = new Main();

         OtherClass otherClass = new OtherClass([B]main[/B]);

}


public class OtherClass{

  public otherClass(Main main){

    JFrame frame = main.getFrame();

    frame.add( new JLabel("Hi OtherClass did this") );

 }

}



#5
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
ok I try to create but it will get
public class OtherClass {


    public OtherClass(.Main main) {

        throw new UnsupportedOperationException("Not yet implemented");

    }


}

instead of
public class OtherClass{

  public otherClass(Main main){

    JFrame frame = main.getFrame();

    frame.add( new JLabel("Hi OtherClass did this") );

 }

}


#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
What do you mean?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users