Toggling JCheckBox value - java

im trying to toggle my jcheckbox. I have set the default to check jcb2. my jcb1 is working fine but my jcb2 can't seem to be toggled on. I added a println and found that it gets printed but my jcb2 does not get check.
class CheckBoxHandler implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
if(jcb1.isSelected())
{
jcb1.setSelected(true);
jcb2.setSelected(false);
}
if(jcb2.isSelected())
{
jcb1.setSelected(false);
jcb2.setSelected(true);
System.out.println("1");
}
}
}

If the first check-box is selected, they will both be selected the moment you select the second check-box. This means the first if-condition will be met, such that the second check-box will be deselected right away.
So instead of checking which check-boxes are selected, you should use the ItemEvent e to see which check-box you just selected.
if(e.getStateChange() == ItemEvent.SELECTED) {
if(e.getItem() == jcb1) {
jcb2.setSelected(false);
} else {
jcb1.setSelected(false);
}
}

Related

Java SWT get bounds of custom element in TreeView

I´ve created a TreeView with a ContentProvider and custom tree elements.
I also have a ISelectionChangedListener added to the TreeView.
I want to add an MouseListener, do detect if an element of the tree is right-clicked and show a popup menu.
If the white area around the tree is clicked, i don´t want to display the popup menu.
The menu is added via Extensions in the plugin.xml.
How can I now evaluate if a tree element is right-clicked, so I can show the popup menu (maybe with visibleWhen in the plugin.xml) ?
I also want to clear the selection, if the right-click is detected in the white area of the TreeView.
Ok, i did not realized that i can still use tree.getItem(...).
So here is my full MouseListener:
treeOPCUA.addMouseListener(new MouseListener()
{
#Override
public void mouseUp(MouseEvent e)
{
if(e.button == 3 && rightMouseClicked == true)
rightMouseClicked = false;
}
#Override
public void mouseDown(MouseEvent e)
{
if(e.button == 3 && rightMouseClicked == false)
rightMouseClicked = true;
if(treeOPCUA.getItem(new Point(e.x, e.y)) == null)
viewer.setSelection(null);
}
#Override
public void mouseDoubleClick(MouseEvent e)
{
viewer.setExpandedState(e.getSource(), true);
}
});
With the boolean variable "rightMouseClicked" I detect in my ISelectionChangedListener if the right mouse is clicked:
if(event.getSelection() instanceof IStructuredSelection && !rightMouseClicked)
I hope this answer helps anyone.

JComboBox itemStateChanged event called twice at a time

resultCombo = new JComboBox();
resultCombo.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent ie) {
sampleText=resultCombo.getSelectedItem().toString();
System.out.println("SampleText : "+sampleText);
}
});
output:
SampleText : selectedword
SampleText : selectedword
Why this event is called twice when selecting item in combobox.?
JComoboBox ItemListener does get called twice for a single change. Once for SELECTED event and once for DESELECTED event.
See this tutorial page on how to write an ItemListener.
Basically what you have to do is
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
//Do any operations you need to do when an item is selected.
} else if(e.getStateChange() == ItemEvent.DESELECTED){
//Do any operations you need to do when an item is de-selected.
}
}
Based on the documentation of ItemListener
Invoked when an item has been selected or deselected by the user. The
code written for this method performs the operations that need to
occur when an item is selected (or deselected).
This suggested that you will receive an event when an item is deselected or selected. Seen as a change in the selected item in the combo box requires that the currently selected item has to be deselected first, it stands to reason that you will receive a ItemEvent.DESELECTED and ItemEvent.SELECTED event

JComboBox - disable setSelectedItem from calling ItemStageChange

I have a JComboBox and it has an associated itemStageChanged method. The JComboxBox is updated in two ways:
I call comboBox.setSelectedItem(...)
The user selects an item in the comboBox via my GUI
I want only (2) to initiate an event. What method (e.g., actionPerformed? changeListener? itemListener? etc) should I use that will only catch (2) and not (1). Currently, itemStateChanged (even with an if statement to check if it is ItemEvent.SELECTED) is being called by (1) and (2).
class ItemChangeListener implements ItemListener{
#Override
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
//gets in here if user selects an item with their mouse
//or if setSelectedItem is called
}
}
}
Give this a try:
Boolean flag = false;
class ItemChangeListener implements ItemListener{
#Override
public void itemStateChanged(ItemEvent event) {
// Condition for the desired event
if (!flag && event.getStateChange() == ItemEvent.SELECTED) {
Object item = event.getItem();
// do something with object
}
}
}
[...]
//Add the listener
myComboBox.addItemListener(new ItemChangeListener());
[...]
// Setting the selection
flag = true;
comboBox.setSelectedItem(...)
flag = false;
Taken from here.

