I manage a list of plain object that I use to build and display JPanels showing information contained in those objects. One JPanel for each object in the list. Every seconds that list of object is updated with a new list. From this new list I'm checking if objects have been removed (not in the new list), or added, then update JPanels accordingly. At the moment what I'm doing is if at least one object has been removed or added from the list I delete all JPanels and add them all again. If not objects have been removed or added in the list I just update information on existing JPanels. I'm using GridBagLayout for that. I don't find this solution good enough and what I would like is removing/adding only JPanels corresponding to objects removed or added. Any suggestion on Layout to use or more optimized way of doing the work is welcomed, thanks.
Related
Sample layout
I am attempting to build a GUI with JPanels which can display multiple projects, with multiple tasks displayed inside each table (see image). My team is using a database to hold all the project/task data. The project panels are all within a scroll frame, and each project panel can scroll as well.
I am trying to learn how to display the same number of projects in my frame that are in my database. There are not fixed numbers of projects or tasks, so I want to add a panel when it corresponds to an entry in the database. How can I only create the number of panels necessary, making the page dynamic?
Any information that can point out what I should be studying to accomplish this would be appreciated.
Based on what you're trying to do, the feat itself would not require an incredibly complex solution.
Ideally, you should create a structure or an object which contains all relevant information for your specific JPanel. From this, the objects can be stored within an ArrayList.
Looping for the number of objects present within the ArrayList, you can then simply create a new instance of your required JPanel using the information from within said object.
Hope this helps!
I had some issues with a List concept in Codename one. I need a list of items populate dynamically at run time, but I don't know how doing it, so anyone helps me regarding this issue?
Depending on how your list is constructed... E.g. when you have a form with a container, which includes your list as labels.
Container c = new Container(BoxLayout.y());
//adding strings as labels
c.add("String1").add("String2");
Then you can later on add a String to this container
c.add("new String");
But the form won't update itself. So you'd have to
form.revalidate() OR form.animateLayout(150)
to be able to show the changes on the screen.
Program Outline:
I plan to make a simple Java program that will load Vehicle objects (Vehicle being the superclass, EnginedVehicle and GoodsVehicle being the subclasses) from an XML file into an ArrayList which will then be displayed on a JList. The user will be able to show/hide the different Vehicle types using check boxes, add a new vehicle type or press the selected item in the JList and edit or delete it. The program will then put the Objects back into the ArrayList where it can be then saved back to the XML file.
Question: So, I am completely fine with the loading of the XML file into the ArrayList and putting that object onto the JList but the thing that is hurting my head is thinking about how I am going to:
What is the best way of getting the object back from the JList ready for it to be modified or deleted and put back into the ArrayList?
How would I show/hide the different types of Vehicles in the JList using the check boxes?
I understand this may seem a lot but, this is my first post and I am new to the community and I have fairly good knowledge of Java and OOP programming but I have just finished writing a fairly big website and going back to Java is a headache.
Since your ArrayList should be equal in size (item count) to your JList, your JList will have the index you're interested based on selection. Regardless if you want to modify or delete the item, store what index it was at and remove the item from the JList (You should be using a DefaultListModel). Use this index value to get the object from your ArrayList. If you're modifying, modify your object as needed, you shouldn't have to remove the object from the ArrayList for modifications, and place it back into your DefaultListModel. If it's a delete, then just remove the object from your ArrayList using the index value you stored.
As far as displaying (show/hide), clear your DefaultListModel (which will clear your JList), iterate through your ArrayList and add the items to the DefaultListModel that match your checkbox selection criteria.
EDIT:
I didn't take into consideration of possibly modifying/deleting items when items are hidden. For this, may want your objects to have a field that stores what index they are at in the ArrayList. This way when you do your filter, I would copy the items from your "Master" ArrayList into a sub list that you can populate your DefaultListModel. Then you apply the same logic to this sublist when selecting an item from you JList, then take your changes from your sublist and apply them to the "Master" ArrayList.
Keep in mind that when you remove an item, you'll have to reassign all items index location from that point on down.
I'm sure there is probably a cleaner way of doing this, but this is what first comes to mind for me.
I don't know if I'm horribly mistaken, but why change to a JList at all? Do you use your JList as a parameter to visualize the information in it? If yes, why dont you use your ArrayList instead? Then the checkboxes only change the visibility ot the Items of the List. So you dont have to care about indices, because they stay the same. And new entries, can be made as well... maybe im wrong but i guess you got kind of a GUI for the user to browse/alter/add new vehicles?
I am working on an assignment which requires the use of JList.
Basically it is an ordering shopping cart. I have 2 lists: one which has all of the items available to be purchased, and a second which holds the items that are "added" to the cart. It all works pretty well. However, I am having a tough time figuring out how to display the empty cart list when there aren't any items in it yet. I have an empty Panel until it's filled.
How can I make it visible all the time?
I've been looking for a solution for the problem your asking, coz i need the same feature.
I sought tutorials and it pointed me here. Basically you just add your list to the viewport of a JScrollPane. It works for me. Try to test and run the code shown on my given link.
Cheers!
You just need to set the default sizes and visible row count.
JList jlist = new JList(model);
jlist.setVisibleRowCount(10);
jlist.setFixedCellHeight(15);
jlist.setFixedCellWidth(100);
I'm trying to create list that shows contacts, each list item shows the name on a line, and the phone number on a second line, and maybe an image or icon. I was thinking of using two labels for that, but i can figure out how to use a custom list model to implement this.
My first attempt was to add a Panel object that contained the info i wanted in the list, then add it to an instance of the defualt list model, but that only turned up the class name in the list.
DefaultListModel Clistmodel = new DefaultListModel();//
Clistmodel.addElement(Contact);//Contact is an JPanel object
GroupList.setModel(Clistmodel);//GroupList is the List object
this didn't work out at all then i learnt that the default list model only knows how to render strings i think, so i have to create a custom list model, or a custom ListCellRenderer, i don't really know which will solve the problem.
Your question asks how to create a custom list model, however, that's not what you need (I don't think) as a DefaultListModel will work nicely for you. Rather you will need to work on the renderer. You need to create a non-GUI class to hold your information that each item will display, probably your Contact class, and then create a JList that holds this in its DefaultListModel.
The key for you will be to then create a custom list cell renderer to display the information on multiple lines -- perhaps a JTextArea, or a JPanel that holds two JLabels in a GridLayout. Please understand that the renderer does not display the actual underlying components, but something more akin to a stamped image of whatever components you're trying to display, so it will not have the full behaviors available to it as the actual component would. It will take work, but the writing a renderer section of the tutorial linked to by user714965 will show you how to do this.
Please give it a try, and then if you still are stuck, come on back with your code, your errors, and your questions, and we'll be better able to give you specific help.