JavaFX OnMouseDragged not getting fired when dragging an Item in a Treeview - java

Bascially I try to implement the behaviour, that when dragging an item from a treeView and going either at the bottom or top of the treeView, the treeView will automatically scroll down or up.
Thus far I was able to simply extend the TreeView and add a onMouseDragged event handler to it. like this
public ExtendedTreeView()
{
super();
setOnMouseDragged((MouseEvent event) -> onMouseMoved(event));
}
The event handler then looks like this (System outs are bascially just for debug purpose)
private void onMouseMoved(MouseEvent event)
{
System.out.println("onMouseMoved-----------------------------------------------------------------");
System.out.println(event.getEventType().toString());
// -----only apply when there is a drag event in progress
if(event.getEventType().equals(MouseEvent.MOUSE_DRAGGED))
{
// -----first check if we are the scrollEdges
System.out.println(String.format("Size: %f : %f", getWidth(), getHeight()));
System.out.println(String.format("Bounds: %f : %f : %f : %f", _scrollBounds.getTop(), _scrollBounds.getRight(), _scrollBounds.getBottom(), _scrollBounds.getLeft()));
System.out.println(String.format("Node: %f : %f", event.getX(), event.getY()));
//-----up and down directions are preferred
ScrollDirection direction = ScrollDirection.None;
if(event.getY() <= _scrollBounds.getTop())
direction = ScrollDirection.Top;
else if(event.getY() >= _scrollBounds.getBottom())
direction = ScrollDirection.Bottom;
else if(event.getX() >= _scrollBounds.getRight())
direction = ScrollDirection.Right;
else if(event.getX() <= _scrollBounds.getLeft())
direction = ScrollDirection.Left;
System.out.println(String.format("Direction: %s", direction.toString()));
}
}
When I drag and move the mouse in the TreeView it get the desired result.
The Problem is how ever
as soon as I drag an actuall Item, the drag event only occurs once and then never again.
The Item also has a drag and drop handling which essentially looks like this.
Note that this is actually a controller which has info about which TreeCell he belongs to.
#FXML
public void onDragDetected(MouseEvent event)
{
Base item = getTreeCell().getItem();
Dragboard dragboard = getTreeCell().startDragAndDrop(TransferMode.MOVE);
//-----get info from the item and put it in the clipboard
dragboard.setContent(content);
//event.consume();
}
#FXML
public void onDragEntered(DragEvent event)
{
boolean acceptDrag = false;
Dragboard dragboard = event.getDragboard();
Base current = getTreeCell().getItem();
//-----get the info from the clipboard and do something with it
//-----essentially the acceptDrag will be set to true here
if((_isDragAccepted = acceptDrag))
{
event.acceptTransferModes(TransferMode.MOVE);
//event.consume();
}
}
#FXML
public void onDragOver(DragEvent event)
{
if(_isDragAccepted)
{
event.acceptTransferModes(TransferMode.MOVE);
//event.consume();
}
}
#FXML
public void onDragExited(DragEvent event)
{
_isDragAccepted = false;
//event.consume();
}
#SuppressWarnings("unchecked")
#FXML
public void onDragDropped(DragEvent event)
{
Dragboard dragboard = event.getDragboard();
TreeItem<Base> currentItem = getTreeCell().getTreeItem();
Base current = getTreeCell().getItem();
//-----get info from clipboard again and do the necessary stuff
//-----essentially an item will be transfered here from one node to another
event.setDropCompleted(true);
//event.consume();
}
I also got some information about the event handling in javafx from this link
According to this if the event was not consumed, it should bubble up all the way to the source again, hence it should also pass the TreeView should it not ? So I would really like to know what I am doing wrong here.

