SWT ComboBoxCellEditor editable - java

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.

Related

Parameterizing Swing dialog

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.

jComboBox changes selectedItem when selecting repeated itens to the first on list

i have a jComboBox, it happens that i may have the same value for more than on item. In this case, when selecting one of them, , the selection goes always to the first item on the list. Right after the click.
Have someone experienced that?Have some solution for that, so the selection doesn't change?
When i select:
http://i.stack.imgur.com/IjlYM.png
Checking again:
http://i.stack.imgur.com/c1lcQ.png
it happens that i may have the same value for more than on item.
Then it sounds like you are adding a custom object to the combo box.
If it displays the same value but is a different item, then you need to implement the equals() method in your Object so the proper Object can be selected.
If you need more help then post a proper SSCCE that demonstrates the problem because we don't have enough information to keep guessing what you might be doing.
A JComboBox always tries to synchronize what is selected in the lists with what is shown in the display field. To do that, it searches the list sequentially for a match of the editor field. So it will always find the first one if there are identical items in the list. Thus you can't just use String objects if there is a possibility for identical Strings in the list. You need to do what #camickr said and use a custom object that has some way to distinguish between two objects whose toString() method returns the same thing (assuming you use the default model and editor).

How can I make a JList interact with my database?

I have a list of items that are pulled from a database, it combines the various fields with a rs.getString method to create a longer string of items, this is done in an action button method.
I would like to be able to click on an item in this list and have one of the fields display as text in a textbox, so this needs to be done through the list selection event method where I instruct the program to set the text to my desired value.
My problem is, I am not sure of the logic to follow in order to specify how to retrieve that fields information that will be corresponding to the item that is selected in the list, can you give me any ideas?
Rather the combining the fields into a single String, create a POJO (Plain Old Java Object), which provides getters (and possible setters) for the fields you want and these objects to the ListModel
Use a ListCellRenderer to customise the way which the JList renders the POJO the way you want to. See Writing a Custom Cell Renderer for more details.
When the user selects an item from the list, use JList#getSelectedValue and cast to the same class as your POJO. You can now use the POJO's getters to extract the properties you want to display.
The idea is to generate a self contained unit of work, which, based on what you want to do, you can customise how the object is displayed.
This concept is a corner stone to the separation of data (model) and the UI (view) behind the Model-View-Controller paradigm and OOP generally...

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

Creating a checkbox java list

i am trying to generate a checkbox list feeded by some records from a database just like the image (JAVA Swing), initially i tried with several chexkboxes but it didn't work, now i am trying with a multiple selection list but it didn't work as well and none of the answered questions here looks like to solve the specific needs i have.
Specifications:
Multiple selection list.
Every node of the list must have a checkbox object.
When checked every node must stay highlighted.
It must have an scrollbar if the content is bigger to the initially setted dimentions.
It must have an scrollbar if the content is bigger to the initially set dimensions.
Put the JList inside a JScrollPane.
When checked every node must stay highlighted.
This is going to confuse the user. The check marks are sufficient.
Every node of the list must have a check box object.
You'll have to extend a JList and a ListCellRenderer to gain this functionality. The ListSelectionListener that I have is over 100 lines of code.
You might find an already existing check box JList on the web. I have one in a book.

Categories

Resources