Add change listner to ComboBox when value remains unchanged - java

I have a ComboBox (JavaFX) that displays info from a database according to the selected String.
For eg: the values in the ComboBox are as follows:
Table 1
Table 2
When I have Table 1 selected, and I then select Table 2, the value change is detected and the code is run.
But when I have Table 1 selected, and I re-select Table 1, no value change is detected. Instead I want code to reload Table 1 from the source database.
The current code:
myComboBox.valueProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue ov, String t, String t1) {
myComboBoxValue = t1;
if (t1 != null) {
displayTable(t1)
}
}
});

The listener will not be triggered if the old selected value is equal to the new value. However, if this is what you want, you can clear the selected value once the comboBox is clicked.
myComboBox.valueProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue ov, String t, String t1) {
myComboBoxValue = t1;
if (t1 != null) {
displayTable(t1)
}
}
});
myComboBox.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
myComboBox.getSelectionModel().clearSelection();
}
});

A working approach to react to item selection in ComboBox irrespective if a new item was selected or not can be found here: https://stackoverflow.com/a/25704438/4749209
Basing on this solution, your sample code would stay like this:
myComboBox.showingProperty().addListener((ov, old_show_state, new_show_state) ->
{
// checks if the combobox popup menu was closed
if (new_show_state == false)
{
myComboBoxValue = myComboBox.getValue();
if (myComboBoxValue != null)
{
displayTable(myComboBoxValue)
}
}
});
The only problem to mention is that the reload operation you mentioned will happen if the user clicks outside the combobox popup menu area, as it will close too.

You don't change a value when it remains the same, so no event gets fired. Depending on what you are trying to do you could e. g. add a listener to onHidden. It's ugly and doesn't cover e. g. when you set the same value programmatically, but would work for changes via mouse click.

Related

JavaFX setting Button style on Mouse Click

I've got a problem with a Java project I'm working on: I'm creating a grid of buttons via code in javafx on a pane. The buttons are all types of a subclass of the javafx Button class that i wrote.
Here's the header of the class:
private final String BASIC_STYLE = "-fx-font: 6 arial;";
private final String CLICKED_STYLE = "-fx-background-color: #0f0";
private int row;
private int col;
private String category;
private boolean selected = false;
Within the constructor i do the follwing:
this.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
toggleSelected();
}
});
Here's the toggleSelected() Method:
public void toggleSelected() {
this.selected = !selected;
this.setStyle(selected ? this.BASIC_STYLE : this.BASIC_STYLE+this.CLICKED_STYLE);
}
It's basically supposed to swap the style everytime you click the button. When i click the button, the button first gets selected by the OS (the border is becoming blue) and only after i click a second time on the exact same button it'll become green (the style that i'm giving it via setStyle).
However, the selected property becomes true on the first click and false on the second click, which means i click once on the button and it gets a blue border and selected = true, if i click on it a second time it becomes green and selected = false and if i click on it a third time it becomes normal again but selected will be true again.
I find it to be really strange that the first click on a button changes the "selected" variable correctly but not the style. Why is this happening and how can i avoid that i've to select the button first before i can click it?
You initialize
selected = false ;
and
setStyle(BASIC_STYLE);
But your event handler enforces the rule
selected == true -> setStyle(BASIC_STYLE);
selected == false -> setStyle(CLICKED_STYLE);
So your initial state is inconsistent with the state your handler enforces.
From the initial state, the first time you click, selected is set to true which causes setStyle(BASIC_STYLE) (which is the value it already has, so nothing changes). From then on, everything will switch as required.
You either need to change the initial state, or switch the logic of the setStyle(...) call in the handler.
public class ButtonEnterAction extends Button {
boolean selected = true;
public ButtonEnterAction(String connect) {
setText(connect);
action();
}
public ButtonEnterAction() {
action();
}
private void action() {
EventHandler<KeyEvent> enterEvent = (KeyEvent event) -> {
if (event.getCode() == KeyCode.ENTER) {
fire();
}
};
addEventFilter(KeyEvent.KEY_PRESSED, enterEvent);
// setOnMouseEntered(new EventHandler<MouseEvent>() {
// #Override
// public void handle(MouseEvent me) {
// SepiaTone st = new SepiaTone();
// setEffect(st);
// }
// });
// setOnMouseExited(new EventHandler<MouseEvent>() {
// #Override
// public void handle(MouseEvent me) {
// setEffect(null);
// }
// });
}
#Override
public void fire() {
super.fire(); //To change body of generated methods, choose Tools | Templates.
if (selected) {
SepiaTone st = new SepiaTone();
setEffect(st);
} else {
setEffect(null);
}
selected = !selected;
}
}
Create the Instant Class in ButtonEnterAction is like.
ButtonEnterAction bea = new ButtonEnterAction("TestButton");
bea.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("hello");
}
});