Ok So I figured out what the problem was,
I was listening to the wrong event, the event I needed to register was the onDragOver event instead of the onMouseDragged event.
So in case anyone ever needs an autoscrollTreeView, the final solution for an autoscroll treeView looks like this now:
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.LongProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.DragEvent;
import com.sun.javafx.scene.control.skin.VirtualFlow;
#SuppressWarnings("restriction")
public class AutoscrollTreeView<T> extends TreeView<T>
{
// region Enumerations
private enum ScrollDirection
{
None, Top, Bottom
}
// endregion
// region Static
private static final double _milliSecondToSecondFactor = 1.0d / 1000.0d;
// endregion
// region Fields
/**
* the time interval in milliseconds in which the scroll is performed
*/
private final LongProperty _checkInterval = new SimpleLongProperty(50);
/**
* the actual scroll speed when being in the scroll areas
*/
private final DoubleProperty _scrollSpeed = new SimpleDoubleProperty(1.0);
/**
* the scroll speed increment per second the user remain in the scroll area
*/
private final DoubleProperty _scrollSpeedIncrementPerSecond = new SimpleDoubleProperty(0.0);
/**
* distance from the top, which defines the area which will start a scroll in the -y axis
*/
private final DoubleProperty _dragIdentifierTop = new SimpleDoubleProperty();
/**
* distance from the bottom, which defines the area which will start a scroll in the +y axis
*/
private final DoubleProperty _dragIdentifierBottom = new SimpleDoubleProperty();
/**
* time at which the user entered the any scroll area
*/
private long _initialDragTime = -1;
/**
* last time the interval was checked
*/
private long _lastCheck = -1;
// endregion
// region Constructor
public AutoscrollTreeView()
{
super();
addEventHandlers();
}
public AutoscrollTreeView(TreeItem<T> root)
{
super(root);
addEventHandlers();
}
// endregion
// region Getter/Setter
public final void setCheckInterval(long value)
{
_checkInterval.set(value);
}
public final long getCheckInterval()
{
return _checkInterval.get();
}
public final LongProperty checkIntervalProperty()
{
return _checkInterval;
}
public final void setScrollSpeed(double value)
{
_scrollSpeed.set(value);
}
public final double getScrollSpeed()
{
return _scrollSpeed.get();
}
public final DoubleProperty scrollSpeedProperty()
{
return _scrollSpeed;
}
public final void setScrollSpeedIncrementPerSecond(double value)
{
_scrollSpeedIncrementPerSecond.set(value);
}
public final double getScrollSpeedIncrementPerSecond()
{
return _scrollSpeedIncrementPerSecond.get();
}
public final DoubleProperty scrollSpeedIncrementPerSecondProperty()
{
return _scrollSpeedIncrementPerSecond;
}
public final void setDragIdentiferTop(double value)
{
_dragIdentifierTop.set(value);
}
public final double getDragIdentifierTop()
{
return _dragIdentifierTop.get();
}
public final DoubleProperty dragIdentifierTopProperty()
{
return _dragIdentifierTop;
}
public final void setDragIdentiferBottom(double value)
{
_dragIdentifierBottom.set(value);
}
public final double getDragIdentifierBottom()
{
return _dragIdentifierBottom.get();
}
public final DoubleProperty dragIdentifierBottomProperty()
{
return _dragIdentifierBottom;
}
// endregion
// region Events
private void onDragEvent(DragEvent event)
{
// -----only apply when there is a drag event in progress
if(event.getEventType().equals(DragEvent.DRAG_OVER))
{
if(_lastCheck == -1 || System.currentTimeMillis() - _lastCheck > _checkInterval.get())
{
ScrollDirection direction = ScrollDirection.None;
if(event.getY() <= _dragIdentifierTop.get())
direction = ScrollDirection.Top;
else if(event.getY() >= getHeight() - _dragIdentifierBottom.get())
direction = ScrollDirection.Bottom;
if(direction != ScrollDirection.None)
{
double additionalScrollSpeed = 0;
if(_initialDragTime > 0)
additionalScrollSpeed = _scrollSpeedIncrementPerSecond.get() * (System.currentTimeMillis() - _initialDragTime) * _milliSecondToSecondFactor;
else
_initialDragTime = System.currentTimeMillis();
if(direction == ScrollDirection.Bottom)
scrollY(_scrollSpeed.get() + additionalScrollSpeed);
else
scrollY(-(_scrollSpeed.get() + additionalScrollSpeed));
}
else
{
_initialDragTime = -1;
}
_lastCheck = System.currentTimeMillis();
}
}
else
{
_initialDragTime = -1;
_lastCheck = -1;
}
}
// endregion
// region Private
/**
* adds the necessary event filters
*/
private void addEventHandlers()
{
addEventHandler(DragEvent.DRAG_OVER, event -> onDragEvent(event));
addEventHandler(DragEvent.DRAG_EXITED, event -> onDragEvent(event));
addEventHandler(DragEvent.DRAG_DROPPED, event -> onDragEvent(event));
addEventHandler(DragEvent.DRAG_DONE, event -> onDragEvent(event));
}
private void scrollY(double offset)
{
VirtualFlow<?> flow = ((VirtualFlow<?>) lookup("VirtualFlow"));
flow.adjustPixels(offset);
}
// endregion
}

Related

Scroll horizontally when dragging/reordering columns in the TableView

