I run LWUITDemo, Some UI can not be shown successful.All of them are TextArea contained by Form.If I change TextArea to Label, it work well.
Sorry, I run it in nokia s40 sdk 2.0. When I run most of codes that include TextArea, exception ocurred;
The Code Like That(From LWUITDemo):
Form aboutForm = new Form("About");
aboutForm.setScrollable(true);
aboutForm.setLayout(new BorderLayout());
TextArea aboutText = new TextArea(getAboutText(), 5, 10);
aboutText.setEditable(false);
aboutForm.addComponent(BorderLayout.CENTER, aboutText);
aboutForm.show();
When I run it, it faild:
Form: showModal
java.lang.NullPointerException
at com.sun.lwuit.TextArea.shouldShowHint(+21)
at com.sun.lwuit.TextArea.calcPreferredSize(+4)
at com.sun.lwuit.Component.preferredSize(+63)
...
You Could Check below Code:
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.layouts.BorderLayout;
import javax.microedition.midlet.*;
public class TextMidlet extends MIDlet {
private Form aboutForm;
public TextMidlet() {
Display.init(this);
aboutForm = new Form();
aboutForm.setScrollable(true);
aboutForm.setLayout(new BorderLayout());
}
public void startApp() {
TextArea aboutText = new TextArea("hiiiiiiiiiiiiii", 5, 10);
aboutText.setEditable(false);
aboutForm.addComponent(BorderLayout.CENTER, aboutText);
aboutForm.show();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
The code looks good to me. Please check that getAboutText() returns a String and does not return null.
If this does not help, you can use the LWUIT-Sources to debug your code. Set a breakpoint at TextArea.shouldShowHint and find out what it is that is null.
Check
import com.sun.lwuit.TextArea;
so i'm trying to set up an application where i have multiple panels inside a jframe. lets say 3 of them are purely for display purposes, and one of them is for control purposes. i'm using a borderLayout but i don't think the layout should really affect things here.
my problem is this: i want the repainting of the three display panels to be under the control of buttons in the control panel, and i want them to all execute in sync whenever a button on the control panel is pressed. to do this, i set up this little method :
public void update(){
while(ButtonIsOn){
a.repaint();
b.repaint()
c.repaint();
System.out.println("a,b, and c should have repainted");
}
}
where a,b, and c are all display panels and i want a,b,and c to all repaint continously until i press the button again. the problem is, when i execute the loop, the message prints in an infinite loop, but none of the panels do anything, ie, none of them repaint.
i've been reading up on the event dispatch thread and swing multithreading, but nothing i've found so far has really solved my problem. could someone give me the gist of what i'm doing wrong here, or even better, some sample code that handles the situation i'm describing? thanks...
The java.util.concurrent package provides very powerful tools for concurrent programing.
In the code below, I make use of a ReentrantLock (which works much like the Java synchronized keyword, ensuring mutually exclusive access by multiple threads to a single block of code). The other great thing which ReentrantLock provides are Conditions, which allow Threads to wait for a particular event before continuing.
Here, RepaintManager simply loops, calling repaint() on the JPanel. However, when toggleRepaintMode() is called, it blocks, waiting on the modeChanged Condition until toggleRepaintMode() is called again.
You should be able to run the following code right out of the box. Pressing the JButton toggle repainting of the JPanel (which you can see working by the System.out.println statements).
In general, I'd highly recommend getting familiar with the capabilities that java.util.concurrent offers. There's lots of very powerful stuff there. There's a good tutorial at http://docs.oracle.com/javase/tutorial/essential/concurrency/
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RepaintTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel()
{
#Override
public void paintComponent( Graphics g )
{
super.paintComponent( g );
// print something when the JPanel repaints
// so that we know things are working
System.out.println( "repainting" );
}
};
frame.add( panel );
final JButton button = new JButton("Button");
panel.add(button);
// create and start an instance of our custom
// RepaintThread, defined below
final RepaintThread thread = new RepaintThread( Collections.singletonList( panel ) );
thread.start();
// add an ActionListener to the JButton
// which turns on and off the RepaintThread
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
thread.toggleRepaintMode();
}
});
frame.setSize( 300, 300 );
frame.setVisible( true );
}
public static class RepaintThread extends Thread
{
ReentrantLock lock;
Condition modeChanged;
boolean repaintMode;
Collection<? extends Component> list;
public RepaintThread( Collection<? extends Component> list )
{
this.lock = new ReentrantLock( );
this.modeChanged = this.lock.newCondition();
this.repaintMode = false;
this.list = list;
}
#Override
public void run( )
{
while( true )
{
lock.lock();
try
{
// if repaintMode is false, wait until
// Condition.signal( ) is called
while ( !repaintMode )
try { modeChanged.await(); } catch (InterruptedException e) { }
}
finally
{
lock.unlock();
}
// call repaint on all the Components
// we're not on the event dispatch thread, but
// repaint() is safe to call from any thread
for ( Component c : list ) c.repaint();
// wait a bit
try { Thread.sleep( 50 ); } catch (InterruptedException e) { }
}
}
public void toggleRepaintMode( )
{
lock.lock();
try
{
// update the repaint mode and notify anyone
// awaiting on the Condition that repaintMode has changed
this.repaintMode = !this.repaintMode;
this.modeChanged.signalAll();
}
finally
{
lock.unlock();
}
}
}
}
jComponent.getTopLevelAncestor().repaint();
You could use SwingWorker for this. SwingWorker was designed to perform long running tasks in the background without blocking the event dispatcher thread. So, you need to extend SwingWorker and implement certain methods that will make sense to you. Note that all long running action should happen in the doInBackground() method, and the Swing UI elements should be updated only on the done() method.
So here is an example :
class JPanelTask extends SwingWorker<String, Object>{
JPanel panel = null;
Color bg = null;
public JPanelTask(JPanel panel){
this.panel = panel;
}
#Override
protected String doInBackground() throws Exception {
//loooong running computation.
return "COMPLETE";
}
#Override
protected void done() {
panel.repaint();
}
}
Now, in your "control" button's action performed event, you could do the following :
controlButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
JPanelTask task1 = new JPanelTask(panel1);
task1.execute();
JPanelTask task2 = new JPanelTask(panel2);
task2.execute();
//so on..
}
});
Another way is using javax.swing.Timer. Timer helps you to fire a change to your ui elements in a timely fasthion.This may not be the most appropriate solution. But it gets the work done too.
Again you should be careful about updating UI elements in right places.
I cannot seem to find any documentation of what events fire and when in GXT.
The API docs have lists of all the events that could fire (in Events). And it describes how to handle events that you catch. But I'm interested in the opposite side, which events are fired when I take a certain action.
I can set some listeners for various different components, or I can use addListener with a specific event code to catch individual events. That's spotty, and I seem to be using trial-and-error to guess what I might want to catch.
Is there a way to log all the events that are firing? Or catch all of them so I could look at them in a debugger?
Or is there some documentation I am missing that has the information? Something along the lines of "when you click on a widget, a ButtonEvent is fired. Events.x is fired on the hover, Events.y on the click."
Maybe someone will find this useful, I've created utility class for seeing what kind of events are risen. The idea of course was proposed in accepted answer.
import java.util.HashMap;
import java.util.Map;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Component;
/**
* Class for debugging purposes. Sometimes it is hard to tell what type of event
* is invoked and when. During debug process you can just do:
*
* EventUtils.attachDebugListeners(c);
* EventUtils.attachDebugListeners(c, "NAME");
*
* You'll then get information about events as they are invoked.
*
* List of events copied from {#link Events} class.
*
*/
public class EventUtils {
public static void attachDebugListeners(final Component c) {
attachDebugListeners(c, null);
}
public static void attachDebugListeners(final Component c, final String msg) {
for (final EventType type : eventTypeNames.keySet()) {
c.addListener(type, new Listener<BaseEvent>() {
#Override
public void handleEvent(BaseEvent be) {
String typeName = eventTypeNames.get(type);
if (msg != null)
System.out.print(msg + " -> ");
System.out.println(typeName);
}
});
}
}
final static Map<EventType, String> eventTypeNames = new HashMap<EventType, String>();
static {
eventTypeNames.put(Events.Activate, "Events.Activate");
eventTypeNames.put(Events.Add, "Events.Add");
eventTypeNames.put(Events.Adopt, "Events.Adopt");
eventTypeNames.put(Events.AfterEdit, "Events.AfterEdit");
eventTypeNames.put(Events.AfterLayout, "Events.AfterLayout");
eventTypeNames.put(Events.ArrowClick, "Events.ArrowClick");
eventTypeNames.put(Events.Attach, "Events.Attach");
eventTypeNames.put(Events.AutoHide, "Events.AutoHide");
eventTypeNames.put(Events.BeforeAdd, "Events.BeforeAdd");
eventTypeNames.put(Events.BeforeAdopt, "Events.BeforeAdopt");
eventTypeNames.put(Events.BeforeBind, "Events.BeforeBind");
eventTypeNames.put(Events.BeforeCancelEdit, "Events.BeforeCancelEdit");
eventTypeNames.put(Events.BeforeChange, "Events.BeforeChange");
eventTypeNames
.put(Events.BeforeCheckChange, "Events.BeforeCheckChange");
eventTypeNames.put(Events.BeforeClose, "Events.BeforeClose");
eventTypeNames.put(Events.BeforeCollapse, "Events.BeforeCollapse");
eventTypeNames.put(Events.BeforeComplete, "Events.BeforeComplete");
eventTypeNames.put(Events.BeforeEdit, "Events.BeforeEdit");
eventTypeNames.put(Events.BeforeExpand, "Events.BeforeExpand");
eventTypeNames.put(Events.BeforeHide, "Events.BeforeHide");
eventTypeNames.put(Events.BeforeLayout, "Events.BeforeLayout");
eventTypeNames.put(Events.BeforeOpen, "Events.BeforeOpen");
eventTypeNames.put(Events.BeforeOrphan, "Events.BeforeOrphan");
eventTypeNames.put(Events.BeforeQuery, "Events.BeforeQuery");
eventTypeNames.put(Events.BeforeRemove, "Events.BeforeRemove");
eventTypeNames.put(Events.BeforeRender, "Events.BeforeRender");
eventTypeNames.put(Events.BeforeSelect, "Events.BeforeSelect");
eventTypeNames.put(Events.BeforeShow, "Events.BeforeShow");
eventTypeNames.put(Events.BeforeStartEdit, "Events.BeforeStartEdit");
eventTypeNames.put(Events.BeforeStateRestore,
"Events.BeforeStateRestore");
eventTypeNames.put(Events.BeforeStateSave, "Events.BeforeStateSave");
eventTypeNames.put(Events.BeforeSubmit, "Events.BeforeSubmit");
eventTypeNames.put(Events.Bind, "Events.Bind");
eventTypeNames.put(Events.Blur, "Events.Blur");
eventTypeNames.put(Events.BodyScroll, "Events.BodyScroll");
eventTypeNames.put(Events.BrowserEvent, "Events.BrowserEvent");
eventTypeNames.put(Events.CancelEdit, "Events.CancelEdit");
eventTypeNames.put(Events.CellClick, "Events.CellClick");
eventTypeNames.put(Events.CellDoubleClick, "Events.CellDoubleClick");
eventTypeNames.put(Events.CellMouseDown, "Events.CellMouseDown");
eventTypeNames.put(Events.CellMouseUp, "Events.CellMouseUp");
eventTypeNames.put(Events.Change, "Events.Change");
eventTypeNames.put(Events.CheckChange, "Events.CheckChange");
eventTypeNames.put(Events.CheckChanged, "Events.CheckChanged");
eventTypeNames.put(Events.Clear, "Events.Clear");
eventTypeNames.put(Events.Close, "Events.Close");
eventTypeNames.put(Events.Collapse, "Events.Collapse");
eventTypeNames.put(Events.ColumnClick, "Events.ColumnClick");
eventTypeNames.put(Events.ColumnResize, "Events.ColumnResize");
eventTypeNames.put(Events.Complete, "Events.Complete");
eventTypeNames.put(Events.ContextMenu, "Events.ContextMenu");
eventTypeNames.put(Events.Deactivate, "Events.Deactivate");
eventTypeNames.put(Events.Detach, "Events.Detach");
eventTypeNames.put(Events.Disable, "Events.Disable");
eventTypeNames.put(Events.DoubleClick, "Events.DoubleClick");
eventTypeNames.put(Events.DragCancel, "Events.DragCancel");
eventTypeNames.put(Events.DragEnd, "Events.DragEnd");
eventTypeNames.put(Events.DragEnter, "Events.DragEnter");
eventTypeNames.put(Events.DragFail, "Events.DragFail");
eventTypeNames.put(Events.DragLeave, "Events.DragLeave");
eventTypeNames.put(Events.DragMove, "Events.DragMove");
eventTypeNames.put(Events.DragStart, "Events.DragStart");
eventTypeNames.put(Events.Drop, "Events.Drop");
eventTypeNames.put(Events.EffectCancel, "Events.EffectCancel");
eventTypeNames.put(Events.EffectComplete, "Events.EffectComplete");
eventTypeNames.put(Events.EffectStart, "Events.EffectStart");
eventTypeNames.put(Events.Enable, "Events.Enable");
eventTypeNames.put(Events.Exception, "Events.Exception");
eventTypeNames.put(Events.Expand, "Events.Expand");
eventTypeNames.put(Events.Focus, "Events.Focus");
eventTypeNames.put(Events.HeaderChange, "Events.HeaderChange");
eventTypeNames.put(Events.HeaderClick, "Events.HeaderClick");
eventTypeNames
.put(Events.HeaderContextMenu, "Events.HeaderContextMenu");
eventTypeNames
.put(Events.HeaderDoubleClick, "Events.HeaderDoubleClick");
eventTypeNames.put(Events.HeaderMouseDown, "Events.HeaderMouseDown");
eventTypeNames.put(Events.HiddenChange, "Events.HiddenChange");
eventTypeNames.put(Events.Hide, "Events.Hide");
eventTypeNames.put(Events.Invalid, "Events.Invalid");
eventTypeNames.put(Events.KeyDown, "Events.KeyDown");
eventTypeNames.put(Events.KeyPress, "Events.KeyPress");
eventTypeNames.put(Events.KeyUp, "Events.KeyUp");
eventTypeNames.put(Events.LiveGridViewUpdate,
"Events.LiveGridViewUpdate");
eventTypeNames.put(Events.Maximize, "Events.Maximize");
eventTypeNames.put(Events.MenuHide, "Events.MenuHide");
eventTypeNames.put(Events.MenuShow, "Events.MenuShow");
eventTypeNames.put(Events.Minimize, "Events.Minimize");
eventTypeNames.put(Events.Move, "Events.Move");
eventTypeNames.put(Events.OnBlur, "Events.OnBlur");
eventTypeNames.put(Events.OnChange, "Events.OnChange");
eventTypeNames.put(Events.OnClick, "Events.OnClick");
eventTypeNames.put(Events.OnContextMenu, "Events.OnContextMenu");
eventTypeNames.put(Events.OnDoubleClick, "Events.OnDoubleClick");
eventTypeNames.put(Events.OnError, "Events.OnError");
eventTypeNames.put(Events.OnFocus, "Events.OnFocus");
eventTypeNames.put(Events.OnKeyDown, "Events.OnKeyDown");
eventTypeNames.put(Events.OnKeyPress, "Events.OnKeyPress");
eventTypeNames.put(Events.OnKeyUp, "Events.OnKeyUp");
eventTypeNames.put(Events.OnLoad, "Events.OnLoad");
eventTypeNames.put(Events.OnLoseCapture, "Events.OnLoseCapture");
eventTypeNames.put(Events.OnMouseDown, "Events.OnMouseDown");
eventTypeNames.put(Events.OnMouseMove, "Events.OnMouseMove");
eventTypeNames.put(Events.OnMouseOut, "Events.OnMouseOut");
eventTypeNames.put(Events.OnMouseOver, "Events.OnMouseOver");
eventTypeNames.put(Events.OnMouseUp, "Events.OnMouseUp");
eventTypeNames.put(Events.OnMouseWheel, "Events.OnMouseWheel");
eventTypeNames.put(Events.OnScroll, "Events.OnScroll");
eventTypeNames.put(Events.Open, "Events.Open");
eventTypeNames.put(Events.Orphan, "Events.Orphan");
eventTypeNames.put(Events.Ready, "Events.Ready");
eventTypeNames.put(Events.Refresh, "Events.Refresh");
eventTypeNames.put(Events.Register, "Events.Register");
eventTypeNames.put(Events.Remove, "Events.Remove");
eventTypeNames.put(Events.Render, "Events.Render");
eventTypeNames.put(Events.Resize, "Events.Resize");
eventTypeNames.put(Events.ResizeEnd, "Events.ResizeEnd");
eventTypeNames.put(Events.ResizeStart, "Events.ResizeStart");
eventTypeNames.put(Events.Restore, "Events.Restore");
eventTypeNames.put(Events.RowClick, "Events.RowClick");
eventTypeNames.put(Events.RowDoubleClick, "Events.RowDoubleClick");
eventTypeNames.put(Events.RowMouseDown, "Events.RowMouseDown");
eventTypeNames.put(Events.RowMouseUp, "Events.RowMouseUp");
eventTypeNames.put(Events.RowUpdated, "Events.RowUpdated");
eventTypeNames.put(Events.Scroll, "Events.Scroll");
eventTypeNames.put(Events.Select, "Events.Select");
eventTypeNames.put(Events.SelectionChange, "Events.SelectionChange");
eventTypeNames.put(Events.Show, "Events.Show");
eventTypeNames.put(Events.SortChange, "Events.SortChange");
eventTypeNames.put(Events.SpecialKey, "Events.SpecialKey");
eventTypeNames.put(Events.StartEdit, "Events.StartEdit");
eventTypeNames.put(Events.StateChange, "Events.StateChange");
eventTypeNames.put(Events.StateRestore, "Events.StateRestore");
eventTypeNames.put(Events.StateSave, "Events.StateSave");
eventTypeNames.put(Events.Submit, "Events.Submit");
eventTypeNames.put(Events.Toggle, "Events.Toggle");
eventTypeNames.put(Events.TriggerClick, "Events.TriggerClick");
eventTypeNames.put(Events.TwinTriggerClick, "Events.TwinTriggerClick");
eventTypeNames.put(Events.UnBind, "Events.UnBind");
eventTypeNames.put(Events.Unregister, "Events.Unregister");
eventTypeNames.put(Events.Update, "Events.Update");
eventTypeNames.put(Events.Valid, "Events.Valid");
eventTypeNames.put(Events.ValidateDrop, "Events.ValidateDrop");
eventTypeNames.put(Events.ValidateEdit, "Events.ValidateEdit");
eventTypeNames.put(Events.ViewReady, "Events.ViewReady");
}
}
I ended up using brute force: Created a Map of EventType and name, then attach a Listener for each type of event that the component could receive. Then I just set a breakpoint inside the Listener, and I could see what events were received when anything happened.
If it hadn't been throwaway code, I would have cleaned it up into a utility class, not used an anonymous Listener class, etc.
final Map<EventType, String> eventTypeNames = new HashMap<EventType, String>();
eventTypeNames.put(Events.BeforeExpand, "BeforeExpand");
eventTypeNames.put(Events.Expand, "Expand");
...
eventTypeNames.put(Events.BeforeStateSave, "BeforeStateSave");
eventTypeNames.put(Events.StateSave, "StateSave");
for (EventType eventType : Arrays.asList(
Events.BeforeExpand,
Events.Expand,
...
Events.BeforeStateSave,
Events.StateSave
)) {
this.addListener(eventType, new Listener<BaseEvent>() {
public void handleEvent(final BaseEvent be) {
String type = eventTypeNames.get(be.getType());
String ev = be.toString();
}
});
}
The API docs for the various widgets describe what events will fire and when they will fire. For an example, let's say we wanted take an action any time a user chooses a new TabItem in a TabPanel.
TabPanel's API documentation (located at http://extjs.com/deploy/gxtdocs/com/extjs/gxt/ui/client/widget/TabPanel.html) shows several events; we're interested in Select:
Select : TabPanelEvent(container, item)
Fires after a item is selected.
container : this
item : the item that was selected
So, to capture the event (which it appears you understand, but I will include for completeness' sake) the process is to add a listener to the TabPanel, watching specifically for the Events.Select event:
tp.addListener(Events.Select, new Listener<TabPanelEvent>(){
public void handleEvent(TabPanelEvent be)
{
MessageBox.alert("Test", be.item.getText(), null);
}
});
Note that many events have a property called doit which you may set to false to cancel the event.
A complete code listing:
package edu.fresno.client;
import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.TabPanelEvent;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
public class GWTSandbox implements EntryPoint {
public void onModuleLoad() {
TabPanel tp = new TabPanel();
TabItem ti1 = new TabItem("TabItem1");
TabItem ti2 = new TabItem("TabItem2");
tp.add(ti1);
tp.add(ti2);
tp.addListener(Events.Select, new Listener<TabPanelEvent>(){
public void handleEvent(TabPanelEvent be)
{
MessageBox.alert("Test", be.item.getText(), null);
}
});
ContentPanel panel = new ContentPanel();
panel.setLayout(new FitLayout());
panel.add(tp);
RootPanel.get().add(panel);
}
}
You could add following code for the constructor:
ContentPanel panel =new ContentPanel(){
public boolean fireEvent(EventType type) {
System.out.println(type.getEventCode());
return super.fireEvent(type);
}
public boolean fireEvent(EventType eventType, BaseEvent be) {
System.out.println(eventType.getEventCode());
return super.fireEvent(eventType, be);
}
public boolean fireEvent(EventType type, ComponentEvent ce) {
System.out.println(type.getEventCode());
return super.fireEvent(type, ce);
}
};
then it will print any event this component can receive.