JComboBox Selection Change

everyone i am quite new to Java GUI, i am having an issue with a JComboBox , where it is firing when i removeAllItems from a combo box to refresh it, this is an issue because i am getting the selected items Details and populating a textboxes with them so as it is firing at that point i am getting a Null Pointer.
Is there any simple(ish) way to have method on the ComboBox that is called when the selected item is changed not just when the combo box contents is changed?
Code
comboBox current method
private void customerComboActionPerformed(java.awt.event.ActionEvent evt) {
setDetails();
}
Method for setting the Items in the combo Box
public void setCustomers()
{
customerCombo.removeAllItems();
for (Customer curr : Main.getNewCustomerList().getCustomers())
{
customerCombo.addItem(curr);
}
}
method for setting the details
public void setDetails()
{
Customer selected = (Customer) customerCombo.getSelectedItem();
forenameText.setText(selected.getForename());
surnameText.setText(selected.getSurname());
costperkgText.setText(String.valueOf(selected.getDeliveryCost()));
line1Text.setText(String.valueOf(selected.getColAddress().getAddressLine1()));
line2Text.setText(String.valueOf(selected.getColAddress().getAddressLine2()));
cityText.setText(String.valueOf(selected.getColAddress().getCity()));
postcodeText.setText(String.valueOf(selected.getColAddress().getPostcode()));
}
You are not accounting for the case where there is no selection.
public void setDetails()
{
Customer selected = (Customer) customerCombo.getSelectedItem();
if (selected != null)
{
// there is a selection so use it
}
else
{
// for example, clear the text boxes
}
}
We would also expect that changing the contents of the combo box might change its selection so we shouldn't ignore it.
I like to set a flag. The key is making sure the flag doesn't get stuck to false.
private volatile boolean fire = true;
public void setItems(Object[] items) {
try {
fire = false; // Don't fire updates
updateItems(items);
} finally {
fire = true; // always reset no matter what!
}
}
private JComboBox create() {
JComboBox cb = new JComboBox();
cb.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
if(fire) {
notifyListeners();
}
}
});
}
You have to make sure this isn't being called by multiple threads, but since Swing isn't thread safe this should be happening anyway.

ComboBox SAME item selected action listener

A combo box will fire an event if a DIFFERENT value is selected. I want to be also be able to listen to the SAME item being selected (that is, valueProperty has no change). There seems to be no way to do this.
I tried extending the ComboBox and finding a way to listen for the little popup menu being closed, but I don't even have access to that! What can I do?
Here is what I was trying:
class ResponsiveComboBox<E> extends ComboBox<E> {
public ResponsiveComboBox() {
super();
assert getContextMenu() != null; //Asssertion failed!
this.getContextMenu().setOnHiding((WindowEvent event) -> {
fireEvent(new ActionEvent());
});
}
}
comboBox.showingProperty().addListener((obs, wasShowing, isShowing) -> {
if (! isShowing) {
System.out.println("Combo box popup hidden");
}
});
This event handler might be triggered before the value is changed.

Showing Edit / Delete buttons for each row in a GWT CellTable?

