Blackberry application showing blank screen - java

Hi everyone im having problems in my Blackberry application.................
I have made a simple application it starts with a file called AppStarter
package in.EventTimer;
import net.rim.device.api.ui.UiApplication;
public class AppStarter extends UiApplication
{
public static void main (String[] args)
{
AppStarter theApp = new AppStarter ();
theApp.enterEventDispatcher ();
}
public AppStarter()
{
//display a new screen
pushScreen (new ConnectionSettings ());
}
}
From this AppStarter file it pushes to the second file which is a Screen for the ConnectionSettings
package in.EventTimer;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.MainScreen;
public class ConnectionSettings extends MainScreen
{
public void RadioButton()
{
RadioButtonGroup rbg = new RadioButtonGroup();
RadioButtonField rb1 = new RadioButtonField("tcp");
RadioButtonField rb2 = new RadioButtonField("gprs");
RadioButtonField rb3 = new RadioButtonField("wifi");
rbg.add(rb1);
rbg.add(rb2);
rbg.add(rb3);
}
public boolean onClose()
{
Dialog.alert ("Exit Connection Settings!");
System.exit (0);
return true;
}
}
But when iam running this application in my Blackberry 9700 simulator it is just giving the blank white screen and when im exiting that white screen it is giving the message exitconnection settings which means it is on the connection settings screen but when i run it is showing blank white screen........i have tried many things but no solution yet ............so plz help or suggest something.
Thanks in advance