In JavaFX's TableView (and TreeTableView) it's really hard to reorder columns using drag & drop when the horizontal scrollbar is present, because the table doesn't scroll automatically when one want's to drag the column to the currently not visible (off the scroll pane viewport) position.
I've noticed that there are already a bug (enhancement) reports for this:
https://bugs.openjdk.java.net/browse/JDK-8092314
https://bugs.openjdk.java.net/browse/JDK-8092355
https://bugs.openjdk.java.net/browse/JDK-8213739
... but as it haven't been tackled for quite some time I am wondering whether there is any other way to achieve the same behavior using the current API.
There is SSCCE:
public class TableViewColumnReorderDragSSCCE extends Application {
public static final int NUMBER_OF_COLUMNS = 30;
public static final int MAX_WINDOW_WIDTH = 480;
#Override
public void start(Stage stage) {
stage.setScene(new Scene(createTable()));
stage.show();
stage.setMaxWidth(MAX_WINDOW_WIDTH);
}
private TableView<List<String>> createTable() {
final TableView<List<String>> tableView = new TableView<>();
initColumns(tableView);
return tableView;
}
private void initColumns(TableView<List<String>> tableView) {
for (int i=0; i<NUMBER_OF_COLUMNS; i++) {
tableView.getColumns().add(new TableColumn<>("Column " + i));
}
tableView.getItems().add(Collections.emptyList());
}
}
Steps to reproduce:
Run the above SSCCE
Try to drag Column 0 after Column 29
I am after a fully functional solution (if any).
As no complete solution was provided I've came up with one of my own. I've introduced a (ColumnsOrderingEnhancer) implementation which will enhance the table view columns reordering by automatic scrolling (when needed).
Usage (with the table view defined in the above SSCCE):
// Enhance table view columns reordering
final ColumnsOrderingEnhancer<List<String>> columnsOrderingEnhancer = new ColumnsOrderingEnhancer<>(tableView);
columnsOrderingEnhancer.init();
ColumnsOrderingEnhancer implementation:
public class ColumnsOrderingEnhancer<T> {
private final TableView<T> tableView;
public ColumnsOrderingEnhancer(TableView<T> tableView) {
this.tableView = tableView;
}
public void init() {
tableView.skinProperty().addListener((observable, oldSkin, newSkin) -> {
// This can be done only when skin is ready
final TableHeaderRow header = (TableHeaderRow) tableView.lookup("TableHeaderRow");
final MouseDraggingDirectionHelper mouseDraggingDirectionHelper = new MouseDraggingDirectionHelper(header);
final ScrollBar horizontalScrollBar = getTableViewHorizontalScrollbar();
// This is the most important part which is responsible for scrolling table during the column dragging out of the viewport.
header.addEventFilter(MouseEvent.MOUSE_DRAGGED, event -> {
final double totalHeaderWidth = header.getWidth();
final double xMousePosition = event.getX();
final MouseDraggingDirectionHelper.Direction direction = mouseDraggingDirectionHelper.getLastDirection();
maybeChangeScrollBarPosition(horizontalScrollBar, totalHeaderWidth, xMousePosition, direction);
});
});
}
private void maybeChangeScrollBarPosition(ScrollBar horizontalScrollBar, double totalHeaderWidth, double xMousePosition, MouseDraggingDirectionHelper.Direction direction) {
if (xMousePosition > totalHeaderWidth && direction == RIGHT) {
horizontalScrollBar.increment();
}
else if (xMousePosition < 0 && direction == LEFT) {
horizontalScrollBar.decrement();
}
}
private ScrollBar getTableViewHorizontalScrollbar() {
Set<Node> scrollBars = tableView.lookupAll(".scroll-bar");
final Optional<Node> horizontalScrollBar =
scrollBars.stream().filter(node -> ((ScrollBar) node).getOrientation().equals(Orientation.HORIZONTAL)).findAny();
try {
return (ScrollBar) horizontalScrollBar.get();
}
catch (NoSuchElementException e) {
return null;
}
}
/**
* A simple class responsible for determining horizontal direction of the mouse during dragging phase.
*/
static class MouseDraggingDirectionHelper {
private double xLastMousePosition = -1;
private Direction direction = null;
MouseDraggingDirectionHelper(Node node) {
// Event filters that are determining when scrollbar needs to be incremented/decremented
node.addEventFilter(MouseEvent.MOUSE_PRESSED, event -> xLastMousePosition = event.getX());
node.addEventFilter(MouseEvent.MOUSE_DRAGGED, event -> {
direction = ((event.getX() - xLastMousePosition > 0) ? RIGHT : LEFT);
xLastMousePosition = event.getX();
});
}
enum Direction {
LEFT,
RIGHT
}
public Direction getLastDirection() {
return direction;
}
}
}
End result (which works surprisingly well):
It's not impossible to work around. You could start with something like this, though it is a very crude implementation, I'm sure in principle it can be refined to something reasonable:
tableView.setOnMouseExited(me -> {
if (me.isPrimaryButtonDown()) { // must be dragging
Bounds tvBounds = tableView.getBoundsInLocal();
double x = me.getX();
if (x < tvBounds.getMinX()) {
// Scroll to the left
tableView.scrollToColumnIndex(0);
} else if (x > tvBounds.getMaxX()) {
// Scroll to the right
tableView.scrollToColumnIndex(tableView.getColumns().size()-1);
}
}
});
In a proper implementation you would likely have to sneak around the Node hierarchy and find the width of the table columns and determine what the next out-of-view column is so you can scroll to the exact right column. Remember when you did that so you can do it again if the user continues to drag outside the table, but not too fast.
EDIT: Based on your self-answer, here is my take on it. I've compacted your code a bit and made it work on JavaFX 8.0:
static class TableHeaderScroller implements EventHandler<MouseEvent> {
private TableView tv;
private Pane header;
private ScrollBar scrollBar;
private double lastX;
public static void install(TableView tv) {
TableHeaderScroller ths = new TableHeaderScroller(tv);
tv.skinProperty().addListener(ths::skinListener);
}
private TableHeaderScroller(TableView tv) {
this.tv = tv;
}
private void skinListener(ObservableValue<? extends Skin<?>> observable, Skin<?> oldValue, Skin<?> newValue) {
if (header != null) {
header.removeEventFilter(MouseEvent.MOUSE_DRAGGED, this);
}
header = (Pane) tv.lookup("TableHeaderRow");
if (header != null) {
tv.lookupAll(".scroll-bar").stream().map(ScrollBar.class::cast)
.filter(sb -> sb.getOrientation() == Orientation.HORIZONTAL)
.findFirst().ifPresent( sb -> {
TableHeaderScroller.this.scrollBar = sb;
header.addEventFilter(MouseEvent.MOUSE_DRAGGED, TableHeaderScroller.this);
});
}
}
#Override
public void handle(MouseEvent event) {
double x = event.getX();
double sign = Math.signum(x - lastX);
lastX = x;
int dir = x < 0 ? -1 : x > header.getWidth() ? 1 : 0;
if (dir != 0 && dir == sign) {
if (dir < 0) {
scrollBar.decrement();
} else {
scrollBar.increment();
}
}
}
}