I have a cell table showing some data. For each row, I want to have two columns which contain edit / delete buttons. When each button is clicked, it should be able to notify a listener which button was clicked (and preferably also be able to pass in the object that row is associated with).
How can I do this? Specifically, I know how to render a button, but how can I process the on-click event and pass in the object which the user clicked to edit or delete?
This is the standard approach:
myTable.addCellPreviewHandler(new Handler<MyObject>() {
#Override
public void onCellPreview(CellPreviewEvent<MyObject> event) {
if ("click".equals(event.getNativeEvent().getType())) {
if (event.getColumn() == 0 || event.getColumn() == 1) {
MyObject object = event.getValue();
Window.alert("Column clicked: " + event.getColumn());
}
}
}
});
This is a more efficient solution, because you only have one handler attached to a table, instead of trying to attach a handler to each button in each row.
I think you can make a foreach through all the rows in the celltable (I never worked with celltables)
And then you can add your own ClickHandler to the Button.
Something like that (not tested):
final int row = myrow; // add the row value or a object identifier or similar
Button delete_button = new Button("delete");
delete_button.addClickHandler(new ClickHandler(){
#Override
public void onClick(ClickEvent event) {
// Insert your delete funciton
delete(row);
}
});
You mentioned a Listener, Listener are depreciated, use Handler instead.

JPopupMenu on JTable -> Get the cell the menu was created on

I have a situation where I have a popup menu created when a JTable is right clicked on. Standard way of creating the popup menu:
aJTable.setComponentPopupMenu(rightClickMenu);
Now afterwards in the action that gets registered, I am unable to find out which cell was right clicked on to get that popup menu to appear.
rightClickMenuItem.addActionListener(new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
// Work out what cell was right clicked to generate the menu
}
});
Any ideas on how you do this?
Astonishing fact: with a componentPopupMenu installed, a mouseListener never sees the mouseEvent that is the popupTrigger (reason is that showing the componentPopup is handled globally by a AWTEventListener installed by BasicLookAndFeel, and that listener consumes the event).
The only place which sees the mousePosition of that trigger is the getPopupLocation(MouseEvent), so the only reliable way to get hold of it (for doing location dependent config/actions) is #Mad's suggestion to override that method and store the value somewhere for later use.
The snippet below uses a clientProperty as storage location:
final JTable table = new JTable(new AncientSwingTeam()) {
#Override
public Point getPopupLocation(MouseEvent event) {
setPopupTriggerLocation(event);
return super.getPopupLocation(event);
}
protected void setPopupTriggerLocation(MouseEvent event) {
putClientProperty("popupTriggerLocation",
event != null ? event.getPoint() : null);
}
};
JPopupMenu popup = new JPopupMenu();
Action action = new AbstractAction("show trigger location") {
#Override
public void actionPerformed(ActionEvent e) {
JPopupMenu parent = (JPopupMenu) SwingUtilities.getAncestorOfClass(
JPopupMenu.class, (Component) e.getSource());
JTable invoker = (JTable) parent.getInvoker();
Point p = (Point) invoker.getClientProperty("popupTriggerLocation");
String output = p != null ? "row/col: "
+ invoker.rowAtPoint(p) + "/" + invoker.columnAtPoint(p) : null;
System.out.println(output);
}
};
popup.add(action);
popup.add("dummy2");
table.setComponentPopupMenu(popup);
#MadProgrammer's suggestion of getPopupLocation looked promising, but I couldn't work out how to get the information across between the table and the actionEvent...
I got around this by making sure that the row was selected when you rightclicked on it -> since the popup menu prevents the selection of the row, you can add in a mouse listener that makes sure the row gets selected no matter what click (left or right) is pressed.
aTable.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
int r = aTable.rowAtPoint(e.getPoint());
if (r >= 0 && r < clt.getRowCount()) {
aTable.setRowSelectionInterval(r, r);
} else {
aTable.clearSelection();
}
}
});
This means that in the rightClickMenuItem's action listener, you can grab the table's selected cell / row
rightClickMenuItem.addActionListener(new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
aTable.get details about the selected one....
}
});
Too easy! Thanks everyone for the help.
JTable has methods
int row = rowAtPoint(p);
int col = columnAtPoint(p);
So pass the MouseEvent's point and use the values
Add a MouseListener and store the last right click point somewhere.

Categories

Resources