Switch Between Jpanels within a JFrame

Hi, I am new to this java thing and I need a hand with the following problem
I have 3 classes
Invaders
Game
OptionsMenu
Basically, you execute the program which executes Invaders, which then creates a JFrame with a border layout and adds the menu bar to the top, JPanel with either game or OptionsMenu to the center and a further JPanel to the bottom containing buttons and labels
The specifics of the problem is I can not work out how to change between the panels in the center of the border layout in the frame created by invaders.
When the program loads its ment to load with the options menu, which is no problem, the problem comes when I need to change the frame from the options menu to the game via a button on the options menu, the options menu passes the variables its gathered to the game constructor and it is then ment to display in the center of the border layout in the JFrame from invaders, any help would be appreciated, I have spent 2 days trying to get it to work and have not been sucessful

I can not work out how to change between the panels in the center of
the border layout in the frame created by invaders.Use a [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]Card Layout.

Similar Messages

  • How do you switch between jpanels in a jframe?

    I have, what seems, a relatively simple question; however I cant find a solution to it anywhere!
    I have a jframe with a jmenubar which has 4 menu items. When each item is clicked a new JFrame is displayed with certain content. What I am trying to do, instead of creating a new Jframe each time a menu item is clicked, is display all jpanels within the current jframe. When different menu items in the jmenu are clicked it "switches" the main jpanel to display the required content.
    So basically I want 1 JFrame, which has 4 JPanels, (alhough only 1 is shown at any given time). When a menu item in the jmenubar is clicked the JPanel which corresponds to the menu item is displayed on the jframe.
    If your still confused, I basically want jtabbedpane functionality only I want to control the switching through tabs via the jmenubar (and I dont want the jtabbedpane tabs obviously).
    Thanks

    Myles wrote:
    Okay that seems obvious ;)
    What do you mean by "validating the content pane" though?
    Thanks for your quick reply.Containers have a method called validate() which will update them when components are added and removed. Without this, you won't see added components.
    And yes, you can have more than 1 container. JPanels are containers themselves and can have components added and removed from them as well. You only have one content pane in the JFrame, but you could add two JPanels to this, one static and one to act as your swapping container.

  • Problem with insets and placement within a JFrame.

    I have written some code that actively renders (at about 100 fps) to a JPanel contained within a JFrame. The size of the frame and the size of the panel are both set to 1000 by 700 (odd i know..)
    Everything seems to work fine and I have encountered insets before so when I am rendering I have been adjusting accordingly. However I have started to try and draw my own menu bar at the bottom of the panel and this is where I am having problems. Whatever I draw seems to be too low and disappears off the bottom. To solve this I tried getting the insets from the JFrame:
    Insets insets = getInsets();
    xAdjustmentLeft = insets.left;
    xAdjustmentRight = insets.right;
    yAdjustmentTop = insets.top;
    yAdjustmentBottom = insets.bottom;When I do this on my vista machine I get insets of 3, 3, 23, 3 accordingly. When I print out the coordinates of mouse clicks I find that at the top left corner i get 3, 23 as you would expect. When I click in the bottom right corner of my screen I get 996 (ish), 716.
    The 996 can be explained by the left inset and my bad clicking however the 716 bears no relation to the top inset of 23 or the bottom inset of 3 in any way .... I am doing something wrong or have I missed something such as by adding the JPanel to the JFrame I have added or subtracted a distance somehow?
    I ran the program on my xp machine and got similar insets of 3,3,29,3. However when I click in the bottom right of the page I get exactly the same coordinates as on the vista machine .. this just doesn't seem to make sense?
    I should probably also mention how the rendering works just incase ... I'm drawing to a VolatileImage which I then draw onto the JPanel using its own graphical context. Interestingly anything I render on the image at y coord of 0 appears correctly at the top of the screen .... which it shouldn't because of the inset ... I'm so confused.
    I hope someone can help / understand my babblings. If you need more code I can post it.. just there's rather alot of it.

    Sorry, my bad I realised I was doing some rather stupid calculations elsewhere in the program. I have now managed to get my drawn bar where I need it to be on both machines.
    However, if I am rendering a picture of 1000 by 700 and the actual screen you are seeing is something along the lines of 993 by 674 (values minus the insets) this means I am wasting time rendering a picture larger than I am actually displaying, how to be normally interpret the sizes, do they go to the very outside of the panel or the visual element inside it? I could easily just increase the JFrame itself to be larger say 1006 by 726 and have the image the exact size within that.
    Whichever way I have solved my original problem so no worries.

  • Graphic distortion when switching between external and built-in display (rMBP)

    Recently (within the past two weeks or so) I've noticed a strange issue when switching between my external display (Thunderbolt) and my built-in display on my 15" rMBP. The following issue seems to be specific to Photoshop CS6.
    Typically, I'll have a PS document open on my external display and I'll wind up taking my to another location. If I close the PS document while on my built-in and then re-open it, I get all kinds of distortion and pixellation. Closing out PS and reopining, restarting, logging on/off; none of it seems to work. It seems like the issue is with the PSD itself, but it doesn't make any sense to me. Any ideas? Screenshot: http://i.imgur.com/iCipSc7.jpg

    Do you have intel gpy as well as other graphic card?  You may be viewing document on different gpu's.  PS does not like multiple gpu's.

  • Major delay when switching between folders in Mail

    More often than not when I switch between folders (inbox-to-trash or junk-to-inbox, etc) none of the messages in that folder show up and it says on top "0 messages".
    Sometimes they come back after 5 or 6 seconds. Other times it takes over a minute. There usually aren't that many messages in either the folder I'm coming from or going to.
    Any thoughts?

    Based on the tons of new messages showed up in the Trash, I suggest the following.
    Quit Mail first and using the Finder go to Home > Library > Mail.
    Copy the Mail folder and place the copy on the Desktop for backup purposes.
    Using the Finder, go to Home > Library > Mail > Envelope Index.
    Delete the Envelope Index file and empty the Trash.
    The Envelope Index file stores an index of all mailboxes and messages.
    Launch Mail and you will be prompted to import mailboxes. Select OK and allow the process to complete. This isn't really importing anything but creating a new Envelope Index file within the Mail folder and re-indexing all mailboxes and messages within.
    This may cause more previously deleted messages to re-appear but this should resolve the delay problem.
    After confirming all mailboxes are available and there is nothing unusual, you can delete the copy of the Mail folder placed on the Desktop.

  • Why can't I use command+alt+arrows to switch between tabs correctly anymore?

    I used to be able to use the method of command+alt+left or right arrows to switch between tabs. For example: I could be playing an online game, and be clicking things inside of the box on the screen that the game was on, use the method, and easily switch to another tab. Now, however, it doesn't let me switch between tabs unless i click outside of the game area.

    Don't forget that there are many "pieces" to internet access that must all be set to make manual IP selection work, namely:
    1) Your IP address
    2) The addresses of your DNS servers
    3) The default route via which packets leave your network ("router" in Network->TCP/IP)
    4) Your subnet mask
    As mentioned above you also need to assign an address from outside the DHCP pool that is on the same subnet, as at least some routers will refuse to route traffic that seems to originate from IP addresses that fall within the DHCP pool over which it has control if it did not yet assign that IP address to a client.

  • How do I replace one JPanel on a JFrame with another?

    I want to replace one JPanel on a JFrame with a different JPanel when the user clicks on a certain menu item. The menu item is working corrrectly but I cant seem to repaint / refresh the component.
    My code is as follows:
    c.add(knotPanel, BorderLayout.CENTER);
    c.add(stepPanel, BorderLayout.NORTH);
         if (stepChoice != 9) {
         c.remove(ownPanel);
         c.add(lessonPanel, BorderLayout.EAST);
         else {
         c.remove(lessonPanel);
         c.add(ownPanel, BorderLayout.EAST);
         c.invalidate();
         c.validate();
         c.update(c.getGraphics());
    Any ideas?

    I think you should use CardLayout manager.
    With CardLayout you can switch beetwen JPanels without removing or adding.

  • Full screen mode: How to switch between open windows?

    Hi There,
    Is there way to get CMD + ` working in full screen mode?
    Other shortcuts like CMD + OPT [ ] to switch between open tabs and CMD + TAB to switch apps seem to work - but without the ability to switch between open windows within the same app, full screen mode is kind of useless.
    Any pointers in the right direction would be much appreciated.
    BTW I'm using Yosemite but the forums won't find a Yosemite community (not sure the community search is working properly)
    Cheers

    or:
    And some additional shurtcuts:
    OS X: Keyboard shortcuts - Apple Support
    http://www.danrodney.com/mac/
    cmscss wrote:
    Hi There,
    Is there way to get CMD + ` working in full screen mode?
    Feedback: www.apple.com/feedback/

  • Why is the mouse 'sticking' and it switches between windows?

    Hello,
    So this morning my mac has had a few issues, all within about a minute of each other:
    -mouse 'sticking', not totally frozen but isn't moving smoothly
    - will switch between open windows and even just fly over to the dashboard randomly
    -open random windows
             for ex. the little window that would open if I right clicked on something, (except I was just typing) and it's happened multiple times today, and never before.
    * I had Pages '09 and Safari open.
    * I've restarted my computer and only launched Safari  and the problem seems to have gone away, making me think there might be a corrupted file or something in Pages.
    Any suggestions would be much appreciated!
    Brandon

    The mouse (at least the Magic Mouse) sticking and flying are well known problems for which there seems to be no cure in sight. Just grit your teeth and try to think something complimentary about Apple.

  • Itunes unresponsive when switching between two devices

    For God Sake help me with this,
    i have so many apps on my iTunes(mac) ,since i updated my itunes from 11 to 12 it has been my worst nightmare ever..
    whenever i attach two or more devices ,switching between devices it becomes unresponsive so that i have to force quit iTunes
    i googled this issue,tried lots of things.but no success
    plzzzzzz help

    Paul,
    Thanks for responding.
    I'm still confused as to which field the script goes in and how to enter it in relationship to any existing script.
    Instead of me trying to explain verbally, I'll show you what I have.
    Document Hierarchy:
    Subform path & script: (I'm assuming I need one for each subform)
    xfa.host.resetData("xfa.form1.Page1.subform1");
    xfa.host.resetData("xfa.form1.Page1.subform2");
    "Radio Button List" Click Event script that shows/hides subforms:
    form1.Page1.FLOC.RadioButtonList::click - (JavaScript, client)
    if (this.rawValue == 1)
        Subform1.presence = "visible";
        Subform2.presence = "hidden";
    else if (this.rawValue == 2)
        Subform2.presence = "visible";
        Subform1.presence = "hidden";
    if (this.rawValue == 1)
        Efficiency.presence = "visible";
    if (this.rawValue == 2)
        Efficiency.presence = "visible";
    I've tried putting the script  inside the curly brackets, outside the curly brackets, at various locations within the above script, but no matter where I put it, it clears the entire form.
    Would you be so kind as to re-write the above script  properly so I can see what it's supposed to look like.
    Thanks!

  • Whatever happened to Alt+F6 (to switch between modeless dialogs)?

    In all versions of Windows until Vista (I think that was when it went
    missing) you used to be able to press the standard defined accelerator
    Alt+F6 to switch between an application's main window and any modeless
    dialogs it had.
    It's still documented as such here:
    http://msdn.microsoft.com/en-us/library/windows/desktop/bb246447(v=vs.85).aspx
    "ALT+F6
     Switch to next window within application (between modeless secondary
    windows and their primary window).
    For example, in Notepad, when you have its Find dialog open, under
    XP/Windows 2003 (and all earlier Windows versions) you can press
    Alt+F6 to switch between the main window and the Find dialog.
    This keystroke was particularly useful when an application's modeless
    dialog was positioned off the screen and couldn't be clicked on it
    with the mouse. You could press Alt+F6 to activate the modeless
    dialog, then Alt+Space to popup the modeless dialog's menu, then
    invoke the move operation - and get the modeless dialog back on
    screen.
    On all versions of Windows subsequent to XP/W2K3, as far as I know
    there's no way of activating a modeless dialog with the keyboard.
    Does anyone know if there's a substitute keystroke, and if not, out of
    interest why we lost that keystroke?
    Dave

    Are you using KMS?
    I'm not sure but you could try launching your WM from .xinitrc:
    'exec ck-launch-session WM'
    Instead of WM you can try 'startxfce4' 'xfce' or wait for somebody smarter than me :-)

  • Crystal Reports - Switch between multiple databases at runtime (esp. DB2)

    Hi,
    we are currently developing a reporting application in .NET (C#) with an integrated Crystal Viewer. We want to be able to change the databse dynamically at runtime, especially between different DBMS.
    We encountered no problems when switching between a SQL Server and an Oracle DB, but we ran into big trouble if trying to connect to a DB2 db.
    In the report itself we have a SQL Server connection defined that is used during the design phase.
    In our application we change the connection at runtime using the following code:
    CrystalReportViewer viewer = new CrystalReportViewer();
    ReportDocument document = new ReportDocument();
    document.Load("<<path>>");
    Tables tables = document.Database.Tables;
    foreach (Table t in tables)
        ConnectionInfo info = new ConnectionInfo();
        info.UserID = "<UserId>";
        info.Password = "<Password>";
        info.ServerName = "<DSN of DB2 DB>";
        info.DatabaseName = "<DB2 DB Alias>";
        TableLogOnInfo tableInfo = t.LogOnInfo;
        tableInfo.ConnectionInfo = info;
        t.ApplyLogOnInfo(tableInfo);
    // ... Setting parameter values ...
    viewer.ReportSource = document;
    We also tried to add the following lines of code after the "ApplyLogOnInfo" call without any success:
    t.LogOnInfo.ConnectionInfo.UserID = "<UserId>";
    t.LogOnInfo.ConnectionInfo.Password = "<Password>";
    t.LogOnInfo.ConnectionInfo.ServerName = "<DSN of DB2 DB>";
    t.LogOnInfo.ConnectionInfo.DatabaseName = "<DB2 DB Alias>";
    Especially we are wondering because we don't get any error message. The Crystal Viewer just opens and shows an empty report without any data records. The TestConnectivity method called on each table (we are only using one table at the moment) also returns true.
    Furthermore it seems that whenever we call ApplyLogOnInfo, the UserID, password, server name and database name is resetted to the values defined in the data connection within the report (SQL Server, used in the desing phase as mentioned above).
    Any ideas, what we are doing wrong?
    Kind regards,
    Markus
    Edited by: X-Giesi-X on Feb 10, 2010 4:53 PM

    Hi Markus
    OK. Make sure you have the latest updates. First apply SP 2:
    https://smpdl.sap-ag.de/~sapidp/012002523100009038092009E/cr2008win_sp2.exe
    Than FP 2.4:
    https://smpdl.sap-ag.de/~sapidp/012002523100001024152010E/cr2008_fp24.zip
    Next, open the report in the CR designer and enable the "Verify on first refresh" option (File | report Options)
    If that does not help, export the report to rpt file format and try to view the exported report in the CR designer. What do you see?
    Ludek

  • How to switch between libraries?

    I have got to do a disk repair. I'm making sure my backup is correct as I don't want to lose all my precious pics. Lucky I checked as I've just discovered I've got two iPhoto libraries! The one I appear to be using is in the mac folder - where Users folder is, Library, System Folder, Updaters etc. It's the first area you come to when you double click on your mac's symbol in upper right corner.
    However, I also have an iPhoto library within a folder which is in my User/Pictures area.
    I want to make sure that the iPhoto library I'm using is within my user/pictures folder as it's my user files which are being backed up. However, I don't want to trash the one which is there without looking in it to see what's within. How do I switch between libraries?
    If it's empty, I can presumable just bin it then use the recommended technique to move my other library to User\Pictures.
    Thanks!

    If you use iPhoto LIbrary Manager like Larry suggested it will display the library name in the top iPhoto frame like this:
    TIP: For insurance against the iPhoto database corruption that many users have experienced I recommend making a backup copy of the Library6.iPhoto (iPhoto.Library for iPhoto 5 and earlier) database file and keep it current. If problems crop up where iPhoto suddenly can't see any photos or thinks there are no photos in the library, replacing the working Library6.iPhoto file with the backup will often get the library back. By keeping it current I mean backup after each import and/or any serious editing or work on books, slideshows, calendars, cards, etc. That insures that if a problem pops up and you do need to replace the database file, you'll retain all those efforts. It doesn't take long to make the backup and it's good insurance.
    I've created an Automator workflow application (requires Tiger or later), iPhoto dB File Backup, that will copy the selected Library6.iPhoto file from your iPhoto Library folder to the Pictures folder, replacing any previous version of it. It's compatible with iPhoto 6 and 7 libraries and Tiger and Leopard. Just put the application in the Dock and click on it whenever you want to backup the dB file. iPhoto does not have to be closed to run the application, just idle. You can download it at Toad's Cellar. Be sure to read the Read Me pdf file.
    Note: There's now an Automator backup application for iPhoto 5 that will work with Tiger or Leopard.

  • Nested Panels: Switching between

    I'm writing a program that has to switch between two panels that are nested in a parent panel. Is anyone there who has done this before and has a workable solution? Thanks in advance...

    Thanks very much, I tried this and this is the error I got when I clicked on the 'proceedBtn' in this case colorBtn...
    Using the content pane, the program does not know where to add the panel, is there a way of directing it to add the panel to the parent panel directly?.. I couldn't find any method that helps in this direction. Thanks again for the help.
    java.lang.Error: Do not use Talk2Me.ChatRoom.add() use Talk2Me.ChatRoom.getContentPane().add() instead
    at javax.swing.JFrame.createRootPaneException(JFrame.java:458)
    at javax.swing.JFrame.addImpl(JFrame.java:484)
    at java.awt.Container.add(Container.java:307)
    at Talk2Me.ChatRoom.colorBtnMouseClicked(ChatRoom.java:257)
    at Talk2Me.ChatRoom.access$100(ChatRoom.java:12)
    at Talk2Me.ChatRoom$2.mouseClicked(ChatRoom.java:190)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:208)
    at java.awt.Component.processMouseEvent(Component.java:5096)
    at java.awt.Component.processEvent(Component.java:4890)
    at java.awt.Container.processEvent(Container.java:1566)
    at java.awt.Component.dispatchEventImpl(Component.java:3598)
    at java.awt.Container.dispatchEventImpl(Container.java:1623)
    at java.awt.Component.dispatchEvent(Component.java:3439)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3450)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3174)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3095)
    at java.awt.Container.dispatchEventImpl(Container.java:1609)
    at java.awt.Window.dispatchEventImpl(Window.java:1585)
    at java.awt.Component.dispatchEvent(Component.java:3439)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)

  • Switching between panels

    Hi.
    I'am got five different GUI files.. where one of them is a controlling block. In that file, I opening an own class, where I have Jmenu's, and a container, with an JPanel inside.. So when I change between the files, I'am just removing the present JPanel from the container, and adding a new one.. The changing between the sites works well.. container.add(window).. But the problem is that the container totally is upsetting the layout.. It don't look the same as when I'am viewing on single file
    What can the reason be?.. Can I change between JPanel without having them in a container.. Would that work better? Any suggestions.?

    Thanks.
    I trying to use, its function.. It worked well, when I added to JPanels to the coontainer, with a "shift color underneath" thing..
    But when I tried to implement into my program.. where I have JMenu inserted into the JFrame, I got problems getting the actionlistener to react.. Can't get it to change between windows..
    Te JFrame are created in static main...

Maybe you are looking for