How to switch Display between different classes in Java ME - java

I am developing a Java ME program. The different forms are located in separate classes. I tried to switch display between main MIDlet and a class and succeeded. How to do the same between two classes? I am just a beginner in Java ME.

I use following code for the same,
First display a static Display variable in Midlet
private static Display display;
Now initialize the dislplay variable in class Constructor
public MyMidlet() {
display = Display.getDisplay(this);
}
Now declare a getDisplay() method in Midlet class
public static Display getDisplay () {
return display;
}
Now you can use this getDisplay() method to get the current Display's object and then set any class's form
MyMidlet.getDisplay().setCurrent(form);

Simplification is:
Display.getDisplay(this).setCurrent(screen);
Where screen is an instance of LCDUI (Form, Alert...) or intance of Canvas object.
The this is an instance of the MIDlet

Related

adding wizardPages dynamically and returning the result

I created a Wizard, then I created a WizardPage namely FrontPage and added it to wizard in wizard's addPage method.
Since I have only one page in addPage method, the Next button not shown. I want it to be shown.
In the nextPressed method of the FrontPage, I created an instance of another java class that the class runs the program written in another language. In several points of that program, I need to create wizardPage dynamically. For this purpose I created a java class to contribute to the program. This class namely interfaceRule create an instances of wizardPage in other plugin.
My questions is how can I add this wizardPages to the main wizard? And also how can I return the value that user select in wizardPage to interfaceRule class?
edit:
public IWizardPage getNextPage() {
boolean isNextPressed = "nextPressed".equalsIgnoreCase(Thread.currentThread().getStackTrace()[2].getMethodName());
if (isNextPressed) {
boolean validatedNextPress = this.nextPressed();
if (!validatedNextPress) {
return this;
}
}
return super.getNextPage();
}
There are several ways to control the Next button in your class extending Wizard
You can call setForcePreviousAndNextButtons(true) in the wizard constructor to force the buttons always to be shown.
You can override the needsPreviousAndNextButtons method to decide if the buttons are required dynamically.
You can override the getNextPage method:
public IWizardPage getNextPage(final IWizardPage page)
to control exactly which page is shown next (also getPreviousPage).

Static variable fields not visible in debbuger

Hi I've got a very simple class defined like this
public class Pokus {
public static String loginToken;
public String neco = "neco";
public Pokus() {
}
public static String getLoginToken() {
return loginToken;
}
public static void setLoginToken(String loginToken) {
Pokus.loginToken = loginToken;
}
}
When I create an instance of this class
Pokus pokus = new Pokus();
pokus.setLoginToken("bla1212");
In a debugger I can see that object pokus has a field/variable called "neco" but not that static variable "loginToken".
debbuger in Android Studio
Is there any way to see static variables as well as the non-static ones?
Thanks guys I knew all of this but didn't know that debugger is taking this into consideration. There is an option to show static field Settings > Build,Execution, Deployement > Debugger > Data Views > Java
Debugger shows it properly, pokus is instance of class Pokus so it have standard method and properties from class Pokus, static methods and properties are in Class not in instance of Class. Static properties are shared for every object created from class Pokus ( or for every component in program if their are public ) so debugger properly not shows them as properties of single instance.
To show static variable examine class not instance. When debugger stops on breakpoint You can use console and write Pokus.someStaticVar and You will see current value. Console is available in debugger - http://imgur.com/a/nHfEo.
You can right-click in the debugger's Variables area and select Customize Data Views...
In there, you can choose to add static and final static fields.
Static variables have the same values for all the instance of the class.Moreover they should be accessed using class and not an instance of the class. In Java, when you declare something as static, you are saying that it is a member of the class, not the object (hence why there is only one). Therefore it doesn't make sense to access it on the object, because that particular data member is associated with the class.
That is the reason i think debugger does not show and it is correct behaviour

Java Swing GUI Console Component

I'm currently creating my first java swing application and as part of the GUI I have a small console in the form of a JTextField component. I would like to be able to print to this console from anywhere in the application using a command like console.print(String). I believe that I should be using print stream but I can't figure out how to make this work properly from anywhere (i.e. in another class which doesn't reference the console) .
I would also like to maintain the ability to print out to the eclipse console. Any help on this matter would be much appreciated.
Create a class for the purpose of accepting and distributing console strings; give it a static method to print to your console. Give that class a (static) reference to your console component.
something like:
public MyConsole
{
private static TextField field;
public static void setField(TextField givenField)
{
field = givenField;
}
public static void print(String msg)
{
field.append(msg);
}
}
The other parts of your application can import MyConsole and call MyConsole.print(msg);

Java JFrame and another class interaction

I've found the similar question, but it's still be unclear for me.
So, I have a main class ProcessorCalculations(), from which I call MainFrame() class. In MainFrame class user should choose the folder. How I can transmit the JFileChooser() object from MainFrame() to ProcessorCalculations()?
I've tried to implement the hint from the link above:
ProcessorCalculation processor = new ProcessorCalculation();
MainFrame mainFrame = new MainFrame(processor);
But I don't know how to call processor methods from mainFrame without creating new objects.
Even I dont't know the correct question I should ask Google.
Help please.
If you're using the code written above, then you're passing the current processor instance into your MainFrame constructor. What are you doing with the reference from within this constructor? Are you settinga a ProcessorCalculation instance to this reference? Please show us your constructor.
Your MainFrame class should look something like...
public class MainFrame extends JFrame {
// your ProcessorCalculation field
private ProcessorCalculation processor;
public MainFrame(ProcessorCalculation processor) {
// set the field with ref passed in parameter
this.processor = processor;
// of course other code goes here
}
public void someMainFrameMethod() {
// use the reference
processor.someProcessorMethod();
}
}
Create an attribute say for example files in the mainframe by which the contents of JFileChooser() are referenced ( you may say contents are stored in this attribute ). If this attribute is private put getter setter methods in the Mainframe for this attribute ( to make it accessible from other classes) now coming back to your ProcessorCalculation class when you write mainFrame.getFiles() ( you have already created object mainFrame object there) it returns the data you wanted in this class.
In case you still face a problem please ask for a coded solution I will do.

Splitting up classes from class and calling methods

I have created an application using the texttospeech api and I have all the functionality within one class. I would like to split this into several classes but when I do so I have a null exception error.
The texttospeech api has onclick buttons. Within these buttons I try to call a method from another class for the functionality.
I extend the class 1 with the current class I am using.
I then add the method image() within the class 1:
public void image() {
if(currentHelloIndex==0){
alertDialog.show();
}
else if (currentHelloIndex == 2) {
Image.setImageResource(R.drawable.books);
} else if (currentHelloIndex == 3) {
Image.setImageResource(R.drawable.mic);
}
Currently no variables are declared in class 1 as it is using the variables in the main class.
I then call this method in the main class. This doesn't seem to be working the class 1 has no onCreate method it is just a standard class which extends the main class.
I would appreciate any help on this as I need to separate the functionality into separate classes.
Edit:
currentHelloIndex is an int which is set to 0 in the main class
if the button is clicked an currentHelloIndex is 0 an alertdialog in the main class will appear
if the button is clicked and currentHelloIndex is 2 this will set the Image which is an ImageView in the main class with the image set.
I have put into the main class: static SoundGameScore sound;
Within the main class I have called sound.Image(); in an onclick. Please can someone let me know what I have done wrong, thanks.
You should use some of the refactoring functionality in your Java IDE (you ARE using a Java IDE, right?)

Categories