How can I setText for a JLabel so it prints multiple print statements from a void method?

srocode

I will give you a snippet of my code:

JButton button = new JButton("Flip");
button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        Flips flip = new Flips();

        flipsLabel.setText(Arrays.toString(flip.hAndT));

        flip.tailsCount();
        tailsLabel.setText("Total Tails " + Integer.toString(flip.tCount));

        flip.headsCount();
        headsLabel.setText("Total Tails " + Integer.toString(flip.hCount));
    }
});

The above is all working because the tCount and hCount are variables so I can set them as text in the JLabels.

However, I have the following method that I want to print to a JLabel as well:

public static void headsCount() {
    System.out.println();
    System.out.println("Heads");
    System.out.println("=====");

    for (int i = 0; i < HandT.length; i++) {
        if (hAndT[i].equals("Head")) {
            System.out.println("index " + Integer.toString(i));
            hCount++;
        }
    }
}

The output is multiple lines for the indices where heads falls in an array. I cannot print this void method by setting it as text for a button action because it is a primitive type.

Will I have to turn the method into a return type and return an array of some sort with this information so I can print it to a JLabel?

camickr

Will I have to turn the method into a return type

Yes, you would probably return a String.

Instead of using the System.out.println(), you might use a StringBuilder and keep appending the text to the StringBuilder.

Then you can simply use the toString() method of the StringBuilder to return the complete String.

so I can print it to a JLabel?

I doubt you would use a JLabel, since a JLabel is not designed to display multiple lines of text. You might instead use a JTextArea.

If you want to use a JLabel then you could still use the StringBuilder to build HTML to display in the Label.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JLabel.setText("__") will not print multiple times while in for loop

From Dev

how can i redirect from void method to a view?

From Dev

How can I insert a Print button that prints a form in a webpage

From Dev

How to test a void method that prints on console in jUnit?

From Dev

How to disable print statements conveniently so that pythonw can run?

From Dev

How can I change this code so it prints on the same line?

From Dev

How can I 'provide' a void method in Dagger

From Dev

How can I print multiple elements from an array inJavaSCript?

From Dev

how can I print multiple odt files from the command line?

From Dev

SqlDataReader prints multiple rows, how can I split?

From Dev

How can I see print() statements in behave (BDD)

From Dev

How can I see Debug.Print() statements?

From Dev

hello, how can i modify this assembly code to print sum from 5 - 10. It currently prints from 1 to 10. Can any assembly programmer help me?

From Dev

How can I run a class method inside of a public void from a different class?

From Dev

How can I prevent statements from being output multiple times when SQL tracing is enabled with DBI?

From Dev

My loop prints multiple times. How do I get it to print once?

From Dev

My loop prints multiple times. How do I get it to print once?

From Java

How can I improve readability and length of a method with many if statements?

From Dev

How can I improve readability and length of a method with many if statements?

From Java

How can I get my print statement to work to where it prints the average score and the correlating letter grade?

From Dev

How can I mock a void method to throw an exception?

From Dev

How can I simplify and enhance multiple if-statements usage?

From Dev

PDO: How can I run multiple prepared statements in a transaction?

From Dev

How to run multiple NSURLSessionDownloadTask's so I can download multiple user photo streams from Instagram?

From Dev

How can I print infix from tree?

From Dev

How to get a return value from a method when the method contains multiple if statements with multiple returns?

From Dev

How to get a return value from a method when the method contains multiple if statements with multiple returns?

From Dev

How can I print multiple file types in Windows from the command line?

From Dev

Why is print so slow in Python 3.3 and how can I fix it?

Related Related

  1. 1

    JLabel.setText("__") will not print multiple times while in for loop

  2. 2

    how can i redirect from void method to a view?

  3. 3

    How can I insert a Print button that prints a form in a webpage

  4. 4

    How to test a void method that prints on console in jUnit?

  5. 5

    How to disable print statements conveniently so that pythonw can run?

  6. 6

    How can I change this code so it prints on the same line?

  7. 7

    How can I 'provide' a void method in Dagger

  8. 8

    How can I print multiple elements from an array inJavaSCript?

  9. 9

    how can I print multiple odt files from the command line?

  10. 10

    SqlDataReader prints multiple rows, how can I split?

  11. 11

    How can I see print() statements in behave (BDD)

  12. 12

    How can I see Debug.Print() statements?

  13. 13

    hello, how can i modify this assembly code to print sum from 5 - 10. It currently prints from 1 to 10. Can any assembly programmer help me?

  14. 14

    How can I run a class method inside of a public void from a different class?

  15. 15

    How can I prevent statements from being output multiple times when SQL tracing is enabled with DBI?

  16. 16

    My loop prints multiple times. How do I get it to print once?

  17. 17

    My loop prints multiple times. How do I get it to print once?

  18. 18

    How can I improve readability and length of a method with many if statements?

  19. 19

    How can I improve readability and length of a method with many if statements?

  20. 20

    How can I get my print statement to work to where it prints the average score and the correlating letter grade?

  21. 21

    How can I mock a void method to throw an exception?

  22. 22

    How can I simplify and enhance multiple if-statements usage?

  23. 23

    PDO: How can I run multiple prepared statements in a transaction?

  24. 24

    How to run multiple NSURLSessionDownloadTask's so I can download multiple user photo streams from Instagram?

  25. 25

    How can I print infix from tree?

  26. 26

    How to get a return value from a method when the method contains multiple if statements with multiple returns?

  27. 27

    How to get a return value from a method when the method contains multiple if statements with multiple returns?

  28. 28

    How can I print multiple file types in Windows from the command line?

  29. 29

    Why is print so slow in Python 3.3 and how can I fix it?

HotTag

Archive