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);
Related
I have a little issue in android studio Java code !
Im using switch compact to change the background image from a light one to a dark one instead of NightMode. Every thing works fine in a class called "Category Activity" where the switch compact is there, but i want this to work in another class i mean whenever i click on switch compact the background Image change in all activity
Here is the code in "Category Activity" :
// ----------- DAY AND NIGHT MODE ----------------------//
switchCompat = findViewById(R.id.switchCompat);
back_images = new int[]{R.drawable.lightbg, R.drawable.darkbg};
switchCompat = findViewById(R.id.switchCompat);
screenView = findViewById(R.id.layoutcategory);
sharedPreferences = getSharedPreferences("dark_light", 0);
Boolean booleanValue = sharedPreferences.getBoolean("dark_light_mode", true);
if (booleanValue) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
switchCompat.setChecked(true);
screenView.setBackgroundResource(R.drawable.lightbg);
switchCompat.setText(R.string.LightMode);
} else {
switchCompat.setChecked(false);
screenView.setBackgroundResource(R.drawable.darkbg);
switchCompat.setText(R.string.NightMode);
}
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
switchCompat.setChecked(true);
screenView.setBackgroundResource(R.drawable.lightbg);
switchCompat.setText(R.string.LightMode);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("night_mode", true);
editor.commit();
} else {
screenView.setBackgroundResource(R.drawable.darkbg);
switchCompat.setText(R.string.NightMode);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("night_mode", false);
editor.commit();
}
}
});
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'm trying to create a couple of BasicEditField objects after i get the number of fields that i want from an ObjectChoiceField.
Problem: the BasicEditField fields that i add to my screen don't refresh unless i do it in the listener from my ObjectChoiceField.
what i want to do :
select the number of BasicEditFields that i want.
refresh the screen so the fields added appear.
PD: if you need more info, just tell me, and sorry about my english. I'm new at developing for the BlackBerry plataform
public final class MyScreen extends MainScreen
{
private int fields_lenght;
public MyScreen()
{
// Set the displayed title of the screen
setTitle("Example");
fields_lenght =0;
final String shortcodes[] = {"1","2","3"};
final ObjectChoiceField dropdownlist=new ObjectChoiceField("Select a number of fields",shortcodes);
this.add(dropdownlist);
dropdownlist.setChangeListener( new FieldChangeListener() {
public void fieldChanged( Field arg0, int arg1 ) {
if(arg1 != PROGRAMMATIC){
fields_lenght= Integer.parseInt(shortcodes[dropdownlist.getSelectedIndex()]);
}
}
} );
// how to refresh the screen with the new fields ???
BasicEditField fields[]=new BasicEditField [fields_lenght] ;
for(int i = 0; i<fields.length;i++){
fields[i]=new BasicEditField("Campo "+i,"");
this.add(fields[i]);
}
}
}
You really should add or delete the fields from within your ObjectChoiceField listener. That's when you know what the proper number of fields is. (Certainly, if you just want to keep your code neat and clean, you could define a separate method, that is called from the choice field listener ... that's not much different).
Try something like this:
public final class MyScreen extends MainScreen {
/** A cached vector of the BasicEditFields, to make deleting easier */
private Vector fields;
public MyScreen() {
super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
setTitle("Example");
final String shortcodes[] = {"1","2","3"};
final ObjectChoiceField dropdownlist = new ObjectChoiceField("Select a number of fields", shortcodes);
add(dropdownlist);
fields = new Vector();
final Screen screen = this;
dropdownlist.setChangeListener( new FieldChangeListener() {
public void fieldChanged( Field field, int context ) {
if (context != PROGRAMMATIC) {
// how many fields has the user chosen?
int fieldsLength = Integer.parseInt(shortcodes[dropdownlist.getSelectedIndex()]);
while (fieldsLength > fields.size()) {
// we need to ADD more fields
Field f = new BasicEditField("Campo " + fields.size(), "");
fields.addElement(f);
screen.add(f);
}
while (fieldsLength < fields.size()) {
// we need to DELETE some fields
Field f = (Field)fields.elementAt(fields.size() - 1);
fields.removeElement(f);
screen.delete(f);
}
}
}
});
}
I defined a new member named fields, which just makes it easier to keep track of the basic edit fields (in case this screen has many other fields, too).
When the choice field listener is called, I determine how many fields the user wants; if they need more, I add them to the screen, and to the fields Vector. If they want fewer, I delete some fields from the end of the Vector, and remove them from the Screen.
Note: there should be no need to call invalidate() here. Calling Screen#add() or Screen#delete() should add/delete the fields and cause repainting.
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() {
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.