Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Best way to work with internal frames.

  1. #1
    Member Hikaros's Avatar
    Join Date
    Sep 2013
    Posts
    42
    My Mood
    Love
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Question Best way to work with internal frames.

    Hi, i'm working in a big project and i am using internal frames however i came a cross several questions (first time doing it in java).
    What is the best way to work with the internal frames? this is how my classes are managed:

    main
    --mainFrame (this is the desktop frame)
    ----method for loginFrame (method where internal frame is created)
    ----method for mainFrame's menu (where it is created)
    ----other methods


    so basically i have methods to create internal frames in the mainFrame class, i thought about creating a different classes for each internal frame but i have a problem when adding the action listeners because what i want the buttons to do. in some cases i need to modify the mainFrame or some of its variables from the action listener in a button in an internal frame but how do i interact back and forth with the desktopframe (mainFrame) from internal frames made in another class? or is the best approach to just make the actual frames in different classes and handle the Action Listeners in the mainFrame?

    i'm lost since is the first time i'm trying to make a project involving desktop frames/ internal frames in java and it is seriously different from .net u_u i will appreciate your advices in what are the best approaches to do this

    PD: this involves databases and i was also wondering, is it better to leave the connections open through the entire application or just open the connections when needed and close them when done over and over and over?


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,554
    Thanks
    24
    Thanked 304 Times in 286 Posts

    Default Re: Best way to work with internal frames.

    Ok, well my first question would be: are you sure you mean internal frames and not panels?

    As far as the interaction back and forth goes, I've handled this approach in the past by creating separate classes for each content panel. However, I would have an abstract class (which extends JPanel, or perhaps in your case: JInternalFrame) with a variable referring to the mainFrame and a constructor which accepts a mainFrame object. All of my content panel classes extend from my abstract class, and use the super constructor with the mainFrame parameter. Then, when I create the content panels, I pass in the instance of the mainFrame each time. This allows backwards interaction.
    Here is a basic code example of what I am describing:
    public class MainFrame extends JFrame {
    	public MainFrame() {
    		super();
    		FirstPanel firstPanel = new FirstPanel(this);
    		SecondPanel secondPanel = new SecondPanel(this);
    	}
    }
     
    public abstract class AbstractPanel extends JPanel {
    	private MainFrame mainFrame;
    	public AbstractPanel(MainFrame mf) {
    		super();
    		mainFrame = mf;
    	}
    	public MainFrame getMainFrame() {
    		return mainFrame;
    	}
    }
     
    public class FirstPanel extends AbstractPanel {
    	public FirstPanel(MainFrame mf) {
    		super(mf);
    	}
    }
     
    public class SecondPanel extends AbstractPanel {
    	public SecondPanel(MainFrame mf) {
    		super(mf);
    	}
    }

    As for the database question: it depends on how often you are querying. If you do a lot of querying, you can probably keep it open. But if you only query in the beginning and then again in the end (for example), it is probably best to close it each time.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Hikaros (October 21st, 2013)

  4. #3
    Member Hikaros's Avatar
    Join Date
    Sep 2013
    Posts
    42
    My Mood
    Love
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Default Re: Best way to work with internal frames.

    Ohh thanks i will check right now if it works for what i want :3

    this is basically what i am doing right now, any advices or should i just go along with what you first said? o:

    public class MainFrame extends JFrame{
        private JDesktopPane theDesktop;
        public MainFrame(){
            theDesktop = new JDesktopPane(); //creates desktop pane
            add(theDesktop); //add desktop pane to frame
     
            LoginFrame(); //method creating the login internal frame
        }
    }
     
    public void LoginFrame(){
        final JInternalFrame login = new JInternalFrame("Internal Frame title", true, true, true, true);
     
    /*
    part of code where adding the action listeners from buttons which interact directly with
    the JDesktopPane (adds stuff to it - sets the JMenuBar from action listener here)
    */
     
    }
     
    public JMenuBar createMenu(){
    ...//creates the menubar here and returns it
    }

    and basically that is what i am doing, wanted to see if it is better to have different classes for JInternalFrames to be used in the options of the JMenu and what was the best approach to it or if i should just do the methods for every JInternalFrame like that (which something tells me it shouldn't be done that way)

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,554
    Thanks
    24
    Thanked 304 Times in 286 Posts

    Default Re: Best way to work with internal frames.

    Why does your MainFrame extend JFrame but doesn't use any JFrame method? If you want your MainFrame class to behave like a JFrame, your first call in your constructor should be to a super constructor.

    Consider your program from a long-term management point of view. The primary reason I separate my panel classes is to separate each panel's components from each other. Each panel class contains only the gui elements needed for that panel. When you create all of your panels in one huge gui class, you have all of those elements all thrown into once class with each other. On a tiny program this is manageable. But for a large program, or even a medium program, all of these variables together can lead to confusion for you or other developers, which greatly reduces how quickly and efficently you can modify your code.
    Even creating them with methods in one gui class will lead to you having to create all the gui elements in that class. They may only have the scope of the method creating the internal frame, but that still results in a huge class file with hundreds, or maybe thousands of lines of code. I'd bet my life savings on this approach becoming too cumbersome for you to comfortably maintain in the long term.

    Having many different class files can also appear cumbersome initially, but I personally prefer a lot of small files with specific goals and operations, compared to one massive file where I attempt to do everything. A lot of small files are thousands of times easier to maintain than one massive file.

    You can combine both mine and your designs by creating a class like this:
    public LoginFrame extends JInternalFrame {
    	private MainFrame mainFrame;
     
    	public LoginFrame(MainFrame mf, String title, boolean bool1, boolean bool2, boolean bool3, boolean bool4) {
    		super(title,bool1,bool2,bool3,bool4);
    		mainFrame = mf;
    		// Create and add all of your log in frame components in the rest of your constructor
    	}
    }
    And then in your MainFrame class you can have a method like this:
    public void createLoginFrame() {
    		final JInternalFrame login = new LoginFrame(this,"Internal Frame title", true, true, true, true);
    	}
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Hikaros (October 21st, 2013)

  7. #5
    Member Hikaros's Avatar
    Join Date
    Sep 2013
    Posts
    42
    My Mood
    Love
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Default Re: Best way to work with internal frames.

    ooohh okay okay! i shall do this then! thank you very much <3

Similar Threads

  1. linking frames
    By tegalinks in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 25th, 2013, 06:48 AM
  2. Does the Scanner class use an internal buffer?
    By Fazan in forum Java Theory & Questions
    Replies: 12
    Last Post: October 9th, 2012, 04:07 PM
  3. Question on Frames.
    By dassix in forum Java Theory & Questions
    Replies: 2
    Last Post: April 24th, 2012, 05:03 PM
  4. Question on HashMap internal implementation
    By tcstcs in forum Java Theory & Questions
    Replies: 6
    Last Post: November 19th, 2011, 11:20 AM
  5. GIF frames
    By vsector in forum AWT / Java Swing
    Replies: 0
    Last Post: April 15th, 2010, 05:25 PM