How do I check a key press in Java?

Basically, I'm learning Java and I'm trying to make a simple game in Netbeans using JavaFX. Right now I have a window set up with a rectangle drawn in it. (I have a canvas set up to draw on.) I have made a player class and global class, but I need to know how to read key inputs. I was taught by a friend who is REALLY good with Java, so the info he gives me is good. I read up on KeyEvent, but I have no clue how to implement it.
Any help is greatly appreciated.
I had the same question some weeks ago. The problem was about how to check at any time if a key is being held down or not. There are various solutions, I solved it by using a bitset:
package game.input;
import java.util.BitSet;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
public class Input {
/**
* Bitset which registers if any {#link KeyCode} keeps being pressed or if it is released.
*/
private BitSet keyboardBitSet = new BitSet();
// -------------------------------------------------
// default key codes
// will vary when you let the user customize the key codes or when you add support for a 2nd player
// -------------------------------------------------
private KeyCode upKey = KeyCode.UP;
private KeyCode downKey = KeyCode.DOWN;
private KeyCode leftKey = KeyCode.LEFT;
private KeyCode rightKey = KeyCode.RIGHT;
private KeyCode primaryWeaponKey = KeyCode.SPACE;
private KeyCode secondaryWeaponKey = KeyCode.CONTROL;
Scene scene;
public Input( Scene scene) {
this.scene = scene;
}
public void addListeners() {
scene.addEventFilter(KeyEvent.KEY_PRESSED, keyPressedEventHandler);
scene.addEventFilter(KeyEvent.KEY_RELEASED, keyReleasedEventHandler);
}
public void removeListeners() {
scene.removeEventFilter(KeyEvent.KEY_PRESSED, keyPressedEventHandler);
scene.removeEventFilter(KeyEvent.KEY_RELEASED, keyReleasedEventHandler);
}
/**
* "Key Pressed" handler for all input events: register pressed key in the bitset
*/
private EventHandler<KeyEvent> keyPressedEventHandler = new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
// register key down
keyboardBitSet.set(event.getCode().ordinal(), true);
}
};
/**
* "Key Released" handler for all input events: unregister released key in the bitset
*/
private EventHandler<KeyEvent> keyReleasedEventHandler = new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
// register key up
keyboardBitSet.set(event.getCode().ordinal(), false);
}
};
// -------------------------------------------------
// Evaluate bitset of pressed keys and return the player input.
// If direction and its opposite direction are pressed simultaneously, then the direction isn't handled.
// -------------------------------------------------
public boolean isMoveUp() {
return keyboardBitSet.get( upKey.ordinal()) && !keyboardBitSet.get( downKey.ordinal());
}
public boolean isMoveDown() {
return keyboardBitSet.get( downKey.ordinal()) && !keyboardBitSet.get( upKey.ordinal());
}
public boolean isMoveLeft() {
return keyboardBitSet.get( leftKey.ordinal()) && !keyboardBitSet.get( rightKey.ordinal());
}
public boolean isMoveRight() {
return keyboardBitSet.get( rightKey.ordinal()) && !keyboardBitSet.get( leftKey.ordinal());
}
public boolean isFirePrimaryWeapon() {
return keyboardBitSet.get( primaryWeaponKey.ordinal());
}
public boolean isFireSecondaryWeapon() {
return keyboardBitSet.get( secondaryWeaponKey.ordinal());
}
}

What does this warning mean in Vaadin: "Ignoring RPC call for disabled connector com.vaadin.ui.Window"?

