Components inside List in CodenameOne - java

my question is. Is possible to add a component like a button (button has a functionality that triggered when it is clicked) inside a list component?
This image explain better what I refer:
http://2.bp.blogspot.com/-HThpKcgDyRA/URI_FdpffMI/AAAAAAAAAUI/SficZAPXaCw/s1600/1.png

Yes but it requires some handcoding and it will only work for touch (since you won't be able to assign focus to it).
We normally recommend just using Component/Container hierarchies for these cases rather than dealing with lists but obviously this isn't always practical.
The key is to always use the list action listener to trigger events, nothing else. So when you are in the action handling code of the list you would want to know if it was triggered by your button...
If you are in the GUI builder this is pretty easy:
Button b = ((GenericListCellRenderer)list.getRenderer()).extractLastClickedComponent();
if(b != null && b == myButton) {
// your event code here for the button, the selected entry is list.getSelectedItem()/Index()
}
The handcoded approach is pretty similar with one major caveat, you don't have the extractLastClickedComponent method. So assuming you have a component within the renderer just add an action listener to it. Within the action listener just set a flag e.g.:
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
buttonWasClicked = true;
}
});
// within the list listener we do the exact same thing:
if(buttonWasClicked) {
// for next time...
buttonWasClicked = false;
// your event code here for the button, the selected entry is list.getSelectedItem()/Index()
}

Yes!!! Try it, this is pretty easy:....
http://www.codenameone.com/how-do-i---create-a-list-of-items-the-easy-way.html
http://www.codenameone.com/how-do-i---create-a-list-of-items-the-hard-way-gui-builder-renderer.html

Related

How to differentiate between a setSelectedItem call and an actionPerformed methods in JComboBox

I have a method which is called at a event of a value change in a JComboBox
public void actionPerformed( ActionEvent e )
{
Object source = e.getSource();
if( source.equals( listComboBox ) )
{
changeList();
}
}
public void changeList()
{ //do some stuff
.....
.....
//warn the user
}
My problem is that this method is called at the initial load method as well.
In the method there is a warning message pop up.
Which is only needed when the user is changing the selected value of the combo box.(Not when i set a value from the code at the loading method.)
Is there a way(flag,different method) to distinguish between these to actions and give the warning message only when its needed?
My problem is that this method is called at the initial load method as well.
So add the listener to the combo box AFTER the load is finished.
You have two options. The first one is to apply a unique listener to each component and handle the event differently ( Which I prefer in most cases cause it keeps code simple for each component and way better to read).
The Second one is to identify which component trigger the event. One way to check that is by taking the sources of the event and check if it matches with the actual object or you can just check if it is 'instanceof' JComboBox and then handle the event as you pleased.
if(sourceObject == myComboBox ){
// handle the event
}
or
if(sourceObject instanceof JComboBox{
// then we are dealing with a combobox
// if it's the only one then you know what to do..
}

FocusListener for all controls of a Composite

I've written code for a Composite containing multiple controls (StyledTexts, Buttons, etc.).
I wanted to implement a FocusListener for the whole Composite (not only for one of the controls inside of it), but the FocusListener did not do anything.
I tried to implement it myself, and my attempt was: Add a FocusListener to every Control inside the composite and handle these events.
While trying to do so, I found some problems:
When two controls have a FocusListener and I change the focus from one to the other, the FocusLost event is fired before the FocusGained
So I can't find out if the focus was given to one of the other controls in my composite, or if the composite lost the focus completely.
Note: I tried using Display.getFocusControl() inside the focusLost(...) method, but it only returns the control from which the focus is taken!
My question: Is it possible to find out which control will receive the focus next while being inside the focusLost(...) method?
If not: Is there any other way to implement a FocusListener for a Composite?
At the time the focusLost() event is sent, it isn't yet known which control (if any) will receive the focus.
You can add a display filter that will inform you whenever a control within your entire application gains focus. Within your ' Listener` implementation, you can save the text input whenever a control gains focus that is not contained in the composite. For example:
Listener listener = new Listener() {
#Override
public void handleEvent( Event event ) {
if( event.widget != text || event.widget != fontButton || ... ) {
save();
}
}
};
display.addFilter( SWT.FocusIn, listener );
Make sure to add the listener only while the observed composite is alive. Remove the filter when the composite is disposed of.
display.removeFilter( SWT.FocusIn, listener );
#RüdigerHermanns works pretty good, but for the sake of completeness I'll post my solution here too:
I've written listeners for SWT.Activate and SWT.Deactivate in my composite:
public MyComposite(Composite parent, int style)
{
super(parent, style);
createGuiElements(this);
this.addListener(SWT.Deactivate, new Listener()
{
#Override
public void handleEvent(Event event)
{
for (FocusListener listener : focusListeners) listener.focusLost(new FocusEvent(event));
}
});
this.addListener(SWT.Activate, new Listener()
{
#Override
public void handleEvent(Event event)
{
for (FocusListener listener : focusListeners) listener.focusGained(new FocusEvent(event));
}
});
}
which notify all focusListeners on the Composite.

Nasa World Wind: SelectListener not triggering on map clicks

I'm using java WWsdk.
I am expecting the SelectListener to respond to user clicks on the map when user clicks just the map (i.e. not an icon or placemark,etc..).
It works fine for me when i click on my objects, but it doesn't trigger when i click "empty space". i.e. like water/land.
The docs for SelectListener says
If no object is under the cursor but the cursor is over terrain, the select event will >identify the terrain as the picked object and will include the corresponding geographic >position
This statement makes it sound like i should get an event whenever i don't click an object,but i don't get this.
Am i supposed to add some other kind of layer to get clicks on map to trigger select events?
I use this which is working for me for actions i need performed on objects that get clicked:
this.worldWindowGLCanvas1.addSelectListener(new SelectListener()
{
public void selected(SelectEvent event)
{
//Never goes here for clicks on map, just clicks
//on objects i have already created.
doStuff();
}
}
Add a MouseListener using addMouseListener():
this.worldWindowGLCanvas1.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
doStuff();
}
...
}
You may also want to add the listener to the AWTInputHandler instead of the WorldWindowGLCanvas if you want to prevent World Wind from doing something else with the click. More details in this question.

