Multi-leveled (nested?) Jlist in Java - java

Is there a way to create a JList that has more than one (I'm aiming at three) levels?
Something like this:
level 1 item
level 2 item
level 2 item
level 3 item
level 1 item
level 1 item
level 2 item
level 2 item
level 3 item
level 3 item
I have (up to)three-level-component GUI in my program, and I would need to somehow enable user to organise the elements of the GUI, to move them above or under each other.
Can it be done with JList, or is there another way of dealing with such things?
Maybe some library?

I think you could, yes, but you're in for a world of hurt that way. JList naturally represents a List from a conceptual point of view, not a tree, meaning most of the ordering logic would have to be done by you. What you're probably interested in is a JTree instead.

I think you should use JTree for that.

You can implement your own ListCellRenderer and your own ListModel.
http://docs.oracle.com/javase/6/docs/api/javax/swing/JList.html

Related

how to move objects with JAVA JFrame?

I try to develop a java project for kids to develop myself. I'm not expecting a code, just examples or documentations about it.
The project, takes two numbers and two different kind of fruits from combobox and it displays thoso fruits with matching via line.
For example, the kid select 2 and 3 as a number and apple and orange as a fruit. The program shows 2 apples,there will be line among 2 apples and 3 oranges and there will be line among 3 oranges. And the kid(user) can move the fruits via drag and drop.
How can i do that? I dont know even keywords to search on anywhere.Which libraries or methods i have to learn ?
Sorry for bad language
Thanks
the kid select 2 and 3 as a number
two JSpinners for numbers with one SpinnerNumberModel (you can to reduce range)
or two JComboBoxes with integers in the model as the Items (you can to reduce range)
apple and orange as a fruit.
two JComboBoxes with Icon/ImageIcon in the model (see Oracle tutorial for working code example)
or two JLists with Icon/ImageIcon in the model (use the same methods as for JComboBox, Items in JComboBox are in JList)
can move the fruits via drag and drop.
use JList for this idea
put generated fruits as Item to the model for the another JList (horizontal wrap), for DnD to read Oracle tutorial for working code example about DnD between two JList
How can i do that? I dont know even keywords to search on
anywhere.Which libraries or methods i have to learn ?
post an SSCCE, short runnable, compilable in the case that you will have any issue(s) with a.m. points, demostrated your progress
You can use javaFX. It is much more easier to use than swing and have a modern appearance. There is also a scene builder for javaFX. You can directly place combo boxes or lines via this scene builder.

Drag-and-drop to order items in Swing

I have a list of items to display in Swing. For simplicity, imagine that each item consists of only a name. I want the user to be able to order these items by dragging and dropping them above/below each other. What's the best way to achieve this?
Alternatively, could this perhaps be accomplished in using a JList, with an "up" and "down" button that would move the selected item up/down in the list. This would require updating the graphical display instantly on each click (I don't know how to do this), and saving the new order by getting the items in the list in their current order (which I also don't know how to do).
Or might a drag-and-drop solution be more feasible?
It'd probably be easier to implement this with the JList solution you mention, so I'll give you some pointers on that (I'm not very experienced with D&D).
Basically, you want to have three components: a JList and two (one up, one down) JButtons. You'll also probably want a custom list model. If you're unfamiliar with models or list models, check out this tutorial. Otherwise, read on.
In the list model class (e.g., ReorderableListModel), go ahead and define two methods: public void moveUp(int index) and public void moveDown(int index).
The code for moveUp is as follows:
if (index > 0) { // error checking
// Swap the given index with the previous index.
// (where `list` is the name of your list variable)
Collections.swap(list, index, index - 1);
}
// Finally, notify the `JList` that the list structure has changed.
fireContentsChanged(this, index - 1, index);
moveDown is similar:
if (index < getSize() - 1) {
Collections.swap(list, index, index + 1);
}
fireContentsChanged(this, index, index + 1);
Now, we need to implement the action listeners for the buttons. For the up button, go try this listener code:
// First, move the item up in the list.
listModel.moveUp(list.getSelectedIndex());
// Now, set the selection index to keep the same item selected.
//
// If you use the default list selection interval, setting the index to -1
// will do nothing (so it's okay, we don't need error checking here).
list.setSelectedIndex(list.getSelectedIndex() - 1);
Add a similar "move down" method and you're done!
With respect to "updating the graphical display instantly on each click," that's what the fireContentsChanged method in the model class does. JList will do the updating for you.
It is a functionality hard to achieve using just swing. Have you heard about JavaFX? It is a great graphic framework to work with if you want to achieve more dynamic functionality in desktop applications, take a look to this article: http://docs.oracle.com/javase/tutorial/uiswing/dnd/index.html
Here you will be able to find links with more information, as well as some examples. Best regards.

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).

GWT RadioButton Grouping

In my application, I have URN-identified data coming in from the server. I'm in the process of abstracting as far as possible so there is very little to no logical code in my views, and I'm using a generic presenter that wraps those views. All widgets have URNs, making it super easy to map incoming data to a specific widget (until now, a 1 to 1 relationship). This has worked well for pretty much every widget, and now I've reached a point where I'm tripped up.
Assume I have (just for simplicity's sake) two RadioButton elements on a view. These buttons belong to a "group" (just by setting their name values to the same thing), but obviously they're 2 distinct elements. I can't map my URN-identified data to a single widget as in every other case because, in this case, it is two widgets.
Here's an example of what I mean:
Utility Company is a ListBox, so just one widget there. I map each item in the list to a specific Enum value.
Utility Rate is a TextBox, so again just one widget to map.
For Energy Usage, they can select to use either an average for the year or input 12 monthly values. I'm stuck here. I can't map to just one of the RadioButton elements, because then I'd need some extra logic in the view to handle the behavior appropriately.
Am I stuck mapping to just one widget and sticking (unwanted) logic in my view to determine what the state of all of the elements should be based on the value that came in for the one widget that is mapped?
How should I handle this case?
Edit (Solution):
Following the concepts of jusio's answer, I came up with a workable solution. Because I didn't want to go sticking special case handling through my logic to take care of a non-widget, I created a RadioButtonSet faux widget (public class RadioButtonSet <T extends Enum<?> & HasDisplayText> extends Widget implements HasValueChangeHandlers<T>, HasValue<T>), into which I manually pass the radios I intend to group. Having done that, I can get or set its value and have it fire appropriate events when the user changes the selection. Then mapping the collection of radios is no different than doing so for a listbox. Thanks jusio.
I believe in your case you shouldn't treat radio buttons as two separate widgets, basically in your case you can treat the radio button group as combo box, because behavior is almost the same (the only problem is that you have additional master detail). So basically what you will have to do is to wrap real BO objects into some kind of RadioButtonGroupModel, and give it to view, view can take this model and generate radio buttons (with some editors or whatever else). I remember running into this problem when i was extending databinding FW for JFace, and this was the best way I could find to solve this problem.
If I understood correctly the problem, there are 2 possible solutions:
Give each RadioButton a unique URN (ex: oldURN_1 , oldURN_2)
When you send data for a URN, disable the other one
Keep the same Name for each RadioButton but add a number variable in the data the server sends indicating which radioButton it is supposed to use (ex: 0 for Average and 1 for Monthly)

Two DropTarget in the same class does not work (Java) ?

I have two Jlist in the same class each with DnD drop enable.
The problem is whatever file I DnD from my desktop to the JLists either number of or number 2 will populate ( and appear ) in JList1. Jlist2 accepts the drag and drop but it is like the content is going to Jlist1 automatically.
Any idea on how to solve that ?
Maybe by cleaning your code ;-)
Take a look at the DropTargetListener you created to enable DnD operations. i think that either it
adds elements to list 1 datamodel wherever you drop them
is the same instance for list 1 and list 2, but configured to drop in list 1

Categories