Try adding the following method to the ConnectionSettings class:
public ConnectionSettings()
{
super();
LabelField title = new LabelField("HelloWorld Sample",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
add(new RichTextField("Hello World!"));
}
Looks like you are missing a constructor... For your MainScreen class
So the final code should look like this:
package in.EventTimer;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.MainScreen;
public class ConnectionSettings extends MainScreen {
public void RadioButton()
{
RadioButtonGroup rbg = new RadioButtonGroup();
RadioButtonField rb1 = new RadioButtonField("tcp");
RadioButtonField rb2 = new RadioButtonField("gprs");
RadioButtonField rb3 = new RadioButtonField("wifi");
rbg.add(rb1);
rbg.add(rb2);
rbg.add(rb3);
add(rb1); //Added by eSniff
add(rb2); //Added by eSniff
add(rb3); //Added by eSniff
}
//Begin added by eSniff
public ConnectionSettings()
{
super();
LabelField title = new LabelField("APP STARTER",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
add(new RichTextField("Hello World!"));
RadioButton();
}
//End added by eSniff
public boolean onClose()
{
Dialog.alert ("Exit Connection Settings!");
System.exit (0);
return true;
}
}

Make the variable "rgb" into a class field
Define a constructor.
In that constructor, you must first call the function RadioButtons(), then call add(rgb) in your constructor, you must first call RadioButtons(), then invoke add(rgb) to ensure that your fields show up on screen.

Related

Implement 2 classes for ActionListener for buttons

I have 2 classes, the first class is where I am creating GUI and all of the components needed. Including the buttons. This is being done outside of the main method and in there own respective methods. I want to .addActionListener, but from another class outside of this one. I do not want to use inner classes.
Here is the classes containing Main and the Gui components and the button.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class PasswordGeneratorGui {
private JFrame interfaceFrame;
private JPanel interfacePanel;
private JMenuBar interfaceMenuBar;
private JMenu interfaceMenu;
private JMenuItem interfaceMenuItemFile;
private JButton interfaceButtonGenerate;
public static void main(String[] args) {
new PasswordGeneratorGui();
}
public PasswordGeneratorGui() {
createInterfacePanel();
createInterfaceFrame();
createInterfaceMenuBar();
createInterfaceMenu();
createInterfaceMenuItem();
createInterfaceButton();
PasswordGeneratorButtonHandler b = new PasswordGeneratorButtonHandler();
interfaceFrame.add(interfacePanel);
interfaceFrame.setVisible(true);
}
public void createInterfacePanel() {
interfacePanel = new JPanel();
interfacePanel.setLayout(null);
}
public void createInterfaceFrame() {
interfaceFrame = new JFrame();
interfaceFrame.setTitle("Password Generator");
interfaceFrame.setBounds(50, 50, 700, 400);
interfaceFrame.setResizable(false);
interfaceFrame.setJMenuBar(interfaceMenuBar);
}
public void createInterfaceMenuBar() {
interfaceMenuBar = new JMenuBar();
interfaceMenuBar.setBounds(0, 0, 700, 20);
interfaceMenuBar.setVisible(true);
interfacePanel.add(interfaceMenuBar);
}
public void createInterfaceMenu() {
interfaceMenu = new JMenu("File");
interfaceMenuBar.add(interfaceMenu);
}
public void createInterfaceMenuItem() {
interfaceMenuItemFile = new JMenuItem("Exit");
interfaceMenu.add(interfaceMenuItemFile);
}
**public void createInterfaceButton() {
interfaceButtonGenerate = new JButton("Generate");
interfaceButtonGenerate.setBounds(0, 358, 700, 20);
interfaceButtonGenerate.addActionListener();
interfacePanel.add(interfaceButtonGenerate);
}**
}
Here is the class for the ActionListener
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PasswordGeneratorButtonHandler implements ActionListener {
PasswordGeneratorButtonHandler generate = new PasswordGeneratorButtonHandler();
public PasswordGeneratorButtonHandler() {
}
public void interfaceButtonGenerateHandler(ActionEvent event) {
System.exit(1);
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
I just want to be able to call the AcitonListener method from the second class. I have tried initiating a new instance of the class and calling it however I think I wasn't quite going in the correct direction.
I'm a little confused about what your asking. You said
I just want to be able to call the AcitonListener method from the second class
Taken literally this means that while you're inside of the PasswordGeneratorButtonHandler class, you want to call the actionPerformed() method. If so, just use this.actionPerformed(), where this is a special keyword in java, representing the current instance of your class.
If however you want to add your handler to the button you created in the first class, which seems like what you might want to do, then you just need to call the JButton#addActionListener() method.
public PasswordGeneratorGui() {
createInterfacePanel();
createInterfaceFrame();
createInterfaceMenuBar();
createInterfaceMenu();
createInterfaceMenuItem();
createInterfaceButton();
PasswordGeneratorButtonHandler b = new PasswordGeneratorButtonHandler();
interfaceButtonGenerate.addActionListener(b); // Add handler to button
interfaceFrame.add(interfacePanel);
interfaceFrame.setVisible(true);
}
Also, inside of the PasswordGeneratorButtonHandler class, you instantiate an instance of the class called generate. This is unnecessary.

Return a value for use in different class

I have the class BossInfo which extends JPanel and has a few components like JLabel, JTextField. My main method is in another file ("DamageCalculator").
Basically, a value is entered into a JTextField via an action listener and I'd like to pass that value to a different file (to use it in calculations). I'm having a lot of trouble with the logic. Here is my class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BossInfo extends JPanel {
private JLabel bossLabel, resultLabel;
private JTextField bossHp;
String bossHpText = new String("");
int valRecd = 0;
public BossInfo() {
//Labels
bossLabel = new JLabel("Boss HP: (Hit Enter to set)");
resultLabel = new JLabel("---");
//Text field for user input of boss hp
bossHp = new JTextField(15);
bossHp.addActionListener(new TempListener());
//add components
add(bossLabel);
add(bossHp);
add(resultLabel);
} //end BossInfo object
public void setVal(int valRecd) {
this.valRecd = valRecd;
}
private class TempListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
int hp;
bossHpText = bossHp.getText();
hp = Integer.valueOf(bossHpText);
dc.setVal(hp);
resultLabel.setText(bossHpText);
}//end action performed
}//end TempListener
} //end class BossInfo
How can I use bossHpText in another class? The "actionPerformed" gets mad if it's any return type other than void so I'm not sure it's meant to return anything.
EDIT: Code updated based on suggestions.
Have a variable valRecd in the MainClass.
On your actionPerformed, call the setter method for this valRecd, e.g,
MainClass obj = new MainClass(); //in your constructor.
public void actionPerformed(ActionEvent event) {
int hp;
bossHpText = bossHp.getText();
hp = Integer.parseInt(text);
obj.setVal(hp); //add this line.
resultLabel.setText(bossHpText);
}
where setVal might be something like this:
public void setVal(int valRecd) {
this.valRecd = valRecd;
}

Error: java.lang.IllegalArgumentException

Error in following code is: java.lang.IllegalArgumentException: adding container's parent to itself.
This is the code:
public class humev extends JFrame implements ActionListener{
//Dichiarazione variabili e costanti
private static final int larghezza = 1300;
private static final int altezza = 1000;
private static final String nome = "Human Evolution";
private JLabel lab;
private JButton gioca;
private JPanel pang;
public humev(){
try{
pang = new JPanel();
gioca = new JButton("Gioca!");
gioca.addActionListener(this);
lab = new JLabel();
gioca.add(gioca);
lab.add(lab);
pang.setLayout(null);
}
catch(Exception e1){
System.err.println(e1);
System.err.println("Impossibile caricare il frame di gioco!");
}
}
public static void main(String[] args) {
//Finestra
try{
humev h = new humev();
JFrame finestra = new JFrame(nome);
Dimension dim_finestra = new Dimension(larghezza, altezza);
finestra.setPreferredSize(dim_finestra);
finestra.setMaximumSize(dim_finestra);
finestra.setResizable(false);
finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
finestra.pack();
finestra.setVisible(true);
}
catch(Exception e2){
System.err.println(e2);
System.err.println("Impossibile caricare la finestra. Frame non caricato");
}
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == gioca){
lab.setText("Gioco avviato con successo!");
}
}
}
You cannot add a labelinto a label :-
lab.add(lab);
Aldo you cannot add a button on a button :-
gioca.add(gioca);
Try adding them to the JPanel or ContentPane instead like
:-
pang.add(gioca);
pang.add(lab);
getContentPane().add(pang);
EDIT:-
For Showing the JFrame you need to do first add your JPanel to JFrame then set the visibility of JFrame to true something like :-
finestra.add(pang); // add panel to frame
finestra.setVisible(true); // show frame visibility to true
Also don't set the layout to null :-
pang.setLayout(null);
Else you will need to set the bounds yourself. So just comment this line.
try to run this example.there is bunch of problems in your code.
you are adding component to itself
gioca.add(gioca); // don't do this
use layouts .don't use null
pang.setLayout(null); // don't do this .use layouts .and even if you use null then
//use bounds to absolutely position .if you use null layout and if you add using `.add()`
//then you will not see those components .
complete code
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class humev extends JFrame implements ActionListener {
private static final int larghezza = 1300;
private static final int altezza = 1000;
private static final String nome = "Human Evolution";
private final JLabel lab;
private final JButton gioca;
private final JPanel pang;
public humev() {
super(nome);
pang = new JPanel();
//pang.setLayout(new FlowLayout()); // use appropriate layout .for example flowlayout.since flowlayout is default layout for jpanel you can avoid it.but don't use null
gioca = new JButton("Gioca!");
gioca.addActionListener(this);
lab = new JLabel("lable");
pang.add(gioca);
pang.add(lab);
add(pang); // add pang panel to frame
Dimension dim_finestra = new Dimension(larghezza, altezza);
setPreferredSize(dim_finestra);
setMaximumSize(dim_finestra);
//setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
}
public static void main(String[] args) {
humev humev = new humev();
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == gioca) {
lab.setText("Gioco avviato con successo!");
}
}
}
IllegalArgumentException is an Unchecked Exception.
It rises explicitly by API Developer or Programmer to indicate that a method has invoked with illegal argument.
example:
Thread t = new Thread();
t.setPriority(15);
output:
RuntimeException: IllegalArgumentExcepion
The valid range of Thread priority is 1 to 10, if we are trying to set the priority with any other value, then we will get IllegalArgumentException.