I have tried to google to find some hints and look at the Vaadin's website, but I didn't find anything related to this king of issue:
In the console when I launch the application, I see this warning several times:
Ignoring RPC call for disabled connector com.vaadin.ui.Window, caption=Window's caption
I am using the Refresher addon which polls my server at an interval of 2000 millisec. and the ICEPush addon which implements pushing to the Vaadin UI.
I think that this is related somehow to the Refresher addon, because if I interact with a component I have created for test (below the code), warnings are added to the console.
Here is the code:
package com.example.events;
import java.util.ArrayList;
import java.util.List;
import com.github.wolfie.refresher.Refresher;
import com.vaadin.event.FieldEvents.BlurListener;
import com.vaadin.event.FieldEvents.FocusListener;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.event.LayoutEvents.LayoutClickNotifier;
import com.vaadin.ui.AbstractTextField.TextChangeEventMode;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.Layout;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
public class Noticeboard extends VerticalLayout {
/**
*
*/
private static final long serialVersionUID = -6023888496081433464L;
private static List<Note> notes = new ArrayList<Note>();
private List<Window> windows = new ArrayList<Window>();
private static int userCount;
private int userId;
private static int noteId;
private Refresher refresher = new Refresher();
private static final int UPDATE_INTERVAL = 2000;
private Note currentlyFocusedNote;
public class NoticeboardUpdater extends Thread {
#Override
public void run() {
while (true) {
try {
Thread.sleep(UPDATE_INTERVAL);
} catch (Exception e) {
e.printStackTrace();
}
getUI().getSession().getLockInstance().lock();
try {
updateNoticeboard();
} finally {
getUI().getSession().getLockInstance().unlock();
}
}
}
}
public Noticeboard() {
refresher.setRefreshInterval(UPDATE_INTERVAL);
userId = ++userCount;
setSpacing(true);
setMargin(true);
addComponent(new Label("Logged in as User " + userId));
Button addNoteButton = new Button("Add note");
addNoteButton.addClickListener(new ClickListener() {
/**
*
*/
private static final long serialVersionUID = -4927018162887570343L;
#Override
public void buttonClick(ClickEvent event) {
Note note = new Note(++noteId);
note.setCaption("Note " + note.getId());
notes.add(note);
Window window = createWindow(note);
windows.add(window);
UI.getCurrent().addWindow(window);
}
});
addComponent(addNoteButton);
addExtension(refresher);
new NoticeboardUpdater().start();
}
private Window createWindow(final Note note) {
final Window window = new Window(note.getCaption());
VerticalLayout layout = new VerticalLayout();
layout.addComponent(createContentNote(note, window));
window.setContent(layout);
window.setWidth(300, Unit.PIXELS);
window.setResizable(false);
window.setPositionX(note.getPositionX());
window.setPositionY(note.getPositionY());
window.setData(note);
window.addBlurListener(createBlurListener(window));
window.addFocusListener(createFocusListener(window));
LayoutClickNotifier mainLayout = (LayoutClickNotifier) getUI().getContent();
mainLayout.addLayoutClickListener(e -> {
if (note.isNoteFocusedWindow() || note.isNoteFocusedTextArea()) {
if (note.getLockedByUser() > -1 && note.getLockedByUser() == userId) {
unlockNote(getWindow(currentlyFocusedNote));
note.setNoteFocusedWindow(false);
note.setNoteBlurredWindow(false);
note.setNoteFocusedTextArea(false);
note.setNoteBlurredTextArea(false);
currentlyFocusedNote = null;
}
}
});
return window;
}
private TextArea createContentNote(final Note note, final Window window) {
TextArea contentNote = new TextArea();
contentNote.setSizeFull();
contentNote.setValue(note.getText());
contentNote.setImmediate(true);
contentNote.setTextChangeEventMode(TextChangeEventMode.EAGER);
contentNote.addBlurListener(createBlurListener(window));
contentNote.addFocusListener(createFocusListener(window));
contentNote.addTextChangeListener(new TextChangeListener() {
/**
*
*/
private static final long serialVersionUID = 8552875156973567499L;
#Override
public void textChange(TextChangeEvent event) {
note.setText(event.getText());
}
});
return contentNote;
}
private BlurListener createBlurListener(Window window) {
return e -> {
Component blurredComponent = e.getComponent();
if (blurredComponent == window) {
currentlyFocusedNote.setNoteBlurredWindow(true);
}
else if (blurredComponent == (((Layout) window.getContent())).iterator().next()) {
currentlyFocusedNote.setNoteBlurredTextArea(true);
}
};
}
private FocusListener createFocusListener(Window window) {
return e -> {
Component focusedComponent = e.getComponent();
Note note = (Note) window.getData();
if (currentlyFocusedNote != null && currentlyFocusedNote != note) {
unlockNote(getWindow(currentlyFocusedNote));
currentlyFocusedNote.setNoteFocusedWindow(false);
currentlyFocusedNote.setNoteBlurredWindow(false);
currentlyFocusedNote.setNoteFocusedTextArea(false);
currentlyFocusedNote.setNoteBlurredTextArea(false);
}
currentlyFocusedNote = note;
if (focusedComponent == window) {
Notification.show("Focused Note Window");
currentlyFocusedNote.setNoteFocusedWindow(true);
}
else if (focusedComponent == (((Layout) window.getContent())).iterator().next()) {
Notification.show("Focused Note TextArea");
currentlyFocusedNote.setNoteFocusedTextArea(true);
}
if (currentlyFocusedNote.isNoteFocusedWindow() && currentlyFocusedNote.isNoteBlurredTextArea() ||
currentlyFocusedNote.isNoteFocusedTextArea() && currentlyFocusedNote.isNoteBlurredWindow()) {
// Lock is already set here, skipping
return;
}
lockNote(window);
};
}
private void lockNote(Window window) {
Note note = (Note) window.getData();
note.setLockedByUser(userId);
String caption = "Locked by User " + userId;
note.setCaption(caption);
window.setCaption(caption);
}
private void unlockNote(Window window) {
Note note = (Note) window.getData();
note.setLockedByUser(-1);
note.setPositionX(window.getPositionX());
note.setPositionY(window.getPositionY());
note.setCaption("Note " + note.getId());
window.setCaption("Note " + note.getId());
}
private void updateNoticeboard() {
for (Note note : notes) {
Window window = getWindow(note);
if (window == null) {
window = createWindow(note);
windows.add(window);
UI.getCurrent().addWindow(window);
}
if (note.getLockedByUser() > -1) {
if (note.getLockedByUser() != userId) {
// If the note is locked by another user, then we disable this window.
window.setEnabled(false);
updateTextArea(window, note);
updateWindowPosition(window, note);
}
else {
// Otherwise the window is enabled.
window.setEnabled(true);
Note focusedNote = (Note) window.getData();
updateFocusedNotePosition(focusedNote, window);
}
}
else {
window.setEnabled(true);
updateTextArea(window, note);
updateWindowPosition(window, note);
}
}
}
private void updateTextArea(Window window, Note note) {
Layout layout = (Layout) window.getContent();
TextArea area = (TextArea) layout.iterator().next();
area.setValue(note.getText());
}
private Window getWindow(Note note) {
for (Window window : windows) {
if (window.getData().equals(note))
return window;
}
return null;
}
private void updateWindowPosition(Window window, Note note) {
window.setPositionX(note.getPositionX());
window.setPositionY(note.getPositionY());
window.setCaption(note.getCaption());
}
private void updateFocusedNotePosition(Note focusedNote, Window window) {
focusedNote.setPositionX(window.getPositionX());
focusedNote.setPositionY(window.getPositionY());
focusedNote.setCaption(window.getCaption());
}
}
And inside the UI's init method I simply use this Noticeboard class:
#Override
protected void init(VaadinRequest request) {
setContent(new Noticeboard());
}
When I move a window created with the "Add note" button or change the focus from a window to another, I experience the warning.
What could be the reason of such an issue?
I know, code is not of the better ones, it is just to see how the this Vaadin addons behave.
When you disable a component, the disabled state is handled both on the client and the server side, meaning, a component isn't just visually disabled, but the server also refuses to handle any requests to a disabled component.
The warning you are seeing means, that there is an RPC request (HTTP request from the client-side) that is targeted for a disabled component. Since the component is disabled, the RPC request will be ignored.
This typically happens when you have a background thread that disables a component and then also do polling from the client-side.

