Parameterizing Swing dialog - java

I need to create a number of dialogs that are of the same basic structure that looks like this:
There will be a varying number of rows, each with a labeled checkbox and two combo boxes that have integers, the range of which varies. The check box just enables the combo boxes. When the selection in the first combobox is changed, the second one gets initialized and enabled.
Since I have to do over 50 over these I'd like automate the programming. I believe some of the code can be handled with loops, selecting combobox names from preset string arrays. What I can't figure out is how to parameterize things like action listeners.
First question is can this be done at all. If it can, how?
Ed

First create a notional RowModel containing a Boolean value for the checkbox, a String for the label and two instances of ComboBoxModel, one for each of the combos. Handle the combo dependency as shown here. Let your program maintain a List<RowModel> for each distinct dialog. You can manage an arbitrary number of rows in a suitable TableModel and display them is a JTable as shown here.

Actually, I just want to say that MadProgrammer provided an answer that worked for me in his Comment.

Related

Differences between ComboBox and ChoiceBox in JavaFX

What are the differences between ComboBox and ChoiceBox in JavaFX? I'm not entirely clear on that just from the Javadoc for both classes.
At the end of the day, I need a dropdown control that can be repopulated dynamically at runtime (I've got a database on the backend). For all cases in my application, I only need to select one item from the dropdown menus. The user also shouldn't be able to add an option to the dropdown menu from the screens they are visible on.
My understanding is that ComboBox allows the user to add items to the dropdown list and allows for selecting multiple items, but from the Javadoc it seems like it's possible to setup ComboBox in a way that meets my needs, so it seems like they're interchangeable to some extent. I guess ComboBox has a bit more overhead than I really need in this case, but is there anything else that only a ComboBox could do that would factor into this decision?
Edit
I guess I kind of answered my own question on the key differences, so is there something else I've not mentioned that differentiates the 2?
ComboBox supports a cellFactory which allows essentially an arbitrary UI for displaying the item in each cell. ChoiceBox does not have this functionality and will only display text in each cell (which you can configure using a converter).
See http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/combo-box.htm#BABJCCIB listing 16.5 for an example of a custom cell factory in a combo box.
Well ChoiceBox is of the idea showing you optional choices, and ComboBox well shows you a list of items, ChoiceBox is like ComboBox but ComboBox is for a really lengthy list as you can specify the number of items to display like 10 or more or less, but ChoiceBox does not have the option it list all options and if its very long you wouldn't like the look.
in short ChoiceBox, for small set of list less than 10, for more ComboBox
That is from my perspective the difference, as for styling you can style all.
Combo Box
A combo box is a typical element of a user interface that enables users to choose one of several options. A combo box is helpful when the number of items to show exceeds some limit, because it can add scrolling to the drop down list, unlike a choice box. If the number of items does not exceed a certain limit, developers can decide whether a combo box or a choice box better suits their needs.
Choice Box
This chapter describes choice boxes, the UI controls that provide support for quickly selecting between a few options.
http://docs.oracle.com/javafx/2/ui_controls/jfxpub-ui_controls.htm
We can simply differentiate ComboBox and ChoiceBox by their functionalities.Just have a look on deffintion.
The JavaFX ComboBox control enables users to choose an option from a predefined list of choices, or type in another value if none of the predefined choices matches what the user want to select.
The JavaFX ChoiceBox control enables users to choose an option from a predefined list of choices only.
Apart from the mentioned differences:
ComboBox can show a prompt with setPromptText (ChoiceBox doesn't provide that method)
ComboBox can show more than 10 rows with setVisibleRowCount (ChoiceBox doesn't provide that method)

What is a good practice to handle a lot of checkboxes in JavaFX

I need to build a View which has a 4 layer nested Multi-Accordion with a lot of checkboxes inside them. All together there might be around 30-40 Checkboxes all through the Accordions.
The next step will be, that i have some sort of logic behind all this. Depending on the selection combination of the checkboxes I will change a text label accordingly.
My thought process was: I put up all these checkboxes and give them a numeric fx:id representing there position in the nested accordion graph. Something like "1_1" or "2_4_1".
After that, I build one ChangeListener calling a Method on Selection of a Checkbox. I can look up the Id of the checked box, look it up in my data (to see which Text belongs to it and if any rules interfer with other boxes) and handle the logic accordingly while putting the id and its text in a Map or List to keep it for later and to keep track of the checked boxes.
Now I came to know, that getting the fx:id isnt something JavaFX wants me to do. I cant deliver a custom id in custom property inside the FXML either (couldnt find anything regarding this).
I am now pretty much at the end of my knowledge (I did just start with JavaFX and have some basic Java knowledge) and it seems to me, that I tackle this topic from the wrong side.
My question is now: What would be a best practice to handle dozens of checkboxes and trigger logic in the code according to the box that was checked without writing a ChangeListener for every single Check Box leaving me with some (imo) ugly code all the way.
EDIT: I forgot to mention: I did achieve some sort of functional solution by writing a custom CheckboxChangeListener with a reference to the Element the addListener method was called on and using "getId()" on this reference. I came to know though, that this method references the css:id of the fxml element and not its fx:id and I am not quite sure if this is a proper way to go
You should look into databinding with javafx. For example:
CheckBox cb1 = new CheckBox("1");
CheckBox cb2 = new CheckBox("2");
BooleanProperty isCb1Selected = cb1.selectedProperty();
BooleanProperty isCb2Selected = cb2.selectedProperty();
Textfield foo = new TextField().visibleProperty().bind(isCb1Selected.and(isCb2Selected));
This would hide the textfield foo if atleast one of the checkboxes isn't selected.
You can find other examples here and here an oracle tutorial

how can i programatically select a particular text from JTable when i do search in it?

here are the screenshots of the application
Rows will be displayed in the Table according to the text which is written in the search textfield.
Now i want to mark that particular text as per the shown in the second image with the yellow color
I know how to select a row or a particular cell.
but I don't know how to select a particular text inside the cell of any row in the table.
I am guessing you know how to search in JTable, so I am not pasting code of it here.
You can look over the SwingX library. It has this kind of function as you said predefined it it. You just need to add it to your table. This is where you can find it. Give it a try you will surely like it.
The basic premise would be to use a custom TableCellRenderer that provided the functionality that you require.
The problem is how to implement it.
I would create a TableCellRenderer based on a JTextField, remove it's border and make it transparent. This will allow you to use the text highlighting functionality provided by JTextCompoent to highlight portions of the text, as demonstrated here.
The next problem is then knowing what to highlight. There are a number of possibilities.
You could provide a method in your table model that could return the current text that should be highlighted.
I'd, personally, probably use the JTable#putClientProperty and JTable#getClientProperty methods to seed the search text.
Or, you could actually provide a simple model that directly to the renderer which had a method that returned the current search text. This might actually be more useful as you could link it to field, the method building the filter and the renderers and allow them to simply seed each other

SWT ComboBoxCellEditor editable

I have a TableViewer where the values in one column should typically come from a dynamic list.
I'm currently using org.eclipse.jface.viewers.ComboBoxCellEditor , which is actually a Select-List: it stores the index of the selected value. If I change the underlying list (calling setItems(String[]), it's clumsy to keep the previous selected value... (specially if it's not included in the list anymore!) What I'd wish is actually a cell editor that stores, not the index from the list, but the string (perhaps letting the user edit it freely, perhaps not), where the list is just used as a suggestion at input time - like a "combobox" was supposed to work in the good old days... Is this possible?
I would suggest you to have your CellEditor to mimic the behavior that you are looking for. Extend ComboBoxViewerCellEditor and override doGetValue() method. Add modify listener on Combo control and also filter (which filters list items based on input text) to comboviewer.
You should look at :
org.eclipse.wst.xml.ui.internal.properties.StringComboBoxCellEditor This class comes from WTP project; It's an extended ComboBoxCellEditor that selects and returns Strings.
codemirror.eclipse.ui.xquery.viewers.StringComboBoxCellEditor It's the copy/paste of WTP StringComboBoxCellEditor; it adds the capability to add the item in the combo when it is not found.

A Swing component to display separate, selectable strings on different lines?

I'm looking for a Swing component that displays several separate strings (probably from a string array or list) on separate lines within a pane or field (scrollable if there's too many to show at once). The user needs to be able to select one of these lines (double-clicking would be ideal) and thereby trigger a listener that does some magic with that line's string. Can anyone point me in the right direction?
Try a JList: http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JList.html. Can be added to a ScrollPane if you want to include scrolling ability.
I suggest you to use a JList inside a vertical JScrollPane.
The user needs to be able to select one of these lines (double-clicking would be ideal) and thereby trigger a listener that does some magic with that line's string
You can use the List Action for this.

Categories

Resources