Custom Java JTree implementation - java

There are 2 JTree: JTree1 and JTree2. Note that the nodes (country, city, colors, blue ...) all will be implemented as JCheckboxes so that user can select particular colors for each city or for the whole country by selecting their corresponding checkboxes.
Problem:
Q1. I want that each country or city can have its own colors selected. Means if a user wants city1.1 to have colors blue and violet and city2.1 to have colors red, then he first have to select the city1.1 checkbox and then select blue and violet, and after that when he selects city2.1, then the checkboxes blue and violet are deselected automatically so that user can select the colors for city2.1. But when the user selects the city1.1 again, then the JTree2should show the selected colors (bule and violet) for city1.1.
So for this purpose, Is the JTree (with its nodes as checkboxes) correct option to implement or I should use some other JComponent?
If JTree is a correct option, then how can I remember the colors of each city?

So for this purpose, Is the JTree
(with its nodes as checkboxes) correct
option to implement or I should use
some other JComponent?
Not exactly sure what you meant, but I, personally, would not use a JTree to present the options on the right hand side. I think it is much simpler to present a JPanel that contains the options in this particular case. Left side seems fine for your example, although I don't really know what sort of data is going into the tree.
If JTree is a correct option, then how
can I remember the colors of each
city?
Note, I'm going to make a couple of assumptions:
The left side that contains your countries and cities remains a JTree and the right hand side can still be a JTree or a JPanel.
You want the options to appear exactly as the user last set it before they select a different node on the left hand side.
The simplest way of achieving this is to add a TreeSelectionListener to the tree's (the one containing the countries and cities) selection model. The TreeSelectionListener is provided with a TreeSelectionEvent which provides the node that was selected and the node that will become selected. This will provide you with the opportunity to extract the colour settings that were set for the node that the selection is changing from to the one that the selection is changing to. The TreeSelectionListener should be added to the TreeSelectionModel that is obtained from the JTree, by calling its getSelectionModel method.
If you use this technique, when you to perform the operation with the last selected options, you'll need to get the options one more time before you perform the operation. For example, if you had a "Save" button, you should check extract the colour settings for which node is selected on the left. This is to capture any changes that the user may have made that the listener has not captured (since the listener is triggered only when the left hand selection changes).
If you need an example, I've written one at http://www.box.net/shared/hgbet4uf6k.

Related

Design a ContextMenu with a TreeView functionality

I want to design a ContextMenu with a TreeView functionality. I didnt found anything how can be done. I want to design a custom TableMenu (that button at the end of table header which can show hide the columns), using a treeView structure in ContextMenu, so the user can show/hide a group of columns, like 1-3, 4-6, and so on, or any kind of grouping, but still keeping the one by one show/hiding. The best way would be a tree structure, so if the user hides the parent all of its children are hidden and vice-versa.
Is there any possibility to adapt somehow the TreeView into ContextMenu?
I would like something like this with CheckBoxes for instance:
Each parent node would be an item that represents a group of columns, and each child would be a column.
P.S.
I know how to create and add a custom ContextMenu as tableMenu (I have got it from here : https://gist.github.com/Roland09/d92829cdf5e5fee6fee9) , I'm interested in how to set a tree structure to context menu instead of the list structure.
I also know that I can add the parent like a "simple" item to the context menu, then handle as a parent, but then I Have to implement every functionality of a TreeItem, but I prefer a much simpler way if it exists.

Vaadin - adding components as elements of another components

I've been using a ComboBox to store some values and make a selection from those values, but the problem is, ComboBox, as it is, only allows one selection at the time and I need multiple selections, ie checkboxes, but that cannot be done via Vaadin. I figured if I could present checkboxes as the elements of the ComboBox, that would solve the issue, except adding components to a component that is not a layout doesn't seem to be possible.
I've done this tutorial https://vaadin.com/docs/-/part/framework/components/components-customcomponent.html
Basically it combines two Vaadin components into one panel and displays them together, but that's not what I need, as I need certain components to be placed inside a parent component.
So what are my options if I'm to do this?
This is not an answer to the question that you are asking (component within a component), but rather the underlying problem that you present. In other words, I believe your question is an example of an XY problem.
I think you want to use a Grid with multi-select turned on. In this mode, check boxes are automatically added to each row and there is a checkbox in the header to allow toggling all on/off, ability to filter, ability to sort columns, etc. See the documentation for more details.

Showing a counter dynamically for JList items highlighted

I have a program that I created for work. This program takes an uploaded file, reads it, and puts the data into a JList in the GUI. The GUI actually has two lists and the user is able to move items between the left and right list by highlighting them like usual with a JList and then hitting an arrow to move the items. The lists are multiple-interval selection.
One small addition I would like to add is some type of counter that shows the user how many items they have selected before they actually move them between lists. This would need to be dynamic so if the user holds control down and begins clicking the counter will continue to update the number of highlighted items.
As the lists are often quite large and a user might need to move an odd number of transactions between the lists (Think 300 transactions in left list and the user needs to move exactly 50) it would be beneficial to have this counter.
Can anyone think of how this could be done? I'm not sure how to add an action listener to just clicking on the items. Please also let me know if I need to elaborate any more.
Generally my question is can I create an action listener just for when a user clicks a item in a JList that updates a counter for the current selected indices? Also it would need to change when they no longer have selected an indice.
Register a ListSelectionListener with your JList.
The listener could simply query how many rows are selected and update the number in the panel to that. Perhaps use getSelectedValues().size().
http://docs.oracle.com/javase/8/docs/api/javax/swing/JList.html#addListSelectionListener-javax.swing.event.ListSelectionListener-

Changing the priority of children in a Composite

I have a Composite called display that has children elements placed in an absolute layout. Throughout my program, these children can move around on the composite and overlap. I want to create a "priority" so I can choose which children are drawn on top of other elements. The children are a custom class named Llama I made that extends Canvas. I have an integer field that stores their priority, but I don't know how to make them display in the order I want them to display (based on the integer field). Here's an image of the program displaying what I mean by overlap.
Basically, they automatically choose an order of display based on the order they were added to the composite (as far as I can tell). How can I change that order to fit my priority field?
Thanks in advance!
You need Control.moveAbove() and moveBelow().

Filtering a ListView in wicket using 2 drop down boxes

I am trying to filter a list that is placed into a listview through the use of 2 drop down boxes.
The first dropdown box is titled price and the second is owner.
I want to be able to select a value in one or more of these drop down boxes and then have the List view re-render with the filtered results.
The trouble is I do not know how to begin this task, would someone be so kind as to enlighten me :D
Thanks in advance!
Your best starting point is probably this example: (Source code also available on this page, ChoicePage.java is the name)
First of all, you have to use a dynamic model in your ListView that generates the list of items depending on what you had selected in the dropdown boxes.
Then the basic idea is that you add an AjaxFormComponentUpdatingBehavior to the components that control the updates (your two dropdown boxes in your case), and in the onUpdate() method of this behaviour you should add the component you want to update to that AjaxRequestTarget passed.

Categories