Java Checkbox List in Netbeans - java

I have to design an application which handles customer orders. The GUI has tabs, one for the customer and one for the admin. In the customer tab, he has the possibility of submitting an order, by chosing the products from the product list, each having a checkbox in front of them and a text field in which the quantity will be specified.
I am using the netbeans gui editor for the design and I am a bit stuck as the code cannot be modified. I cannot create the product list dynamically (so to create a line for each product in the product array list, and on each line to put the checkbox and the textfield) or at least I don't know how to.
My question is - is there any way of dynamically creating such a list (checkbox + label with the product name + textfield which waits for the quantity) or is there an alternative to my idea ?

Sounds like you need to use a JTable for your list of Products and Quantities. In the NetBeans GUI editor you will only be able to place the JTable on the panel you are designing. After that you need to define a 'Model' for your table which will hold the data you are entering. See the official Oracle Java tutorial on How to Use Tables.
Of course there are lots of other use cases that would fit your requirements, but your design sounds fine. The checkbox is probably superfluous though, as entering a quantity against a product should be enough to indicate that the customer has chosen that Product. If your list of Products gets too long, though, you might want to re-visit this design. Perhaps the table could have 2 coulmns, the first one being a combo-box list of Products, and the second column has the quantity.

Related

How can I add a List to the JLabel?

I'm writing Java game right now, I have a problem that I have a List which type is Ranking, with the fields name and score, and I would like to add that fields to the JLabel. Method setText() unfortunately doesn't fix. What should I do?
with the fields name and score
A JLabel is not designed to display multiple lines of text.
You can format the text using HTML.
Or I would suggest a better approach is to use a JTable, which is designed to display data in a row/column format.
Read the section from the Swing tutorial on How to Use Tables for more information and working examples.
I have a List which type is Ranking
You may also want to check out Row Table Model for an example of a custom TableModel show how you can display your Ranking object in a table.

How to organize communinaction between 3 list view adapters

I have 3 list views and 3 adapters correspondingly. Then class Menu, that contains ArrayList of Products, where Product is also a class.
1st adapter contains ArrayList and display all menus in ListView1.
2nd must contain and display all Products from Menu, selected in 1st adapter.
3rd must contain all available products, that are not in 2nd ListView.
The question is, how organize architecture in such way, that selecting one item from first ListView affect 2nd and 3rd Adapters. I mean do I need to create a new class, that will hold all changes, or it is better to make ArrayLists static and call their updates from OnClickListeners?
I would think that the 2nd and 3rd views wouldn't even be displayed until a selection was made on 1st view, no?
Regardless, if 2 and 3 are exclusionary drawing from the same list of products, it sounds like you should be able to build them using the same underlying data source. How are you populating these adapters? A content provider sounds like it would suite this well if you have a large enough product list.
In terms of behaviour, when a selection is made on the 1st view, that should cause a refresh of your 2nd and 3rd views on the newly changed parameters. So if in the 1st view the user has selected a number of menus which contain products, the 2nd view should do a query of some kind using the selected menus as a key for which products to then display. The 3rd view would just be what's left over, or use the selected menu as sort of a reserve key, i.e. only get products not in said menus, which would be a minor change to the query.

Custom component for JList instead of just strings

I've been trying to freshen up on my Java knowledge and I've been building a small GUI program and I've been running into a bit of a problem.
Basically, I have a JList which I'm currently populating with strings from an object from one of my classes which implement AbstractListModel which we can call my ItemList class. It contains an ArrayList of objects of the type Item which implements Serializable.
But, what I'd like to do is rather than populate my JList with a bunch of strings I'd like to populate it with some kind of string + JTextField combination so I can see one property of each Item object while also being able to update another property by changing the JTextField.
Now, what I'm looking for is the simplest possible way of doing this, and I am assuming there is a (relatively) simple way to do this since it's such a common thing to want to do in a GUI application (although I wouldn't put it past Java and Swing to make it convoluted and complicated).
So, what is the correct way of doing this?
No need to ever use String objects. Instead:
Put Item objects in the JList.
Add a ListCellRenderer to the list, to show the Item object the most user friendly way.
When the user selects an item, show the details in a different place (I'm thinking a panel with 2 columns of labels and text fields, and two rows - one for each attribute, and perhaps a button to Save)
The edit controls would best be encapsulated in a panel that can then hidden when not required, & put in a variety of places, e.g.
Below the list
In the main part of the GUI
displayed in a JOptionPane or a (modal or not) JDialog
Here is an example of placing the 'view/edit panel' (the file details) below the selection component (the table).

Binding database column to java jComboBox and JList

Ok, say I have a database table with two columns - one "Name", the other "Age", and there are over 40 names and their respective ages in the table. I want these names to be listed out in a jList/jComboBox, and also I want to be able to click on a name in the jList/jComboBox and have its respective age appear in - say - a text box. Do I have to go about this by simply writing a code that selects all the names from the table and populates the jList/jComboBox and then another code that takes the selected name, puts it in an sql statement, finds the matching age and sends it to a text box, OR is there some kind of a VB-esque column-to-comboBox/List-binding that I can utilise to go about this?
For only 40 name-age combinations, I would just query the database once, and store this information in a Map. Then you can just query the map when a name is selected, and update the age textfield. This will go a lot faster then running SQL queries each time the selection has been changed.
You have to set Model for your swing elements and for updating data based on changes at one place to other implement Listeners.
Have a look at this
Binding comboboxes in swing
Create a custom object that stores both the name and age values and add this object to the combobox. Then when you select an item you have access to both values.
For example: How to use Map element as text of a JComboBox

Help with Java program (Swing + database)

Could somebody give me a suggestion on how to simply and effectively make GUI representation of directories which are contained inside the database. Now, getting information using SQL queries is one thing. I can do that.
In fact using separate small examples I can put a file inside the database along with his information and I can get the file out of the database. The thing is I was just doing this without GUI, just to test does it work.
Now I need a GUI of this and I really don't know where to start. DO I use JTable, JList or something third? Also, I think I need an multidimensional array because I have, for example, id of a file, name_of_file and size.
So I need different types to put them in: int, String and int.
Also, I need to obviously hide the id of a file from the user yet keep it at the same time in order to be able to reference it.
How do I hide it in a GUI component?
So, let's say that I have a database table for files with these columns:
id, name, size, binary_of_file.
My real table has a bit more of columns like, id of a parent directory, id of a owner, etc. but for now this is not important.
So, I tell database to give me all info about the file (except it's binary because I just want to list the files):
...
ResultSet rs = statementObject.executeQuery("SELECT id, name, size FROM Files;");
while(rs.next()){
//Where do I store the values in? Which GUI component and how?
...
I guess I need an JPanel that will contain this component that will show my files from the database. What component? Please help!
It sounds like you want to use a JTable, it has a TableModel interface that you can implement to adapt to your resultset.
Also follow the link at the top of the docs. to Creating a Table Model.
Perhaps a combined JList/JTable component would fit this need.
That is a screen shot of the GUI of FileBro.
My idea is that the JTree on the left would represent the 'directories' and table (names) of the DB. The JTable on the right would contain the data of the selected table. Change the Locate Open Edit Print buttons for Create Update Delete and the panel below that to show details of records, and it would be the start of a DB CRUD component.

Categories