How do you get the current keyboard state globally? (i.e. what keys are currently depressed regardless of if the inquiring application has focus)

I'm writing a screen capture utility, and I'd like to be able to store the current state of the keyboard and mouse whenever a screenshot is taken.
Doing this for the mouse is simple, since using the PointerInfo class in a manner described in this related question gives you the screen coordinates for the current mouse location and the click information if desired. However, I haven't been able to find an analog to this class for the keyboard; all the keyboard related classes appear to be focus specific.
So, is there a way to get the current keyboard state in Java?
P.S. Remember that I'm looking for a function to call to retrieve the information regarding what keys are depressed, not a listener for such depression events.
Something you can do is implement the KeyListener interface and give states to all the keys you're interested in.
If you're interested in checking if the arrow keys are depressed upon a screenshot, for instance, you can implement this KeyListener interface and override keyPressed() and keyReleased() methods and set the state for those keys you are interested in to keyPressed or keyReleased. Depending on the event. That way, when the screenshot occurs, you can just read the state of those keys
If you need this solution to be global, regardless of application focus, you could write a small hook in C that you can integrate with Java Native Interface to listen for key events. Java doesn't let you listen to key events without you attaching the listener to a component and that component having focus. Have a look at JNativeHook.
If you just need it when your application has focus but on every component you could inelegantly attach the listener to all your components or you could write your own custom KeyEventDispatcher and register it on the KeyBoardFocusManager. That way, as long as your application has focus, regardless of component that has specific focus, you could catch all keyboard events. See:
public class YourFrame extends JFrame {
public YourFrame() {
// Finish all your layout and add your components
//
// Get the KeyboardFocusManager and register your custom dispatcher
KeyboardFocusManager m = KeyboardFocusManager.getCurrentKeyboardFocusManager();
m.addKeyEventDispatcher(new YourDispatcher());
}
private class YourDispatcher implements KeyEventDispatcher {
#Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_TYPED) {
// Do something to change the state of the key
} else if (e.getID() == KeyEvent.KEY_PRESSED) {
// Do something else
}
return false;
}
}
public static void main(String[] args) {
YourFrame yF = new YourFrame();
yF.pack();
yF.setVisible(true);
}
}

MouseListener fired without checking JCheckBox

This one is pretty crazy:
I've got an AppSight recording (for those not familiar, it's a recording of what they did including keyboard/mouse input + network traffic, etc) of a customer reproducing a bug. Basically, we've got a series of items listed on the screen with JCheckBox-es down the left side. We've got a MouseListener set for the JPanel that looks something like this:
private MouseAdapter createMouseListener() {
return new MouseAdapter(){
public void mousePressed( MouseEvent e ) {
if( e.getComponent() instanceof JCheckBox ) {
// Do stuff
}
}
};
}
Based on the recording, it appears very strongly that they click just above one of the checkboxes. After that, it's my belief that this listener fired and the "Do stuff" block happened. However, it did NOT check the box. The user then saw that the box was unchecked, so they clicked on it. This caused the "Do stuff" block to fire again, thus undoing what it had done the first time. This time, the box was checked. Therefore, the user thinks that the box is checked, and it looks like it is, but our client thinks that the box is unchecked as it was clicked twice.
Is this possible at all? For the life of me, I can't reproduce it or see how it could be possible, but based on the recording and the data the client sent to the server, I can't see any other logical explanation.
Any help, thoughts, and or ideas would be much appreciated.
Maybe the user clicked on the checkbox, but before release the mouse button, he moved the mouse away from the component. I'm pretty sure the chechbox won't get checked in this case, although not so sure about the Event Thread behaviour under this scenario.
I don't think you can assume that because the mouse was pressed on the checkbox that it will be also released on the checkbox. Maybe just do something like this:
private MouseAdapter createMouseListener() {
return new MouseAdapter(){
public void mouseReleased( MouseEvent e ) {
if( e.getComponent() instanceof JCheckBox ) {
// And just to be sure....
JCheckBox jcb = (JCheckBox) e.getComponent();
if(jcb.isSelected())
{
// Do stuff
}
}
}
};
}

Categories