Portrayals not Showing

EDIT: Everything displays correctly when either field or hunterField hold no objects in any location. field and hunterField both exclusively hold objects which extend the same class, so I guess it may have something to do with inheritance...?
I have created a simple Agent-Based Model using MASON. The back-end works find, but when I try displaying my agents only "wall" agents are displayed. (Wall Portrayal) My code is below... Any idea?
package sim.app.celai;
import java.awt.Color;
import javax.swing.JFrame;
import sim.app.tutorial3.Tutorial3WithUI;
import sim.display.Controller;
import sim.display.Display2D;
import sim.display.GUIState;
import sim.portrayal.grid.SparseGridPortrayal2D;
import sim.util.Bag;
public class FieldWithGUI extends GUIState {
public Display2D display;
public JFrame frame;
SparseGridPortrayal2D hunterPortrayal = new SparseGridPortrayal2D();
SparseGridPortrayal2D wallPortrayal = new SparseGridPortrayal2D();
SparseGridPortrayal2D childPortrayal = new SparseGridPortrayal2D();
public FieldWithGUI() {
super(new Field(System.currentTimeMillis()));
}
public void setupPortrayals() {
childPortrayal.setField(((Field) state).field);
hunterPortrayal.setField(((Field) state).hunterField);
wallPortrayal.setField(((Field) state).wallField);
childPortrayal.setPortrayalForAll(new sim.portrayal.simple.OvalPortrayal2D(Color.blue));
hunterPortrayal.setPortrayalForAll(new sim.portrayal.simple.OvalPortrayal2D(Color.red));
wallPortrayal.setPortrayalForAll(new sim.portrayal.simple.OvalPortrayal2D(Color.green));
display.reset();
display.repaint();
}
public void quit()
{
super.quit();
if (frame!=null) frame.dispose();
frame = null; // let gc
display = null; // let gc
}
public static void main(String[] args)
{
new FieldWithGUI().createController();
}
public void start()
{
super.start();
// set up our portrayals
setupPortrayals();
}
public void init(Controller c)
{
super.init(c);
// Make the Display2D. We'll have it display stuff later.
display = new Display2D(400,400,this); // at 400x400, we've got 4x4 per array position
frame = display.createFrame();
c.registerFrame(frame); // register the frame so it appears in the "Display" list
frame.setVisible(true);
// specify the backdrop color -- what gets painted behind the displays
display.setBackdrop(Color.black);
// attach the portrayals
display.attach(childPortrayal, "children");
display.attach(hunterPortrayal, "hunter");
display.attach(wallPortrayal, "wall");
}
}

