Trying to to give different "screens" in jpanel when changing comboBox - java

So im trying to creat a Jpanel with a comboBox at the top, with 2 options, when choose options you get different "screens" in the jpanel but the comboBox stays, i know how to create comboBox and ActionLister but my question is how do i change the screens? do put different panels in the jframe? any ideas?

im trying to create JPanel with a comboBox at the top, with 2 options, when choose options you get different "screens" in the jpanel but the comboBox stays
Read the section from the Swing tutorial on How to Use Card Layout for a working example that does exactly what you want.

Related

Building a JFrame that run in the same order in Java

I'm making a JFrame that contains a lot of JButtons and JTextfields which contain data from a database. In the design everything is okay, but when I run my program the JButton and the JTextfield change their places and I don't know why.
Here is a screen shot from the design window and the run window:
Seems like you have aligned the textfields to each other and the buttons too. But you should have aligned one button to one textfield. One way to do this would be to use a GridBagLayout instead of the FreeDesign option in NetBeans. To do this right-click onto your frame and select Set Layout>Grid Bag Layout.
You can then right-click again and select Customize Layout... in order to place your components as you wish.

JPanels one above the other

I want to design a jFrame where there are three jButtons and a set of three jpanels one above the other. When we call a jPanel from the respective jButton , that pane will be displayed. It would appear to the user as if the same portion of the jFrame is displaying the content to be shown on clicking each jButton.But when i am trying to set the jPanels one above the other, they are being shown side by side thus elongating the jFrame horizontally. What should i do to put one jPanel over the other? Any other idea than jPanel which should do the work i intend to do would also be help !!
Your behavior sounds like you are using a FlowLayout. This will not "layer" anything. Instead us a CardLayout, which does exactly what you are trying to accomplish. You call method like show, next, and previous to navigate the panels. See How to Use CardLayout for more details.
Also there are probably hundreds of other examples here on so. Go through the cardlayout questions.
[Tip: navigate the different tabs like "votes" and "frequent" to filter to some of the better posts]
Here's one that uses the show() method to switch between two panels by name.

Adding content to a JPanel

I'm new to Java and actually designing the GUI for an application.
My main is a JFrame with 5 buttons and 1 panel which will have the "content", for the first button for example, I've designed a Jframe which has a JTabbedPane.
Now I would like to know how can I incorporate the content from that frame to the "content" panel when clicking on the button ?
I tried to use .add but I get:
java.lang.IllegalArgumentException: adding a window to a container
(seems we can't add Jframe to Jpanel).
I also tried the setVisible way but it doesn't meet what I need since it will hide the panel completely and I will get a tiny window with the buttons.
![Jframe content][1]
![Main Jframe with buttons and Jpanel to show the jframe content][2]
The code is generated by netbeans, and I forgot to mention that I did research on adding a Jframe into another Jframe but here isn't my problem at all.
I tried by changing the Jframe by JInternalFrame but clicking on button doesn't do anything.
Button has
contentPanel.add(new GestionUtilisateur());
So basically when you click on the "Gestion Utilisateur" button for example, you get that JTabbedPane that has to appear in the content area (which is blank here)
You should not be putting JFrames inside JPanels. If you have multiple panels you would like to display, depending on something like a button, look in to LAYOUTS.
In particular, it sounds like a CardLayout would work well for your needs. CardLayouts allow you to swap which panel is displayed in a frame by bringing it to the "front" of a list of panels. This would let you display your JTabbedPane on one button click, then click another to change the content pane.
JFrame can not be added in a JPanel.
use JInternalFrame
Make and hold references to JPanels containing your content. A JFrame is really just that, it's a frame (though you can add a single component to it).
You can't add a JFrame to a JPanel. If you want multiple components to be visible use layouts such as BorderLayout, GridBag, etc. Check out some of the Swing layout tutorials here.
Content should be designed as JPanel (you can design it with drag&drop just like JFrame) but if you really have to put a JFrame to JPanel for some reason, you can do it by
myJPanel.add(myJFrame.getContentPane());
however i would suggest modification of your program.

JComboBox with 4 cols and 4 rows data[][] form

In my Java Swing app, I need to have a JComboBox with four columns and four rows. Basically, it's a combo box to select color. When the combo box is collapsed, the user can see the first row's four columns, and when it's expanded, the user can see sixteen colors in 4x4 form.
I am not sure how to design such a combo box layout. I can use a renderer, but I'm not sure about how to implement it. I can only see two possibilities:
Create a JLabel named "ColorLabel" that draws a circle and fills the color of the circle as specified in its constructor.
Create a panel "ColorRowPanel" and add four ColorLabels in it. Create four objects of ColorRowPanel and set that as the model to the combo box.
Create a JTable of 4x4 and add ColorLabel in each cell. Set the JTable as the model of the JComboBox.
Is any of this possible? If so, in what object do I add my ColorRowPanels/JTable to set as the model? And when a color is selected, I should be able to know which colorLabel or which row-col is selected to get the color selected by user.
UPDATE
Thanks. As per your guidances, I started up.
1) Created a class ColorButton of type JToggleButton - just to create a shape for the button. Nothing more is implemented. Code taken from here
2) Created a class ColorContainer that extends AbstractColorChooserPanel. Created a Panel of 4 btns and added 4 such panels to the class. Also implemented ButtonGroup to each button of the panels. Designed as shown in CrayonPanel - added my initComponents() in buildChooser(). Constructor is empty. Rest all abstract methods are empty as per now.
3) Added panel to JCC and using JDialog.createDialog could show the ColorContainer. It comes properly fine, just I don't want the Ok, Cancel, Reset btns. I didn't find any option in JCC class or on net to hide the buttons panel of JCC dlg.
4) To try the same with Panel, I made ColorContainer extend JPanel instead of AbsColorChooserPanel and show the ColorContainer in Popup and it shows perfectly well.
I can't get way for 2 things yet -
1) MAIN - I want results like this :
1st part shows when the dropdown is collapsed & 2nd when it is shown. You see the 1st panel is still visible regardless of drop down been shown or collapsed.
What I got till now is : I added a JLAbel and on mouseEnter event I show the popup.
How do I achieve the results as I want to ? Can't think of a way to implement this.
2) Is it possible to hide the btns of JColorChooser and get the results as I am expecting !!
Please guide me and help me.
You cannot use combobox here. Combobox is designed to have 1 element in each popup row.
You should create a Panel with 4 buttons for the selected row and additional button to open the popup with 12 another buttons. To create the popup see the class javax.swing.Popup.
Here is the step-by-step guide:
Create panel with 4 color toggle Buttons and the button to open the
popup.
Create Panel with 12 another color buttons.
Add all color buttons to a ButtonGroup
Make trigger to open/close popup and add it to the button to open
the popup
Make trigger to close the popup if one of the color buttons is
switched and add it to all color buttons.

Java Swing - set panel

I'm quite new to Swing, and I'm programming an application with NetBeans' UI designer.
Now I have an JPanel called "editorPanel", and it must be able to display multiple things. (so, sometimes it has to display an image, and sometimes it has to display a text editor)
I have made separate panels for this, so say I'd have a JPanel called ImagePanel and one called TextPanel. It has to switch easily between them, so I tried this:
editorPanel = new ImagePanel();
But that didn't work.
So, what I want to do, is set an empty panel to a defined panel.
How can I make this work?
The proper way to achieve your goal is to using a card layout and switching panels accordingly.
You ca get some idea on how card layout stuff is working in here

Categories