multiple Jpanel with same functionality buttons - java

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.

Related

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.

Animated table of strings

I'm trying to write a scorekeeping app in Java that allows a server application to send scores to client applications. The clients will then display a list of teams, team IDs and their scores, sorted by score. Of course I could just use a swing JTable to display everything, but I want to achieve a unique effect: I want the text dynamically reorder itself every time a team moves up in the list. That is, it want to animate the text around the screen. I would also need to be able to update the contents of the labels after being added. What would be the best way to achieve this? Is there any component that allows you to add other components to it and place/move them freely?
JTable is a JComponent so you can set desired LayoutManager and add JLabels above the JTable. Then move/reorder them to achieve desired effect. See SwingWorker as well.
You could use a JTable and change the contents of the rows as teams move up. Or you could arrange a series of labels and change the text whenever you want. To change the value displayed for a JLabel you simply use the JLabel.setText("new value"); method.
I've never done this but I think you need to use a panel with a 'null' layout manager. Meaning you are responsible for absolutely positioning your elements
http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html
You would need some kind of SwingWorker or Timer running to update the gui layout. Make sure you actually make the GUI changes on the EventThread, which you can do by using SwingUtilities.invokeLater();
As alternatives to a null layout, consider letting the layout work for you using
one of the marquee effects discussed here, or
shuffling labels in a suitable layout, shown here and here.

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();

Clearing a JScrollPane but can't then 'attach' tables

I am creating an application where I have 3 Panes with buttons which dynamically create buttons in the next pane depending on the selection, clicking the last button shows a table of data brought up via an SQL query:
[buttonPane1][buttonPane2][buttonPane3][table]
If a user has clicked a button on all 3 panes and then wants to change their choice on buttonpanel1, it will bring up the choices in buttonpanel2 and using
buttonPanel3.removeAll();
buttonPanel3.repaint();
I can clear the third button panel, my problem is how to clear the table. I want to remove it from the Table ScrollPanel however if I try
tableScrollPanel.removeAll();
it just means that the table never shows.
How can I remove any current table but allow a table to be 'reattached' I am doing this to create and 'attach' the table
jTableTemp.setModel(new DefaultTableModel(
tableContent, tableTitles));
tableScrollPanel.setViewportView(jTableTemp);
Thanks Very Much
Try setting the table model to a DefaultTableModel with empty data and the original headers, then repaint. As long as you have a JScollPane wrapped around the JTable, the headers should show up, assuming this is what you want.
Other wise, you can set the viewport to a new instance of a JTable with new headers.

Interactive JTable

I would like to create an interactive JTable. For this, I would like to add JPanels in the cells of the table. Once the JPanels are in the cells, I can add my various components to the JPanels thus making the table interactive. Each JPanel could have different components. Would it be possible to accomplish this and only have to create 1 table cell editor and 1 table cell tenderer. Does anyone know of a better way to do it?
Thanks
EDIT: Thanks for the responses. I actually already have a framework I am using. I just needed a JTable that users could drag and drop images in, play movies, display graphs, etc... I already have the functionality to do those things, I just needed a JPanel to add them too. I wanted it to be displayed in a JTable so the cells could be sorted, moved, add/delete rows/col, and well structured. I couldn't get it to work using the JTable, so I went ahead an created my own. Its just a JPanel that contains smaller JPanels (the table cells) using the GridLayout. It works well enough for my puposes. Just a pain to rewrite all of the functionality from scratch that a table has.
This is hard. JTable actually uses the cell renderers only for painting the cell content. I would recommend to check if a gridlayout packaged into a scrollpane would be the easier solution.
It sounds like you're trying to use JTable as a docking framework. Assuming this is the case you're better off using something like MyDoggy or JDock which allow you to decompose your GUI into multiple split pane areas.
JSplitPane may be an alternative in this context: one pane would hold the JTable, while the other displays expanded details of the selected row. A compete example using GridLayout is shown here.

Categories