Extending JFX ComboBox UI for MultipleSelectionModel - java

I want to implement a MultipleSelectionModel ComboBox for Open Java FX 8, which would include all of the functionalities of the ControlFX CheckComboBox (as seen in this post) plus other supplementary functions that are out of the scope of this question.
My issue is that I have been reading through the JavaFX source for hours now and I have yet to find in which method 1. the ComboBox creates the list of element to print (using ListView and other JFX UI elements) and 2. the ComboBox receives the event to show the list and shows it.
Could you help me find these methods?
I have read through the following classes:
javafx.scene.control.ComboBox,
javafx.scene.control.ComboBoxBase,
com.sun.javafx.scene.control.skin.ComboBoxBaseSkin,
com.sun.javafx.scene.control.behavior.ComboBoxBaseBehavior,
javafx.scene.control.Cell
and some other unrelated classes like the MultipleSelectionModel.
I plan to extend ComboBoxBase to make my MultipleComboBox. I have yet to be able to determine if ComboBoxBase can support MultipleSelectionModel.

The standard location for sources is here.
I don't understand the first question: "the ComboBox creates the list elements to print". What does this mean?
For 2: ComboBoxBaseSkin registers a listener with the comboBox's showingProperty() (this is done in the constructor). The handleControlPropertyChanged(...) method invokes show() or hide() when the property changes. These are abstract methods, implemented in the subclass ComboBoxPopupControl; the implementation of show() in that class basically just calls show(...) on the PopupControl that holds the list view. The content of the popup (i.e. the list view) is created in the subclass ComboBoxListViewSkin.
The actual trigger to show the popup is managed by calling the ComboBox's show() method, which causes the isShowing property to be set to true, triggering the listener described above. This is done from the ComboBoxBaseBehavior method.

Related

Vaadin Best approach: distribute object state changes to other components

I'm dealing with a WebApp (Vaadin19) and stuck now in the question, how to share an object-state change from one component to another. There is one object instance in two or more components. After changing an attribute of the object in one component and going back to another component, I want to see the changed attribute.
Let me explain, what I mean:
There is a grid with some lines of data. The grid shows only a subset of the data to respect the clarity.
A click on the grid opens a detailed view in "read mode". The data is structured (contains sub-objects itself).
A click on the "read mode"-view opens then a dialog with tabs. The activated tab depends on the sub-object, that was clicked before.
After changing an element in the sub-object and closing the dialog, I want that the UI will reload/revalidate it's content. I think it's clear, that I use there the same object-instance.
Is there an event I have to submit to the UI?
Or:
What is the best approach for this?
The actual refresh is easy: theGrid.getDataProvider().refreshAll(), or refreshItem instead if you have access to the item that has been changed and it has a good implementation of equals and hashCode.
How to hook things up so that the dialog notifies the grid is then really depending on your architecture.
If they are already close to each other in the code, then you could e.g. store a reference to the Grid in an instance field and just reference that in the dialog handler.
If you want to decouple, then you need some kind of event bus. You can use the regular Spring event mechanisms as long as you ensure that the event stays within the UI scope. Alternatively, you can use ComponentUtil::addListener and ComponentUtil::fireEvent to use e.g. UI.getCurrent as a simple event bus.

how to make a JTextArea have clickable buttons

As the title says I'm trying to make it so my JTextArea have some strings so that I can call other functions, like a hyperlink of sorts, can that be done?
For reference, I would like to know how the left side of any Tutorialspoint tutorial is made but in Java.
PS: I'm using a CardLayout.
EDIT: Something like this
Forget about using a JTextArea. Take a look at JList instead. This is Swing's basic list class. It supports the selection of one or more items from a list. Although often the list consists of strings, it is possible to create a list of just about any object that can be displayed.
Although a JList will work properly by itself, most of the time you will wrap a JList inside a JScrollPane, so long lists will automatically be scrollable.
A JList generates a ListSelectionEvent when the user makes or changes a selection. This event is also generated when the user deselects a n item. It is handled by implementing ListSelectionListener. This listener specifies only one method, which is called:
void valueChanged(ListSelectionEvent le)
Here, le is a reference to the object that generated the event. Although ListSelectionEvent does provide some methods of its own, often you will interrogate the JList object itself to determine what has occurred.
By default, a JList allows the user to select multiple ranges of items within the list, but you can change this behavior by calling setSelectionMode(int), which is defined by JList. The integer passed to this method must be one of the values defined by the ListSelectionModel interface:
SINGLE_SELECTION
SINGLE_INTERVAL_SELECTION
MULTIPLE_INTERVAL_SELECTION
You can obtain the selected values by calling getSelectedValuesList(), or, if you are using single selection, you can call getSelectedValue(). Once you have the selected the value(s), you can invoke the method(s) dedicated to that/those objects accordingly.
One last tip: In Java, they are called methods, not functions ;)
Happy programming!

How often to pass an object or variable through methods in Java?