JRadioButton: how to replace text by IconImage?

I want to replace the text in my radio button list by an icon.
I've tried this:
rotateButton = new JRadioButton(rotateIcon.getImage());
But this replaces the radio button and text by the icon. I would like to keep the radio button and display the image.
What should I do?
What I'm currently getting is:
But I want it to end up with this:
Create a JRadioButton with no text and put a JLabel with the image next to it. You can also create a class to hide complexity.
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ChangeListener;
public class RadioButtonWithImage extends JPanel {
private JRadioButton radio = new JRadioButton();
private JLabel image;
public RadioButtonWithImage(Icon icon) {
image = new JLabel(icon);
add(radio);
add(image);
}
public void addToButtonGroup(ButtonGroup group) {
group.add(radio);
}
public void addActionListener(ActionListener listener) {
radio.addActionListener(listener);
}
public void addChangeListener(ChangeListener listener) {
radio.addChangeListener(listener);
}
public Icon getImage() {
return image.getIcon();
}
public void setImage(Icon icon) {
image.setIcon(icon);
}
} // end class RadioButtonWithImage
public JRadioButton(String text, Icon icon) and simple example here
I just reproduced your described behavior using this source:
import java.awt.Image;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.net.URL;
class RadioWithImage {
public static void main(String[] args) throws Exception {
URL url = new URL("http://www.gravatar.com/avatar/" +
"a1ab0af4997654345d7a949877f8037e?s=128");
Image image = ImageIO.read(url);
final ImageIcon imageIcon = new ImageIcon(image);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JRadioButton radioButton = new JRadioButton("A.T.", imageIcon);
JOptionPane.showMessageDialog(null, radioButton);
}
});
}
}
It seems like a bug to me, though I cannot recall seeing a radio with an icon. How are they supposed to look?
Time to reach into my 'box of hacks'.
import javax.swing.*;
class RadioWithImage {
public static void main(String[] args) throws Exception {
String url = "http://www.gravatar.com/avatar/" +
"a1ab0af4997654345d7a949877f8037e?s=128";
final String html = "<html><body><img src='" +
url +
"' width=128 height=128>";
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JRadioButton radioButton = new JRadioButton(html);
JOptionPane.showMessageDialog(null, radioButton);
}
});
}
}
This technique will not work if:
The use-case requires other types of icons (pressed, roll-over, selected etc.)
The button is disabled (it will render incorrectly).
There is no radio button constructor that allows an image as the content argument instead of a text. The only way to replace the text of a radio button by an image it is generate html and pass it as an argument to the default constructor.
import javax.swing.*;
class RadioWithImage {
public static void main(String[] args) throws Exception {
URL url = Windows_ChordsGenerator.class.getResource("/images/img1.png");
final String html = "<html><body><img src='" + url.toString() +"'>";
JRadioButton radioButton = new JRadioButton(html);
}
}

Categories

Resources