0

I am trying to allow a user click a btnGenerate which then generates a random number that is assigned to a phrase that can be called to print out the phrase in a box below the button in the application window in eclipse.

The problem is that the random statement comes up on the Eclipse console instead of the textbox on my GUI.

Any help is appreciated. Here is my code so far:

//generate crime button

JButton generateBtn = new JButton("Generate Crime");
generateBtn.setBackground(Color.LIGHT_GRAY);
generateBtn.setFont(new Font("HGHeiseiKakugothictaiW3", Font.BOLD, 20));
GridBagConstraints gbc_generateBtn = new GridBagConstraints();

gbc_generateBtn.fill = GridBagConstraints.BOTH;
gbc_generateBtn.insets = new Insets(0, 0, 5, 5);
gbc_generateBtn.gridx = 15;
gbc_generateBtn.gridy = 5;

frmHeroVillains.getContentPane().add(generateBtn, gbc_generateBtn);
generateBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {

        /*random number generator that generates a number between 1-4 and outputs a random crime to the updatePane depending on which 
        number was generated */
        int number =  ((int) (Math.random()*4)+1);
        switch (number) {
        case 1:
            System.out.println("Jewelry Heist on main street!");
            break;
        case 2:
            System.out.println("Mugging in China town!");
            break;
        case 3:
            System.out.println("Boeing 247 - Hijacked!");
        default:
            System.out.println("Nothing to Report.");
            break;


        }
    ;
    JLabel updateLabel = new JLabel("UPDATE ALERT.... " + number);
            GridBagConstraints gbc_updateLabel = new GridBagConstraints();
            gbc_updateLabel.gridheight = 3;
            gbc_updateLabel.insets = new Insets(0, 0, 5, 5);
            gbc_updateLabel.gridx = 15;
            gbc_updateLabel.gridy = 12;
            frmHeroVillains.getContentPane().add(updateLabel, gbc_updateLabel);}    }
);
5
  • So what exactly is the issue?
    – Mureinik
    May 10, 2015 at 21:32
  • the random statement comes up on the eclipse console instead of the textbox on my GUI May 10, 2015 at 21:35
  • 1
    What "textbox" are you hoping to update? May 10, 2015 at 22:10
  • 2
    Where do you expect output from System.out.println() if not the console? If you want it to go to your textbox then call setText() on the textbox where you want it to appear.
    – swingMan
    May 10, 2015 at 22:21
  • @swingMan append instead of setText probably. May 11, 2015 at 4:48

2 Answers 2

1

You need to redirect the System.out.println(...) message to your own component.

Check out the Message Console for one approach to doing this. You can redirect the output to a JTextArea or JTextPane.

0
public static void main(String[] args) throws Exception {

//Must throws Exception

    JPanel myOutput = new JPanel();
    myOutput.setVisible(true);
    myOutput.setBackground(Color.GRAY);

    JTextArea mynewText = new JTextArea();
    myOutput.add(mynewText);

    URL oracle = new URL("http://www.oracle.com/");
    BufferedReader in = new BufferedReader(new InputStreamReader(
            oracle.openStream()));
//InputStreamReader wrapped in BufferedReader

    String inputLine;
    inputLine = in.readLine();
    mynewText.setText(inputLine);

    in.close();

//In the target window class
mainWindow.add(myOutput);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.