cooldudsk
cooldudsk

Reputation: 65

How to show multiple messages within a single joptionpane message box?

I am new to these forums so I apologize in advance for any confusion/mistakes I might make.

I am trying to make a single message box with JOptionPane, and I want it to display many things such as:

JOptionPane.showMessageDialog( null, string1, int1, string2, int2); 

Is there a way to use these message boxes to print multiple strings and ints within one box?

To clarify, basically I want the printlns at the bottom to be converted into one message box.

I am new to java.

import javax.swing.*;

public class PayrollJoption {

    public static void main(String[] args) {

        String nameFirst;
        nameFirst = JOptionPane.showInputDialog("Enter Your First Name: ");

        String nameLast;
        nameLast = JOptionPane.showInputDialog("Enter Your Last Name: ");

        int hourlyRate;
        String hourlyRateString;
        hourlyRateString = JOptionPane.showInputDialog("Enter Your Hourly Rate: ");
        hourlyRate = Integer.parseInt(hourlyRateString);

        int hoursWorked;
        String hoursWorkedString;
        hoursWorkedString = JOptionPane.showInputDialog("Enter Your Hours Worked: ");
        hoursWorked = Integer.parseInt(hoursWorkedString);

        double grosspay = hourlyRate * hoursWorked; // calculating gross pay
        double taxWithholding = grosspay * 0.28; // calculating tax withholdings
        double netPay = grosspay - taxWithholding; // calculating net pay

        JOptionPane.showMessageDialog( null, "Name: ");  
                // This is the message box i was referring to

        System.out.println("Name: " + nameFirst + " " + nameLast); // printing full name
        System.out.println("Your Gross Pay Is:  " + "$" + grosspay); // printing gross pay
        System.out.println("Your Income Tax Is (28%) "); // showing tax percentage
        System.out.println("Your Tax WithHolding is:  " + "$" + taxWithholding); // showing tax withholdings
        System.out.println("Your Net Pay Is: " + "$" + netPay); // printing net pay
        System.out.println(" "); // skipping line for space
        System.out.println("You Need A New Job!"); // printing text

    }

}

Upvotes: 1

Views: 8264

Answers (2)

frankfg
frankfg

Reputation: 625

welcome to the forum, in the strings you can use \n to break message into lines:

"First line \n second line"

Also you can use HTML and the content of the string will be rendered:

"<html><b> a bold text </b></html>"

Upvotes: 2

Am_I_Helpful
Am_I_Helpful

Reputation: 19168

You can try this :-

String to_print="Name: " + nameFirst+ " "+ nameLast +"\n"+
"Your Gross Pay Is:  "+ "$" + grosspay+"\n"+
"Your Income Tax Is (28%) "+"\n"+
"Your Tax WithHolding is:  "+ "$"+ taxWithholding+"\n"+
"Your Net Pay Is: "+ "$"+ netPay+"\n"+" \n"+
"You Need A New Job!";   //printing text

Remove all the bottom System.out.println() lines and then call,

JOptionPane.showMessageDialog( null,to_print);

I hope this works for you.If it doesn't,please comment below!

Upvotes: 2

Related Questions