I am trying to select/focus a row of a TableView programmatically.
I can select a row, but it is not getting rendered as focused (not highlighted). I have tried many combinations of the code below, but nothing seems to work.
table.getSelectionModel().select(0);
table.focusModelProperty().get().focus(new TablePosition(table, 0, column));
table.requestFocus();
Is it possible to highlight a row programmatically?
I am using JavaFX 2.2.21
Try putting your request for table focus first and then wrapping the whole thing in a runLater.
Platform.runLater(new Runnable()
{
#Override
public void run()
{
table.requestFocus();
table.getSelectionModel().select(0);
table.getFocusModel().focus(0);
}
});
table.getFocusModel().focus(0); is not needed, but I would also add scrollTo as well.
Java 8:
Platform.runLater(() ->
{
table.requestFocus();
table.getSelectionModel().select(0);
table.scrollTo(0);
});
Java 7:
Platform.runLater(new Runnable()
{
#Override
public void run()
{
table.requestFocus();
table.getSelectionModel().select(0);
table.scrollTo(0);
}
});
I have two components: a ListView and a TableView. When an item in the ListView is clicked, I want the focus and selection to move to the TableView and render the selected component in the TableView. To accomplish this, I did it with:
void listViewClickHandler(MouseEvent e){
A a = listView.getSelectionModel().getSelectedItem();
if(a != null){
// some stuff
// move focus & selection to TableView
table.getSelectionModel().clearSelection(); // We don't want repeated selections
table.requestFocus(); // Get the focus
table.getSelectionModel().selectFirst(); // select first item in TableView model
table.getFocusModel().focus(0); // set the focus on the first element
tableClickHandler(null); // render the selected item in the TableView
}
void tableClickHandler(MouseEvent e){
B b = table.getSelectionModel().getSelectedItem();
render(b);
}
table.getSelectionModel().select(0); works for me. Maybe the problem is in your css?
Related
I have a SWT Table with check boxes enable on the rows to allow TableItems to be selected. I currently have a listener on there
myTable.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event selectionEvent) {}});
This listener fires both when a check box for a row is checked and when a row is highlighted / selected, I haven't been able to find a way to separate these actions. Am I using the wrong listener or is there a way to do this?
Thanks for any help.
You can check within the Listener if it's a checkbox event or a selection event:
myTable.addListener(SWT.Selection, new Listener()
{
public void handleEvent(Event e)
{
if(e.detail == SWT.CHECK)
{
// Checkbox
}
else
{
// Selection
}
}
});
Is there a way to set the value in a ComboBoxCellEditor other then when the focus is lost on the cell? I'm using it in each cell of a column in a TreeViewer and the only time that the setValue method is called is when focus is lost on the cell. So when a user makes a selection and doesn't click off of the cell the value is never set to the new selection. I've tried adding listeners on the ComboBoxCellEditor and on the control of the ComboBoxCellEditor but nothing seems to pick up the selection event.
I figured out that I needed to cast the control to a CCombo in order to add the correct type of listener to the ComboBoxCellEditor. Here's what I did:
CCombo combo = (CCombo) cellEditor.getControl();
combo.addSelectionListener(new SelectionListener()
{
#Override
public void widgetSelected(SelectionEvent paramSelectionEvent)
{
//selection code here...
}
#Override
public void
widgetDefaultSelected(SelectionEvent paramSelectionEvent)
{
//do nothing here...
}
});
I have an editable JComboBox with a single listener on it.
It is a documentListener that execute some code when the user insert or remove some text inside the combobox textfield:
((JTextComponent)combobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(..)
My problem is that when the user select an element from the popup and the content of the combobox textfield changes there are two events executed into the documentListener, one is a removeUpdate() corresponding to the deletion of the previous content and the other is a insertUpdate() corresponding to the insertion of the new value.
I want that only one execution of my code is done and not two. How can I avoid that the code is executed two times when the user select an entry from the popup?
I tried various combination of different listener but for now without result.
What I want in the end is that my code is execute only one time when:
- The user change the text into the combobox textfield.
- The user select an element from the combobox popup
Thanks in advance.
[EDIT 1]
As requested I updated adding SSCCE
myCombobox = new javax.swing.JComboBox<String>();
myCombobox.setEditable(true);
((JTextComponent)myCombobox.getEditor().getEditorComponent()).getDocument().addDocumentListener(
new DocumentListener(){
#Override
public void insertUpdate(DocumentEvent e) {
System.out.println("insert performed");
}
#Override
public void removeUpdate(DocumentEvent e) {
System.out.println("remove performed");
}
#Override
public void changedUpdate(DocumentEvent e) {
System.out.println("change performed");
}
});
myCombobox.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
System.out.println("Action performed");
}
}
});
Note that in this case I have an ItemEvent instead of an ActionEvent because I'm continuing to modify my code searching for a solution in any case the behavior should not be influenced by this.
You can check ((JTextComponent)combobox.getEditor().getEditorComponent()).hasFocus() to be sure user types in the editor.
I am very new to SWT. Started working on it today actually. I have a table of type CheckboxTableViewer. What i want to be able to do is whenever the user selects the row (i.e clicks anywhere on the row) I want the check box to be checked (ticked). Currently I have a listener on the CheckboxTableViewer as follows:
diagnosesTableViewer.addCheckStateListener(new ICheckStateListener() {
#Override
public void checkStateChanged(CheckStateChangedEvent event) {
Nomenclature changedStateNomenclature = (Nomenclature) event
.getElement();
if (event.getChecked()) {
selectedNomenclatures.add(changedStateNomenclature);
} else {
selectedNomenclatures.remove(changedStateNomenclature);
}
}
});
I am able to select the row by checking on the checkbox. But i want to select the check box even when the user selects the row by clicking anywhere on that row on any column (not just the checkbox).
I guess that logic would go somewhere in the addSelectionChangedListener for the addSelectionChangedListener. But I am not sure how to go about it. Can anyone help me with this?
Use this code: Add selection listener to the table. ctv is the instance of of your CheckboxTableViewer.
Also I assumed CheckboxTableViewer allow only single selection not multi.
ctv.getTable().addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
int df = ctv.getTable().getSelectionIndex();
ctv.setChecked(ctv.getElementAt(df), !ctv.getChecked(ctv.getElementAt(df)));
}
});
I made my JComboBox editable using:
myCombo.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
I encounter multible problems with the following task:
When writing into the combobox, the new content of the box should be taken and compared with a list and all entries starting with this text should be shown in the popupmenu.
So if i have a list with: "Aban" "Aben" "Aber" "Acen" "Aden"
and enter "Ab" into the box, then the pop-up should display the first 3 entries.
When clicking one of the entries (Either by keyboard selecting and pressing enter/tab or by clicking with a mouse) The ComboBox should get that value and the Popup should hide. I need to find this action as some of the elements have a note at the end (In backets which I require) but only when one of the entries is selected
Here are the most imporant parts of my code:
final JTextComponent tcA = (JTextComponent) myCombo.getEditor().getEditorComponent();
tcA.getDocument().addDocumentListener(new DocumentListener() {
public void methodUsedByinsertUpdateAndremoveUpdate(DocumentEvent e) {
String item = ((JTextComponent) myCombo.getEditor().getEditorComponent()).getText();
//Routine to get the new list in a vector, not pasted for readability
DefaultComboBoxModel newMyComboModel = new DefaultComboBoxModel(myVectorList);
myCombo.setModel(newMyComboModel);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
myCombo.showPopup();
}
});
}
}
myCombo.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(myCombo.getModel().getSize() == 1) {
//Special logic to find out if the selected item has a note
SwingUtilities.invokeLater(new Runnable() {
public void run() {
myCombo.hidePopup();
}
});
}
}
With this, i have:
Trouble with the first character (Caret position not working correctly)
Popup not automatically shown and hides when entering new character into the field
Problems with Swing GUI not being actualised
If you require more information just ask
The Glazed List recommended by peeskillet did exactly what I wanted