Problems with single-tap and press-and-hold with LWJGL

I've been researching a way to use LWJGL for my input system. I'm having problems detecting if it is a single-press or a press-and-hold. The event fires twice when I tap, instead of just once.
while(Keyboard.next())
{
if(Keyboard.getEventKeyState())
{
if(Keyboard.isRepeatEvent())
{
//Key held.
doAction(Keyboard.getEventKey(), true, false);
}
else
{
//Key pressed
doAction(Keyboard.getEventKey(), false, false);
}
}
else
{
//Fired when key is released.
doAction(Keyboard.getEventKey(), false, true);
}
}
Edit: I've both resolved the issue and modified this. Here you go, a modified version. (Dammit, Teamviewer..)
/**
* Updates all mouse info, keys bound, and performs actions.
*/
public static void tick()
{
mouseButtons[0] = Mouse.isButtonDown(0);
mouseButtons[1] = Mouse.isButtonDown(1);
mousePos[0] = Mouse.getX();
mousePos[1] = Mouse.getY();
while(Keyboard.next())
{
doAction(0, false);
if(Keyboard.getEventKeyState())
{
if(!Keyboard.isRepeatEvent())
{
doAction(Keyboard.getEventKey(), false);
}
}
else
{
doAction(Keyboard.getEventKey(), true);
}
}
while(Mouse.next())
{
}
}
/**
* Does the associated action for each key. Called automatically from tick.
* #param key The key to check & perform associated action
*/
public static void doAction(int key, boolean ifReleased)
{
if(mouseButtons[0])
{
}
if(mouseButtons[1])
{
}
if(key == 2 & !ifReleased)
{
System.out.println("a");
}
if(Keyboard.isKeyDown(3))
{
System.out.println("b");
}
}
I know it's been awhile since this question was asked, but I came up with a solution myself. My InputHelper lets you determine if a key or mouse button is pressed, released, or held down, and can be accessed from anyother class without initializing and sharing the same instance of it.
It works by having 2 arrays, 1 for mouse events, 1 for keyboard events, that each store one enum value for each key. If there is a button or key event, when updated, the update function sets the value in the appropriate array for that button/key to a certain enum. Then, the next time it is updated, it sets all the key and button events to no event, and repeats the process, handling any new events.
/*
* Handles mouse and keyboard input and stores values for keys
* down, released, or pressed, that can be accessed from anywhere.
*
* To update the input helper, add this line into the main draw loop:
* InputHelper.update();
*
* Use as so (can be used from anywhere):
* InputHelper.isKeyDown(Keyboard.KEY_SPACE);
*/
import java.util.ArrayList;
import org.lwjgl.input.*;
/**
*
* #author Jocopa3
*/
public class InputHelper {
private static InputHelper input = new InputHelper(); //Singleton class instance
private enum EventState {
NONE,PRESSED,DOWN,RELEASED;
}
private ArrayList<EventState> mouseEvents;
private ArrayList<EventState> keyboardEvents;
public InputHelper(){
//Mouse initialization
mouseEvents = new ArrayList<EventState>();
//Add mouse events to Array list
for(int i = 0; i < Mouse.getButtonCount(); i++) {
mouseEvents.add(EventState.NONE);
}
//Keyboard initialization
keyboardEvents = new ArrayList<EventState>();
//Add keyboard events to Array list
for(int i = 0; i < Keyboard.KEYBOARD_SIZE; i++) {
keyboardEvents.add(EventState.NONE);
}
}
private void Update(){
resetKeys(); //clear Keyboard events
//Set Key down events (more accurate than using repeat-event method)
for(int i = 0; i < Keyboard.KEYBOARD_SIZE;; i++){
if(Keyboard.isKeyDown(i))
keyboardEvents.set(i, EventState.DOWN);
}
while(Keyboard.next()){ //Handle all Keyboard events
int key = Keyboard.getEventKey();
if(key<0) continue; //Ignore no events
if(Keyboard.getEventKeyState()){
if(!Keyboard.isRepeatEvent()){
keyboardEvents.set(key, EventState.PRESSED);
}
}else{
keyboardEvents.set(key, EventState.RELEASED);
}
}
resetMouse(); //clear Mouse events
//Set Mouse down events
for(int i = 0; i < Mouse.getButtonCount(); i++){
if(Mouse.isButtonDown(i))
mouseEvents.set(i, EventState.DOWN);
}
while (Mouse.next()){ //Handle all Mouse events
int button = Mouse.getEventButton();
if(button<0) continue; //Ignore no events
if (Mouse.getEventButtonState()) {
mouseEvents.set(button, EventState.PRESSED);
}else {
mouseEvents.set(button, EventState.RELEASED);
}
}
}
//Set all Keyboard events to false
private void resetKeys(){
for(int i = 0; i < Keyboard.KEYBOARD_SIZE;; i++) {
keyboardEvents.set(i, EventState.NONE);
}
}
//Set all Mouse events to false
private void resetMouse(){
for(int i = 0; i < Mouse.getButtonCount(); i++) {
mouseEvents.set(i, EventState.NONE);
}
}
//Non-static version of methods (Only used in the singleton instance)
private boolean KeyDown(int key){
return keyboardEvents.get(key)==EventState.DOWN;
}
private boolean KeyPressed(int key){
return keyboardEvents.get(key)==EventState.PRESSED;
}
private boolean KeyReleased(int key){
return keyboardEvents.get(key)==EventState.RELEASED;
}
private boolean MouseButtonDown(int key){
return mouseEvents.get(key)==EventState.DOWN;
}
private boolean MouseButtonPressed(int key){
return mouseEvents.get(key)==EventState.PRESSED;
}
private boolean MouseButtonReleased(int key){
return mouseEvents.get(key)==EventState.RELEASED;
}
//Static version of methods (called from anywhere, return singleton instance value)
public static boolean isKeyDown(int key){
return input.KeyDown(key);
}
public static boolean isKeyPressed(int key){
return input.KeyPressed(key);
}
public static boolean isKeyReleased(int key){
return input.KeyReleased(key);
}
public static boolean isButtonDown(int key){
return input.MouseButtonDown(key);
}
public static boolean isButtonPressed(int key){
return input.MouseButtonPressed(key);
}
public static boolean isButtonReleased(int key){
return input.MouseButtonReleased(key);
}
public static void update(){
input.Update();
}
}
It has to be updated every frame manually, so your main draw loop you should add the line InputHelper.update(); like this:
while(!Display.isCloseRequested()) {
InputHelper.update(); //Should go before other code that uses the inputs
//Rest of code here
}
Once you have it setup to update every frame, you can use it anywhere you need to determine input states for Mouse or Key buttons as so:
//Mouse test
if(InputHelper.isButtonPressed(0))
System.out.println("Left Mouse button pressed");
if(InputHelper.isButtonDown(0))
System.out.println("Left Mouse button down");
if(InputHelper.isButtonReleased(0))
System.out.println("Left Mouse button released");
//Keyboard Test
if(InputHelper.isKeyPressed(Keyboard.KEY_SPACE))
System.out.println("Space key pressed");
if(InputHelper.isKeyDown(Keyboard.KEY_SPACE))
System.out.println("Space key down");
if(InputHelper.isKeyReleased(Keyboard.KEY_SPACE))
System.out.println("Space key released");

