I am having problem with Blackberry horizontal and vertical Field Managers. In my sample application I have two vertical field managers added to one horizontal field manager and the GUI is in the following figure.
In the field change listener of button I am removing the left VFM, then only the Home button is displayed, and in home button listener I am adding the left VFM, then it get back to the above screen.
The problem is occurring after scrolling the left content and deleting the left VFM, Then only Home button is displayed and track pad navigation (scrolling) leads to Null Pointer exception screen. The following figure contains the simulator screen shot of error.
And here is the complete source code for reproducing the error.
public class MyApp extends UiApplication{
public static void main(String[] args)
{
// Create a new instance of the application and make the currently
// running thread the application's event dispatch thread.
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}
/**
* Creates a new MyApp object
*/
public MyApp()
{
// Push a screen onto the UI stack for rendering.
pushScreen(new MyScreen());
}
}
public final class MyScreen extends MainScreen{
private HorizontalFieldManager hfmMainManager = null;
private VerticalFieldManager menuButtonManager = null;
private String[] buttons = new String[]{"BUTTON ID 1",
"BUTTON ID 2",
"BUTTON ID 3",
"BUTTON ID 4",
"BUTTON ID 5",
"BUTTON ID 6",
"BUTTON ID 7",
"BUTTON ID 8",
"BUTTON ID 9",
"BUTTON ID 10"};
private boolean menuShowing = false;
/**
* Creates a new MyScreen object
*/
public MyScreen()
{
super( Manager.NO_VERTICAL_SCROLL | MainScreen.NO_VERTICAL_SCROLLBAR
| Manager.NO_HORIZONTAL_SCROLL | Manager.NO_HORIZONTAL_SCROLLBAR);
hfmMainManager = new HorizontalFieldManager(Manager.NO_HORIZONTAL_SCROLL
| Manager.NO_HORIZONTAL_SCROLLBAR);
////////////////////MENU BUTTONS
menuButtonManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR);
menuButtonManager.add(new ButtonField("Header Button", Field.FIELD_HCENTER));
VerticalFieldManager vfmScrollable = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR);
for (int i = 0; i < buttons.length; i++) {
ButtonField buttonField = new ButtonField(buttons[i], Field.FIELD_HCENTER);
buttonField.setChangeListener(listenerButton);
vfmScrollable.add(buttonField);
}
menuButtonManager.add(vfmScrollable);
hfmMainManager.add(menuButtonManager);
VerticalFieldManager vfmMenuContentManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLLBAR | Manager.NO_VERTICAL_SCROLL);
ButtonField buttonField = new ButtonField("Home Button", Field.FIELD_HCENTER);
buttonField.setChangeListener(listenerButton);
buttonField.setMargin(10, 0, 10, 0);
vfmMenuContentManager.add(buttonField);
hfmMainManager.add(vfmMenuContentManager);
add(hfmMainManager);
menuShowing = true;
}
private FieldChangeListener listenerButton = new FieldChangeListener() {
public void fieldChanged(Field arg0, int arg1) {
synchronized (UiApplication.getEventLock()) {
if(menuShowing){
menuShowing = false;
hfmMainManager.delete(menuButtonManager);
}else{
menuShowing = true;
hfmMainManager.insert(menuButtonManager, 0);
}
}
}
};
}
How do I solve this Issue?
Thanks in advance
Regards
Anish
I've seen similar behavior when trying to change the field structure from a focus or unfocus event. When a field removes itself from the screen in the middle of the focus event, the focus tracking can get out of sync, and cause unusual errors like this. Try changing from grabbing the UI lock to calling invokeLater instead. That way the change listener can finish executing before you delete the VFM, keeping all the invariants in place.
Get rid of this:
synchronized (UiApplication.getEventLock()) {
Replace it with this:
UiApplication.getInstance().invokeLater(new Runnable{ public void run() {
Related
I have the following problem:
My method opens a JDialog with a bunch of buttons (only one in example code). I want to click a button and thereby choose an ImageIcon for my method to return. But the Method does not wait for me to click a button. It opens the window and then returns an empty ImageIcon.
public class Kartenauswahl {
ImageIcon bandit;
public ImageIcon auswahlfenster() {
int bwidth = new Integer(150);
int bheight = new Integer(225);
bandit = new ImageIcon("cover/Bandit.jpe");
bandit.setImage(bandit.getImage().getScaledInstance(bwidth,bheight,Image.SCALE_DEFAULT));
final JDialog kartenwahl = new JDialog();
kartenwahl.setTitle("Kartenwahl");
kartenwahl.setSize(1500,1000);
kartenwahl.setVisible(true);
kartenwahl.setLayout(new FlowLayout());
ImageIcon returnicon= new ImageIcon();
final JButton b1 = new JButton(); //just to get the Icon out of the void loop
JButton B1 = new JButton(bandit); //this is going to be the button I want to click to choose the ImageIcon which is returned
B1.setContentAreaFilled(false);
B1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
b1.setIcon(bandit);
kartenwahl.dispose();
}
});
kartenwahl.add(B1);
returnicon = (ImageIcon) b1.getIcon();
return returnicon;
}
}
Question: can I bind the return statement to a condition? Like "only return after I clicked that Button B1"?
Hi sorry for the long wait. I have written an custom JDialog that should work for you.
public class CustomDialog extends JDialog {
JButton[] buttons;
ImageIcon selectedImageIcon;
public CustomDialog() {
setSize(500, 500);
setLayout(new GridLayout(4, 6));
ActionListener actionListener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
selectedImageIcon = ((ImageIcon) ((JButton) e.getSource()).getIcon());
dispose();
}
};
buttons = new JButton[24];
for(int i = 0; i < 24; i++) {
buttons[i] = new JButton(new ImageIcon("path_to_your_image_file"));
buttons[i].addActionListener(actionListener);
add(buttons[i]);
}
setVisible(true);
}
public ImageIcon getSelectedImageIcon() {
return selectedImageIcon;
}
}
The initial size is not that important the GridLayout is. you mentioned that you would need 24 buttons so I created an grid with 4 rows and 6 columns.
Then I create the buttons in a loop and adding the same Listener to set the selection icon with the icon of the pressed button. Afterwards I dispose the screen triggering an windowClosed event.
You could simply create this Dialog from your main class and wait for the response like so:
public class main {
public static void main(String[] args) {
CustomDialog customDialog = new CustomDialog();
customDialog.addWindowListener(new WindowAdapter() {
#Override
public void windowClosed(WindowEvent e) {
ImageIcon icon = customDialog.getSelectedImageIcon();
//do something with your icon
}
});
}
}
Don't forget to mark this answer as correct if it fixes your problem.
Have a good one!
i am trying to add a custom window in blackberry but before this i am trying to add a custom label in that popup screen for my satisfaction that i can add or cant . so at the time when i am adding that i am facing the problem of IllegalArguementException error , so can you please tell me how can i solve that problem . i am doing like this .
see this is my MYScreen class which i am using to add pop-up . so pop-up is added when ever i clicked that press button , which is added in the Screen .
public final class MyScreen extends MainScreen implements FieldChangeListener
{
private ButtonField btn;
public MyScreen()
{
setTitle("MyTitle");
btn = new ButtonField ("press");
btn.setChangeListener(this);
add(btn) ;
}
public void fieldChanged(Field field, int context)
{
if ( field == btn )
{
Dialog.inform("hello");
pop_manager manager_object = new pop_manager(0);
UiApplication.getUiApplication().pushScreen( new up_pop_test( manager_object ) );
}
}
}
so in this i have added , new_up_pop_test class which is :
public class up_pop_test extends PopupScreen
{
public up_pop_test( pop_manager delegate)
{
super(delegate);
add(delegate);
}
}
and pop_manager is :
public class pop_manager extends Manager
{
protected pop_manager(long style)
{
super(style);
}
protected void sublayout(int w, int h)
{
Field f = getField(0);
layoutChild( f , w/3+w/3 , 50 ) ;
setPositionChild ( f , w/33 + w/33 , w/67+w/104 );
setExtent(w,h);
}
}
You don't need to do as much work as you were trying to...
All you need is a PopupScreen, which you can customise at will, just like a normal screen.
public class MyPopup extends PopupScreen {
public MyPopup() {
super(new VerticalFieldManager());
LabelField infoLabel = new LabelField("Here is a label in a popup");
add(infoLabel);
}
}
In order to call it, all you need to do is push like a normal screen.
UiApplication.getUiApplication().pushScreen(new MyPopup());
You cannot gain control over the Dialog class, these are for simple operations like informing the user or asking a question, and are standardised
I am using PaneManagerDemo project code (this project is present in sample demo projects of BB) for developing tab.
I am facing one problem when I select tab it got focus but when I select data below that tab the focus goes to that data.
I want when user select data under tab, it must focus on that data as well as the tab so that user can understand selected data is under which tab.
I am not getting it.
Please tell me.
Here is code for the reference.
public class PaneManagerDemo extends UiApplication
{
public static void main(String[] args)
{
UiApplication app = new PaneManagerDemo();
app.enterEventDispatcher();
}
public PaneManagerDemo()
{
invokeLater(new Runnable()
{
public void run()
{
int headerType = 0;
// Display a dialog for user to select header type
OptionDialog dialog = new OptionDialog();
int result = dialog.doModal();
if(result == Dialog.OK)
{
headerType = dialog.getHeaderType();
}
else if(result == Dialog.CANCEL)
{
System.exit(0);
}
//PaneScreen screen = new PaneScreen(headerType);
PaneScreen screen = new PaneScreen(headerType);
pushScreen(screen);
}
});
}
/**
* A dialog popup used to choose a header type
*/
private static class OptionDialog extends Dialog
{
public static final int SCROLL_HEADER_TYPE = 0;
public static final int TAB_HEADER_TYPE = 1;
private ObjectChoiceField _choiceField;
/**
* Create a new HeaderDialog object
*/
public OptionDialog()
{
super(Dialog.D_OK_CANCEL, "Choose Header Type", Dialog.OK, null, Dialog.GLOBAL_STATUS);
_choiceField = new ObjectChoiceField("", new String[]{"Scrollable", "Tab"}, 0);
add(_choiceField);
_choiceField.setFocus();
}
/**
* Returns an integer representing the header type
*
* #return SCROLL_HEADER_TYPE if scrollable header selected, TAB_HEADER_TYPE if tab header selected
*/
public int getHeaderType()
{
return _choiceField.getSelectedIndex();
}
}
/**
* Main screen for the application. Displays three panes
* switchable via horizontal scroll field or tabs, depending
* on user selection.
*/
private final static class PaneScreen extends MainScreen
{
/**
* Creates a new PaneScreen object
* #param headerType The header type for the PaneManager, scrollable or tab style
*/
public PaneScreen(int headerType)
{
super(Field.FOCUSABLE);
// Instantiate the model for the pane manager and enable looping
PaneManagerModel model = new PaneManagerModel();
model.enableLooping(true);
// Create a pane
VerticalFieldManager vfm = new VerticalFieldManager();
vfm.add(new LabelField("Data 1"));
XYEdges edgesOne = new XYEdges(1, 1, 1, 1);
vfm.setBorder(BorderFactory.createRoundedBorder(edgesOne));
Pane pane = new Pane(new LabelField("Pane 1", Field.FOCUSABLE | Field.FIELD_HCENTER), vfm);
// Add the pane to the model
model.addPane(pane);
// Create a second pane
vfm = new VerticalFieldManager();
for(int i = 0; i < 30; i++)
{
vfm.add(new LabelField("Data " + i, Field.FOCUSABLE));
}
LabelField iconTextLabelField = new LabelField("Pane 2");
model.addPane(new Pane(iconTextLabelField, vfm));
// Create a third pane
vfm = new VerticalFieldManager();
ButtonField button = new ButtonField("Button", ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY);
button.setChangeListener( new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
Dialog.inform("Button activated.");
}
});
vfm.add(button);
model.addPane(new Pane(new LabelField("Pane 3"), vfm));
// Choose which pane the model is displaying
model.setCurrentlySelectedIndex(1);
// Create the header and initialize the model and visual properties
TitleView header = null;
PaneManagerController controller = null;
if(headerType == OptionDialog.SCROLL_HEADER_TYPE)
{
header = new HorizontalScrollableTitleView(Field.FOCUSABLE);
controller = new HorizontalScrollableController();
}
else if(headerType == OptionDialog.TAB_HEADER_TYPE)
{
header = new HorizontalTabTitleView(Field.FOCUSABLE);
((HorizontalTabTitleView)header).setNumberOfDisplayedTabs(model.numberOfPanes());
controller = new HorizontalTabController();
}
else
{
throw new IllegalStateException("Header type is not valid.");
}
header.setModel(model);
XYEdges edgesFour = new XYEdges(4, 4, 4, 4);
header.setBorder(BorderFactory.createRoundedBorder(edgesFour));
// Set arrow images
Bitmap leftArrow = Bitmap.getBitmapResource("leftArrow.png");
Bitmap rightArrow = Bitmap.getBitmapResource("rightArrow.png");
if(leftArrow != null)
{
header.setLeftArrow(leftArrow);
}
if(rightArrow != null)
{
header.setRightArrow(rightArrow);
}
// Create the PaneView object, which will display the panes and is
// controlled by the model.
PaneView paneView = new PaneView(Field.FOCUSABLE);
paneView.setBorder(BorderFactory.createSimpleBorder(edgesOne));
paneView.setModel(model);
// Initialize the PaneManagerView
PaneManagerView view = new PaneManagerView(Field.FOCUSABLE, header, paneView);
view.setModel(model);
view.setBorder(BorderFactory.createRoundedBorder(edgesFour));
model.setView(view);
// Initialize the Controller
controller.setModel(model);
controller.setView(view);
model.setController(controller);
view.setController(controller);
add(view);
}
}
}
Only one field in a screen can hold focus at any one time, so you will need to indicate which tab contains the focused field by other means. e.g. by painting the selected tab a different colour.
Hi friends I got sample code for Pane Manager from blackberry samples but when i run that sample and double click on tabs ,menus appear .I don't want to show that menus on click.how to do it.please tell me why this is happening? i am not understanding the code.Please help
public class PaneManagerDemo extends UiApplication
{
/**
* Entry point for the application
* #param args Command line arguments (not used)
*/
public static void main(String[] args)
{
UiApplication app = new PaneManagerDemo();
app.enterEventDispatcher();
}
/**
* Creates a new PaneManagerDemo object
*/
public PaneManagerDemo()
{
invokeLater(new Runnable()
{
public void run()
{
int headerType = 0;
// Display a dialog for user to select header type
OptionDialog dialog = new OptionDialog();
int result = dialog.doModal();
if(result == Dialog.OK)
{
headerType = dialog.getHeaderType();
}
else if(result == Dialog.CANCEL)
{
System.exit(0);
}
//PaneScreen screen = new PaneScreen(headerType);
PaneScreen screen = new PaneScreen(headerType);
pushScreen(screen);
}
});
}
/**
* A dialog popup used to choose a header type
*/
private static class OptionDialog extends Dialog
{
public static final int SCROLL_HEADER_TYPE = 0;
public static final int TAB_HEADER_TYPE = 1;
private ObjectChoiceField _choiceField;
/**
* Create a new HeaderDialog object
*/
public OptionDialog()
{
super(Dialog.D_OK_CANCEL, "Choose Header Type", Dialog.OK, null, Dialog.GLOBAL_STATUS);
_choiceField = new ObjectChoiceField("", new String[]{"Scrollable", "Tab"}, 0);
add(_choiceField);
_choiceField.setFocus();
}
/**
* Returns an integer representing the header type
*
* #return SCROLL_HEADER_TYPE if scrollable header selected, TAB_HEADER_TYPE if tab header selected
*/
public int getHeaderType()
{
return _choiceField.getSelectedIndex();
}
}
/**
* Main screen for the application. Displays three panes
* switchable via horizontal scroll field or tabs, depending
* on user selection.
*/
private final static class PaneScreen extends MainScreen
{
/**
* Creates a new PaneScreen object
* #param headerType The header type for the PaneManager, scrollable or tab style
*/
public PaneScreen(int headerType)
{
super(Field.FOCUSABLE);
// Instantiate the model for the pane manager and enable looping
PaneManagerModel model = new PaneManagerModel();
model.enableLooping(true);
// Create a pane
VerticalFieldManager vfm = new VerticalFieldManager();
vfm.add(new LabelField("Data 1"));
XYEdges edgesOne = new XYEdges(1, 1, 1, 1);
vfm.setBorder(BorderFactory.createRoundedBorder(edgesOne));
Pane pane = new Pane(new LabelField("Pane 1", Field.FOCUSABLE | Field.FIELD_HCENTER), vfm);
// Add the pane to the model
model.addPane(pane);
// Create a second pane
vfm = new VerticalFieldManager();
for(int i = 0; i < 30; i++)
{
vfm.add(new LabelField("Data " + i, Field.FOCUSABLE));
}
LabelField iconTextLabelField = new LabelField("Pane 2");
model.addPane(new Pane(iconTextLabelField, vfm));
// Create a third pane
vfm = new VerticalFieldManager();
ButtonField button = new ButtonField("Button", ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY);
button.setChangeListener( new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
Dialog.inform("Button activated.");
}
});
vfm.add(button);
model.addPane(new Pane(new LabelField("Pane 3"), vfm));
// Choose which pane the model is displaying
model.setCurrentlySelectedIndex(1);
// Create the header and initialize the model and visual properties
TitleView header = null;
PaneManagerController controller = null;
if(headerType == OptionDialog.SCROLL_HEADER_TYPE)
{
header = new HorizontalScrollableTitleView(Field.FOCUSABLE);
controller = new HorizontalScrollableController();
}
else if(headerType == OptionDialog.TAB_HEADER_TYPE)
{
header = new HorizontalTabTitleView(Field.FOCUSABLE);
((HorizontalTabTitleView)header).setNumberOfDisplayedTabs(model.numberOfPanes());
controller = new HorizontalTabController();
}
else
{
throw new IllegalStateException("Header type is not valid.");
}
header.setModel(model);
XYEdges edgesFour = new XYEdges(4, 4, 4, 4);
header.setBorder(BorderFactory.createRoundedBorder(edgesFour));
// Set arrow images
Bitmap leftArrow = Bitmap.getBitmapResource("leftArrow.png");
Bitmap rightArrow = Bitmap.getBitmapResource("rightArrow.png");
if(leftArrow != null)
{
header.setLeftArrow(leftArrow);
}
if(rightArrow != null)
{
header.setRightArrow(rightArrow);
}
// Create the PaneView object, which will display the panes and is
// controlled by the model.
PaneView paneView = new PaneView(Field.FOCUSABLE);
paneView.setBorder(BorderFactory.createSimpleBorder(edgesOne));
paneView.setModel(model);
// Initialize the PaneManagerView
PaneManagerView view = new PaneManagerView(Field.FOCUSABLE, header, paneView);
view.setModel(model);
view.setBorder(BorderFactory.createRoundedBorder(edgesFour));
model.setView(view);
// Initialize the Controller
controller.setModel(model);
controller.setView(view);
model.setController(controller);
view.setController(controller);
add(view);
}
}
}
If you want to hide context menu (popup menu) just write this code
protected boolean navigationClick(int status, int time)
{
return true;
}
This is totally works good for me .
In my app I would like to create a popup sub-menu. How do I do this?
//Menus: makeMenu Creates the menu and adds the items in Menu
protected void makeMenu( Menu inMenu, int inInstance )
{
inMenu.add(showIt1); //inMenu adds the menuItems
inMenu.add(showIt2);
inMenu.add(showIt3);
super.makeMenu( inMenu, inInstance );
/The class extends MainScreen (net.rim.device.api.ui.container.
MainScreen). As you can probably guess, this is a display class. For
BlackBerry applications, this class provides basic display features, including
supplying a Close menu item. This is achieved by the call to super() in
the constructor./
}
//Creates menuItems
✓"Show It 1": This is the text that the menu item will display.
✓ 50: This ordering parameter determines where in the menu the
item will appear. Lower numbers appear closer to the top of the
menu. In addition, a separator bar is added automatically between two
MenuItems whose ordering values differ by 65536 or more.
✓ 50: This value represents priority, which determines which menu item
is likely to receive the focus. Lower number gets the focus.
MenuItem showIt1 = new MenuItem("Show It 1", 50, 50)
{
public void run()
{
//Do whatever you want
}
};
MenuItem showIt2 = new MenuItem("Show It 2", 100, 100)
{
public void run()
{
//Do whatever you want
}
};
MenuItem showIt3 = new MenuItem("Show It 3", 150, 150)
{
public void run()
{
//Do whatever you want
}
};
//End of Menu Creation
MenuItem mymenu = new MenuItem("Categories" , 100, 10)
{
public void run()
{
//navigation purposes
}
}
Call this in constructor
screen.addMenuItem(mymenu);