I am new to stackoverflow and am sorry, if this question was already asked, but when I searched for one containing my problem, I could not find one.
Here is my question:
In Java I am creating a game (just for fun and am learning to code). You start my game with a launcher to log in. However, if you do not have an account, you can register. What I like more is to use one frame. I have a panel containing the launcher elements and one containing the register elements. I am trying to learn how to code, if another one is working on my project, he does not necessarily need to look all over the code to do some work. In other words, I am using packages to sort classes. Example: Classes for the launcher are in package Launcher. Classes for register are in package register.
In my code, I have a class called Display extending JFrame. Display is instanciated in the class with the main method. Display has 2 Methods for removing a panel and adding a panel, requiring you to pass a JPanel, if you use any of both methods. After Display is instantiated, it instantiates a jpanel inside the constructor. This has all the components to Display. The event listeners are in another class. So I am passing the buttons and Display to that class, because here is the eventlistener code for the button register. In my register class I have defined and instantiated all necessary components. I passed the display through all of the classes until it reached the register class which extends jpanel. I even passed the Launcher class. Now i can use the methods in display to remove the launcher panel and add my register panel.
My Question:
is this good code or overkill?
I never instantiated Display after the main method again. I always declared it and set it to the passed display. I did this, because instantiated a new Display would mean unnecessary use of memory and passing objects in java is actually a passing by references, meaning it is not passing the object but a pointer to the object. So this would mean less memory usage.

What is an HierarchyEvent in Java/Swing?

I'm refreshing my knowledge of Java by looking through some of the java tutorials. On this page I saw the addHierarchyListener and removeHierarchyListener method.
I've searched the API and google and this website. All I got was this:
Adds or removes the specified hierarchy listener to receive hierarchy changed events from this component when the hierarchy to which this container belongs changes. If listener l is null, no exception is thrown and no action is performed.
~Java Tutorial
And the API notes the listener:
The listener interface for receiving hierarchy changed events. The class that is interested in processing a hierarchy changed event should implement this interface. The listener object created from that class is then registered with a Component using the Component's addHierarchyListener method. When the hierarchy to which the Component belongs changes, the hierarchyChanged method in the listener object is invoked, and the HierarchyEvent is passed to it.
Hierarchy events are provided for notification purposes ONLY; The AWT will automatically handle changes to the hierarchy internally so that GUI layout, displayability, and visibility work properly regardless of whether a program registers a HierarchyListener or not.
~JavaAPI#HierarchyEventListener
And Later On the events:
Hierarchy events are provided for notification purposes ONLY. The AWT will automatically handle changes to the hierarchy internally so that GUI layout and displayability works properly regardless of whether a program is receiving these events or not.
This event is generated by a Container object (such as a Panel) when the Container is added, removed, moved, or resized, and passed down the hierarchy. It is also generated by a Component object when that object's addNotify, removeNotify, show, or hide method is called. The ANCESTOR_MOVED and ANCESTOR_RESIZED events are dispatched to every HierarchyBoundsListener or HierarchyBoundsAdapter object which registered to receive such events using the Component's addHierarchyBoundsListener method. (HierarchyBoundsAdapter objects implement the HierarchyBoundsListener interface.) The HIERARCHY_CHANGED events are dispatched to every HierarchyListener object which registered to receive such events using the Component's addHierarchyListener method. Each such listener object gets this HierarchyEvent when the event occurs.
~JavaAPI#HierarchyEvent
My understanding of hierarchy outside of Java is that they specify when one thing is above another etc. Like Maslow's Hierarchy of needs (some needs are above others in importance). So I know what the word means, but what does it mean for a component to belong to a hierarchy? Is it 'owned' by another component, or is one the parent?
THe only event out of added, removed, moved, or resized I can envisionage happening at runtime is the resizing event...but when would this matter?
Would an example be of a JFrame being resized, and then via a HierarchyEvent telling a JPanel to change the layout to fit the new size?
Could someone help explain them?

Netbeans GUI passing textfield values to action defined in App not in View

I created a simple app in Netbeans, it contains a few textfields for user input and a button, I've associated an action with the button through the Netbeans interface but I decided to define the action in the App and not the View so as to follow some notion of MVC.
The action works fine, I can print out the console every time the button is clicked.
But in order to do what I want, I need the values included in the jTextFields!
How to do this? This is the code in TestApp.java:
#Action
public void ClickedOnButton() {
System.out.println("Clicked ok");
System.out.println("Will now attempt to read notes.ini");
ReadNotesFile();
}
And this is the code in TestView.java:
javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(tpa_fixer.TPA_FixerApp.class).getContext().getActionMap(TPA_FixerView.class, this);
jButton1.setAction(actionMap.get("ClickedOnButton")); // NOI18N
What have you tried, and how doesn't it work? The standard way to get a JTextField to display text is to call setText() on it. Have you tried doing this?
Also,
Have you gone through the Swing tutorial about these concepts including using text components, JButtons, and ActionListeners?
Are you seeing any errors in these attempts? If so, post them here.
Is your "control" class, the one with the listener code, separate from your "view" or GUI class? If so, does control have a valid reference to view?
Edit
You state:
I don't want to set the text in the jTextFields, I want to get the values out of them and use it in the method that gets run when I click on the button. I can't see how to do this unless I can pass arguments somehow within the body of the action definition in the View class.
What I've done in this situation, where I need to extract information out of gui fields for manipulation in other classes:
You can give each field an associated public getText() method and then call these methods using the control's reference to the view object. For instance say view has a nameField JTextField, then I'd give it a getNameFieldText() method that returns nameField.getText();.
If you had many such fields, then it may be more efficient to use just one getText method but allow it a parameter to let you choose which field to extract text from. To make this work efficiently, I've sometimes given my GUI a HashMap and then have control pass in the String key that allows the getText method to obtain the correct JTextfield, get its text and return it. I often use the same Strings used as JLabels associated with the JTextField as my key Strings.

Categories