Java - JComboBox Invalidade Item Selection in ItemListener

I am trying to add a condition inside my ItemListener and only if the condition is verified the item gets selected, otherwise I dont want the user to be able to select that item.
How can I invalidade a selection on item listener? Make a new selection inside the item listener? won't that be a infinite loop? :o
Thanks alot in advance.
Here is my code:
private final class classeComboBoxItemListener implements ItemListener {
#Override
public void itemStateChanged(ItemEvent e) {
try {
if(e.getStateChange() == ItemEvent.DESELECTED)
updateLabelLugares(true, (Classe) e.getItem());
if(e.getStateChange() == ItemEvent.SELECTED)
updateLabelLugares(false, (Classe) e.getItem());
} catch (Exception e1) {
/// HERE I WANT TO INVALIDATE THE SELECTION
/// Returning to the item selected before!
}
}
}
How can I invalidade a selection on item listener? Make a new
selection inside the item listener? won't that be a infinite loop? :o
remove ItemListener from JComboBox, then call JComboBox.setSelectedIndex(-1), add ItemListener back to JComboBox (standard and good practicies)
create two void (standard and good practicies) in one add listener, in second remove listener
don't wrap if(e.getStateChange() == ItemEvent.DESELECTED) inside boolean for the reason, to block code executions untill reset status ended
use if - else for if(e.getStateChange() == ItemEvent.DESELECTED){ .... } else { ... }

How to set a mouse Listener that it cannot be clicked in another table?

I have one Table with Playlists. When I double click on a Playlist I get Movies for this Playlist.
However, when I double click on the movies I get an empty table back. Consequently, I do not want to click in the movie table. How to prevent this behaviour?
That's my listener:
playlistTable.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
JTable target = (JTable)e.getSource();
int row = target.getSelectedRow();
videoTableModel = new VideoTableModel(playlistService.getMoviesOfPlaylist(row));
target.setModel(videoTableModel);
movieTable.setEnabled(true);
createPlaylist.setEnabled(false);
setButtonIcon("icons\\playlist_grau.png", createPlaylist, "createPlaylist");
removePlaylist.setEnabled(false);
setButtonIcon("icons\\bin_grau.png", removePlaylist, "removePlaylist");
playlistTable.setEnabled(false);
revalidate();
}
}
});
I appreciate your answer.
Either remove the mouse listener from the table, or set a flag that will make the listener do nothing:
playlistTable.addMouseListener(new MouseAdapter() {
private boolean ignoreDoubleClicks = false;
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2 && !ignoreDoubleClicks) {
JTable target = (JTable)e.getSource();
target.removeMouseListener(this);
or
playlistTable.addMouseListener(new MouseAdapter() {
private boolean ignoreDoubleClicks = false;
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2 && !ignoreDoubleClicks) {
ignoreDoubleClicks = true;
...
You are using the same table to represent both sets of data
JTable target = (JTable)e.getSource();
....
target.setModel(videoTableModel);
Option 1) remove the listener when you change to "video mode"
target.removeMouseListener(this);
Option 2) add a variable and set it to false when you change to "video mode", if the variable is false do nothing.
Option 3) depends of the rest of your code, check the class of the current model (it assumes you have a different one for playlists)
if (!(target.getModel instanceof VideoTableModel)) {
[Your code here]
}
In 1) or 2) you will have to reset the listener when switching back to playlist.
Personally, I would use two tables and hide one or the other based in the mode.
However, when I double click on the movies I get an empty table back.
this has nothing to do with MouseListener, have to disable TableCellEditor
.
public boolean isCellEditable(int rowIndex, int columnIndex){
return false;
}
.
have to test if return -1 (any row is selected) for int row = target.getSelectedRow();
this logics isn't correct why you enables whatever on mouse_double_click, disable ListSelectionMode,

Categories