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

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.

Related

How to add a control panel on a JTable row on MouseOver (hover)

I have a non editable table, on which I'd like to display a row contextual panel (likely a JPanel). Somewhat like Gmail is doing: when moving the mouse over mail rows there's a simple tool bar showing up on that specific row.
Like in gmail the action of controls I'd like to display won't edit the values, instead they will use the value in the row to perform some offer work.
I have played with the following :
TableCellRenderer, the display mostly works, but it has limitations:
the component is only used for rendering, so one cannot use it to simply add multiple buttons
it requires another column
for the hovering behavior (ie display on row only when the mouse is hovering the row) it requires collaboration with the table's MouseListener
TableCellEditor, my table is not editable so the cell editor is neither called
it also requires a specific column
it also requires collaboration with the table's MouseListener
MouseMotionListener can be used to display a popup for certain coordinates
the popup feels like it's the right component for this
there's quite some code to handle the popup lifecycle (closing it when the mouse move out of the row, don't re-open a popup if there is already one open)
tool tips: as far as I am aware the swing tooltips do not allow to have control components like buttons, etc
I did related question and answer on stack overflow. But they all require to add a column to display and use these swing components.
Given that you have posted no code, this question is a bit broad.
Nevertheless, the way to do it would be to stick a JPanel in a JPopupMenu. You need to create a listener on your GUI to know when and where the JPopupMenu should appear
--- Edit ---
I think you have to add JMenus to a JPopupMenu, and what I suggested about adding a JPanel won't work cleanly. You can either use JPopupMenu, or use a JWindow and put your JPanel in that.

Hide/Open JTable by a press of a button in JScrollPane

I want to represent data split into groups (categories) and when pressing a button of a category I want JTable to show below category button. I've built JScrollPane on it is placed JPanel which is a container of costume made panels (BoxLayout is used for placement in the container).
Costume made panel (CMP) to show category with a function to hide or show JTable
Now the problem is when i press button to show or hide table function is executed in code of CMP but JScrollPane does not refresh and does not show the action. It refreshes when you click on scroll bar. What can i do about it? Is there any better way to build something like this?

multiple Jpanel with same functionality buttons

i want to create JFrame with multiple panel each panel displaying some information from database (say product_id and description ) and each frame have a button(add to cart) which will add the information from that panel to cart table in my database how will i implement this?
I would try to simply things by using a JTable to display such data, with each column displaying a database field. I'd add an extra column, say a Boolean column that displays as a check box, and then assess the state of that column when needing to decide which to add to a cart.
Edit You ask:
how will i know how much quantity of that product is selected,
Then add a quantity field as well, perhaps one that uses a JSpinner as its editor.
and also i am thinking to add image of that product also can i do it in Jtables
Absolutely. It knows how to display ImageIcons for instance. Please have a look at my answer to another question for an example.
You should create a JPanel (called PanelButton) which contents [Add to cart] button.
In other JPanels, you add PanelButton.
So, by this way, you can reuse your button without implement a lot of [Add to cart] button for each panel.

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

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.

Dynamic fields addition in java/swing form

I'm pretty new to java, and using netbeans for design a UI.
What I am trying to do is...
in the form. there are a jComboBox and a JTextField, where user can input the service he is choosing and an observation. So far so good. JComboBox is populated from database entries.
The problem is, a user can input N different services at once (there are too much to be a bunch of checkboxes). I was thinking into add a "[+]" button (along with a "[-]" for removal). Thus, users click on [+] and another new line with a jcombobox + jtextfield appear right below the previous ones.
I'm stucked at this point. On [+] button ActionPerformed I just can't clone and add previous nodes. Any idea on how proceed.
My background is webdev. Doing this with javascript would be really quick. Well, I think you already know what I'm trying to do. Waiting for some light. Thx.
You're on the right track. Here's some source code to give you some ideas
The basic idea is that the EntryList is responsible for keeping track of the rows to display; each row has a plus/minus button, and then delegates out the actual adding/removing to this EntryList. It also exposes methods to disable the minus/plus button so that the list view can ensure that you don't remove a single entry (so that you don't have an empty display)
This doesn't work perfectly; you'll notice you need to resize the frame to get the new rows to show up correctly. But this should be enough to get you started.
Create your main panel to use a layout manager that displays component horizontally. The Box class is easy to use for this. Then you just create a new panel with the components you want to display and add this panel to your main panel. Something like:
JComboBox checkBox = new JComboBox(...);
JTextField textField = new JTextField(...);
JPanel row = new JPanel();
row.add( comboBox );
row.add( textfield );
mainPanel.add( row );
mainPanel.revalidate();

Categories