Java GUI application? - java

I'm building application in NetBeans 7.1, and I'm new in it. I have made one JFrame and two JPanel's, I want to switch between JPanel's on button click(I have buttons in those panels of course). The thing is I made those JFrame and JPanel's with NetBeans IDE and I don't have idea how to switch between JPanel's inside one JFrame. I tried with SetVisible(true); but that only opens new JFrame which I don't want.
Thank you all in advance.

If you want to swap between panels in the frame, then you probably want a JCardLayout.
You would set the layout manager to JCardLayout of the frame, and then add the two panels to the frame. Then, when the button is pressed, you can use 'first(), last(), next(), previous() or show()` to move between the panels.
If you want your flipper button to be visible all the time, use some other layout on the frame, and put the button to the frame (in whatever appropriate fashion), and then create another panel with a card layout, add the two panels with controls to the card layout panel, and then add the card layout panel to the frame.

Related

Putting JPanel on JFrame of any use?

I am working in the NetBeans. For that when I am working in the new JFrame form or the design view if I add the panel and then add the label and rest of the contents, it makes no difference if I don't add a panel and add the contents like JLabel, JButton, etc. it makes no difference.
Is there any reason why panel should be added to the frame? I tried to close the application when the panel was inserted and when it was not inserted on the frame, the application closes both the times. (When I press run and try to close the application, both times it closes.)
Then what is the use of putting JPanel on a JFrame?
When you add components to the frame the components are added to the content pane of the frame which by default is a JPanel. Read the section from the Swing tutorial on Using Top Level Containers for more information.
By creating a separate panel and adding components to that panel you give yourself more flexibility when designing your application. For example you may want to use a CardLayout which allows you to swap out different panels on the frame. The tutorial also has a section on using CardLayout.

How to create a rectangle with multiple images inside?

What is the best way to display let's say rectangle (3x5) with icons 20x20 px.? I want to change the image file of every pic icon later (= it's not just static pictures). I tried to make JFrame full of JPanels, but i was able to display only one panel at a time. I don't want to use GridLayout, because I need just small rectangle inside a frame. Any ideas how to do it? Couldn't find any tutorial or solution. I'm completely new to GUI developement. Thanks
You do want to use a GridLayout. Your problem is that the JFrame you put the icons into uses a BorderLayout by default (and really, you shouldn't change the layout of a top level component).
What this means is that, if you add multiple panels to the frame, without using one of the NORTH, EAST, SOUTH, WEST constraints, only one of the panels will be visible and take up all the space. If you use a GridLayout for that one panel you get, the icons will be stretched, because the panel receives all the space due to the frame's BorderLayout. An alternate layout that doesn't stretch its contents is FlowLayout, but the layout to use depends heavily on your context.
To display the icons, a JLabel is handy. Use an ImageIcon for the label's icon. You can later use setIcon() on the label to choose a new icon.
overall, my approach would be this:
use a JFrame which has a BorderLayout
to the frame, add a JPanel to the frame. The default layout is a FlowLayout, which will prevent the stretching
to the panel, add a JPanel with an appropriate GridLayout
to that panel, add the JLabels, each having an appropriate ImageIcon

JPanels to appear on every GUI i have created

How do i have the JPanels in the Mainframe to appear on all other GUI i've created?
Do i have to make the same JPanels in the Mainframe again or is there another way to make it as a fixed layout so i can only edit the content part?
You state in comment:
i'm creating an application with a fixed desgin. So i have everything fixed in the Mainframe (the navigaion bar), So how do i have the navigation bar to appear on other JPanels for other GUI? I only have one JFrame.
I wouldn't have the same navigation bar appear in multiple JPanes, but rather would place my navigationBar in the main JFrame's contentPane in a fixed position, say in the BorderLayout.PAGE_START spot, and then swap my JPanel "views" in the BorderLayout.CENTER position using a CardLayout. This way only one navigationBar need be made, and it would be visible all the time (unless you explicitly choose not to show it).

Clarify the difference and connection of canvas, frame, pane and panel

I'm quite confused and I when I try to find an answer with google I get bombarded with tutorials.
What is the purpose of each one?
How are they connected?
What is the purpose of the connection?
In java canvas is area used to draw something by java graphics. For ex. drawing an image or rectangle.
Frame is used as JFrame(swing), a top level container which can contain canvas, panels, pane(DesktopPane, ScrollPane) etc..
Panel or JPanel is a subcontainer used to contain textboxes, buttons, canvas etc.
Jframe can contain multiple panels, but panel can't contain JFrame.
Textboxes, buttons can directly be added to Jframe but it decreases flexibility, Suppose we want to hide a set of buttons from ui, then we need to hide them one by one from JFrame. If those text boxes are added to panel then we just need to hide that panel only. There are so many cases about using panel in jframe.

Sliding hidden sections of a JPanel into view

I want to display a JFrame ( made with the Netbeans GUI Editor ) that has an enclosed panel ( the panel encovers the entire JFrame ). The panel is twice as wide as the frame, so I want it so that when a button is pressed inside of the panel, the panel's visible area slides over ( over about 2 seconds) to the hidden area of the JPanel and the previously visible section of the JPanel becomes invisible. I couldn't find any function how to set the currently visible section of a JPanel, so the function and/or a different solution to this would be helpful.
I suggest that you put the JPanel in a JScrollPane, one that if you wish does not show its scrollbars. Then you could easily use the scrollpane's model and a Swing Timer to create an animation that shows the JPanel sliding.
The solutions is CardLayout based http://java-sl.com/tip_slider.html
You can add 2 (or more) panels into container and rotate them.

Categories

Resources