how to make a JTextArea have clickable buttons - java

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!

Related

How to update a panel which shows details of an object

I have a panel, let's call it detailsPanel, which holds a Person reference and displays its field values in the following manner:
Name: person.getName ();
Surname: person.getSurname ();
Emain: person.getEmail ();
.... .......
.... .......
And so on. I will use JLabels (correctly aligned using a GridBagLayout) to show each (fieldName, fieldValue). I have a lot of fields to display.
The problem is that the panel which shows the details must be always visible, i.e it will not be shown in a modal JDialog, so that i could create the panel by simply reading my Person object fields at the panel creation.
The panel must always be visible, and its Person reference will change when the user selects a different row in a Person list. This means i will call a method to update its state, something like:
detailsPanel.setPerson (aPerson);
Now, i'm wondering how i should update all the fields. Should i keep a reference to all the JLabels which show the values, and use setText(value) on each of them when i update the panel, or would it be better to override getText() method for every label, returning the correct field value, so that in the update method i would only repaint the panel, and the text would automatically change when the getter method is used on a different Person object?
Any suggestion is appreciated!
Since this is UI stuff which is usually called almost never (relative to how often things are called in other computation) you don't need to worry about efficiency at all. Just do what you think is the most elegant solution. There are three options That quickly come to my mind. They are ordered from quick and static to elegant and reusable:
Quick and dirty: create your constructor and make everything look nice. Then move everything from the constructor to a separate init() method and every time the entities change, you just call removeAll(); and then init() again.
As you suggested, keep a reference to all labels and use the setPerson() method to update all panels. Then call this method in the constructor (this is arguably the most common solution).
As you suggested, build your own extension of JLabel. This new class should either have an update() method which is to be called when things change, or have it set its own listeners to ensure that it gets notified of any relevant change.
If you are planning to create a single panel which is supposed to display all kinds of objects, you could have those object implement an interface called Displayable which gives you generic access to all its values and maybe even listeners to each value. An alternative to the Displayable interface is to use reflection and use annotations to allow the panel to get its values for display.
Please note that the most elegant solution is - contrary to what some people may tell you - not always the best for any situation. How much maintenance do you expect there to be in the future? How big is the application? Will you ever hand off the code to someone else? All these and more need to be considered to decide how "nice" you want your solution to be.

Extending JFX ComboBox UI for MultipleSelectionModel

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.

JOptionPane List Actions

I want change the icon property of an open JOptionPane upon clicking a list item. (They will correspond with each other)
Is there an equivalent to ActionListener for this? I know how to code for the buttons, unfortunately these actions are not easy?
JOptionPane.showInputDialog(null,
"SELECT A PRIMARY WEAPON FROM THE LIST",
"ARMOURY",
JOptionPane.OK_CANCEL_OPTION,
get.getIcon("shield_and_swords.png"),
character.weaponList(),
"Absolix Polearm")
This is not possible. The JOptionPane.showInputDialog documentation states:
It is up to the UI to decide how best to represent the selectionValues, but usually a JComboBox, JList, or JTextField will be used.
This means the actual GUI element used is implementation-defined and so there couldn't really be a general way to access it, or a consistent action interface to it (it might not even be a clickable list box, for example). Basically, you call showInputDialog() and you get a result back, but beyond that it's a black box and the actual GUI could be anything -- no guarantees are made.
Perhaps you could come up with some really dirty object tree hacks to find the GUI element in most cases, but... consider perhaps creating your own custom dialog with a predictable GUI that you can control instead.

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.

Design for TreeCellRenderer

I have been looking into JTree and TreeCellRenderer. It seems in general, the application (with one JTree) has only one instance of TreeCellRenderer. The application makes multiple calls to TreeCellRenderer's getTreeCellRendererComponent method to decide how each TreeCell is drawn, and such call are made in many occasions (when a cell is selected, deselected, move over, when scrolling, etc.). Why did they decide to do that instead of having multiple instances of TreeCellRenderer, each responsible for one cell??
I am trying to make a JTree where each cell contains a checkbox. The checkbox can be checked/unchecked by the user. Then, the TreeNode userObject's values are set base on the state of these checkboxes. But, from the current JTree design this is impossible - since there is only one instance of JCheckBox, and is only used to show how the Cell looks like (you can't really check it). In some sense I want to separate selection of the TreeCell and the checking of the boxes.
I have some workarounds (implementing MouseAdapter and checking if the mouse click is close by where the checkbox is rendered, then emulate a check on the box by changing its appearence in TreeCellRenderer), but still I want to know if this can be done more directly. Thanks!
Why did they decide to do that instead of having multiple instances of TreeCellRenderer, each responsible for one cell?
This is a nice example of the flyweight pattern.
For a check box tree, I like org.netbeans.swing.outline.Outline, mentioned here, but other examples are available.
Addendum: Reading your question more closely, you ask:
In some sense I want to separate selection of the TreeCell and the checking of the boxes.
This is the correct instinct: The data (checked or unchecked) should be stored in the model (TreeModel), not the view (JCheckBox). The example uses instances of CheckBoxNode in it's (implicit) model, accordingly.

Categories