Can i use JCheckbox to show "mixed state"

In windows it is possible to show a grayed out JCheckbox, to show that the collection of data which it represents not all items have the same value.
Is this even possible with a JCheckBox?
How do i go about this?
(Hoping there's a way to not override it)
Thanks
JIDE Common Layer has a TristateCheckBox.
It's possible with some of work.
I have this code from some years ago. Is based in some examples I found in internet, but I cannot find any reference to the original creator, so I apologize
import javax.swing.*;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.ActionMapUIResource;
import java.awt.event.*;
/**
* Maintenance tip - There were some tricks to getting this code
* working:
*
* 1. You have to overwite addMouseListener() to do nothing
* 2. You have to add a mouse event on mousePressed by calling
* super.addMouseListener()
* 3. You have to replace the UIActionMap for the keyboard event
* "pressed" with your own one.
* 4. You have to remove the UIActionMap for the keyboard event
* "released".
* 5. You have to grab focus when the next state is entered,
* otherwise clicking on the component won't get the focus.
* 6. You have to make a TristateDecorator as a button model that
* wraps the original button model and does state management.
*/
public class TristateCheckBox extends JCheckBox {
/** This is a type-safe enumerated type */
public static class State { private State() { } }
public static final State NOT_SELECTED = new State();
public static final State SELECTED = new State();
public static final State DONT_CARE = new State();
private final TristateDecorator model;
public TristateCheckBox(String text, Icon icon, State initial){
super(text, icon);
// Add a listener for when the mouse is pressed
super.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
grabFocus();
model.nextState();
}
});
// Reset the keyboard action map
ActionMap map = new ActionMapUIResource();
map.put("pressed", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
grabFocus();
model.nextState();
}
});
map.put("released", null);
SwingUtilities.replaceUIActionMap(this, map);
// set the model to the adapted model
model = new TristateDecorator(getModel());
setModel(model);
setState(initial);
}
public TristateCheckBox(String text, State initial) {
this(text, null, initial);
}
public TristateCheckBox(String text) {
this(text, DONT_CARE);
}
public TristateCheckBox() {
this(null);
}
/** No one may add mouse listeners, not even Swing! */
public void addMouseListener(MouseListener l) { }
/**
* Set the new state to either SELECTED, NOT_SELECTED or
* DONT_CARE. If state == null, it is treated as DONT_CARE.
*/
public void setState(State state) { model.setState(state); }
/** Return the current state, which is determined by the
* selection status of the model. */
public State getState() { return model.getState(); }
public void setSelected(boolean b) {
if (b) {
setState(SELECTED);
} else {
setState(NOT_SELECTED);
}
}
/**
* Exactly which Design Pattern is this? Is it an Adapter,
* a Proxy or a Decorator? In this case, my vote lies with the
* Decorator, because we are extending functionality and
* "decorating" the original model with a more powerful model.
*/
private class TristateDecorator implements ButtonModel {
private final ButtonModel other;
private TristateDecorator(ButtonModel other) {
this.other = other;
}
private void setState(State state) {
if (state == NOT_SELECTED) {
other.setArmed(false);
setPressed(false);
setSelected(false);
} else if (state == SELECTED) {
other.setArmed(false);
setPressed(false);
setSelected(true);
} else { // either "null" or DONT_CARE
other.setArmed(true);
setPressed(true);
setSelected(true);
}
}
/**
* The current state is embedded in the selection / armed
* state of the model.
*
* We return the SELECTED state when the checkbox is selected
* but not armed, DONT_CARE state when the checkbox is
* selected and armed (grey) and NOT_SELECTED when the
* checkbox is deselected.
*/
private State getState() {
if (isSelected() && !isArmed()) {
// normal black tick
return SELECTED;
} else if (isSelected() && isArmed()) {
// don't care grey tick
return DONT_CARE;
} else {
// normal deselected
return NOT_SELECTED;
}
}
/** We rotate between NOT_SELECTED, SELECTED and DONT_CARE.*/
private void nextState() {
State current = getState();
if (current == NOT_SELECTED) {
setState(SELECTED);
} else if (current == SELECTED) {
setState(DONT_CARE);
} else if (current == DONT_CARE) {
setState(NOT_SELECTED);
}
}
/** Filter: No one may change the armed status except us. */
public void setArmed(boolean b) {
}
/** We disable focusing on the component when it is not
* enabled. */
public void setEnabled(boolean b) {
setFocusable(b);
other.setEnabled(b);
}
/** All these methods simply delegate to the "other" model
* that is being decorated. */
public boolean isArmed() { return other.isArmed(); }
public boolean isSelected() { return other.isSelected(); }
public boolean isEnabled() { return other.isEnabled(); }
public boolean isPressed() { return other.isPressed(); }
public boolean isRollover() { return other.isRollover(); }
public void setSelected(boolean b) { other.setSelected(b); }
public void setPressed(boolean b) { other.setPressed(b); }
public void setRollover(boolean b) { other.setRollover(b); }
public void setMnemonic(int key) { other.setMnemonic(key); }
public int getMnemonic() { return other.getMnemonic(); }
public void setActionCommand(String s) {
other.setActionCommand(s);
}
public String getActionCommand() {
return other.getActionCommand();
}
public void setGroup(ButtonGroup group) {
other.setGroup(group);
}
public void addActionListener(ActionListener l) {
other.addActionListener(l);
}
public void removeActionListener(ActionListener l) {
other.removeActionListener(l);
}
public void addItemListener(ItemListener l) {
other.addItemListener(l);
}
public void removeItemListener(ItemListener l) {
other.removeItemListener(l);
}
public void addChangeListener(ChangeListener l) {
other.addChangeListener(l);
}
public void removeChangeListener(ChangeListener l) {
other.removeChangeListener(l);
}
public Object[] getSelectedObjects() {
return other.getSelectedObjects();
}
}
}
My colleague who this question came from thought of this;
Create a dummy JCheckBox which is disabled and selected. set the same size as the real one.
Create an Icon which' paint method actually paints the dummy JCheckbox.
Set the original JCheckBox' Icon to the one painting the dummy.
Remove the icon as soon as the JCheckBox is clicked.
++ No overridden JCheckBox
-- not a real tri-state Combo
I think he's satisfied.
Thanks for the help

Categories