Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how to output using JLabel?

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Location
    Malaysia
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question how to output using JLabel?

    here my question...
    When user clicks CALCULATE button, display total amount of loan with interest (use JLabel to display the text).
    But if user has entered an invalid input (such as character, string or symbol), promt user that he or she has entered an invalid input.

    so now, here my code:
    package projek;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class loan extends JFrame implements ActionListener{
        JButton calculate;
        JTextField pinjaman, interest;
        double hasil;
     
        loan(){
        Container kotak=getContentPane();
        kotak.setLayout(new GridLayout(3,3));
     
        JLabel loanAmount=new JLabel("Jumlah pinjaman");
        kotak.add(loanAmount);
        pinjaman=new JTextField(10);
        kotak.add(pinjaman);
     
        JLabel interesting=new JLabel("Faedah(Riba)");
        kotak.add(interesting);
        interest=new JTextField(10);
        kotak.add(interest);
     
        calculate=new JButton("Kira");
        kotak.add(calculate);
        calculate.addActionListener(this);
     
        JLabel jawapan=new JLabel("Result: TOTAL LOAN WITH INTEREST IS "+hasil);
        kotak.add(jawapan);
     
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("LOAN CALCULATION");
        pack();
        show();
        }
     
     
        public void actionPerformed(ActionEvent e){
            Object butangtekan=e.getSource();
            if(butangtekan==calculate){
                try{
                 double untukloan=Double.parseDouble(pinjaman.getText());
                 double faedah=Double.parseDouble(interest.getText()), riba, hasil;
     
                 riba=(faedah*untukloan)/100;
                 hasil=riba+untukloan;
     
     
                }
                catch(Exception a){
                    JOptionPane.showMessageDialog(null, "Wrong input, must be numeric", "Error",JOptionPane.ERROR_MESSAGE);
                }                  
               }
            }
     
        public static void main(String args[]){
            new loan();
        }
    }
    so can somebody show me how to input the result(hasil) into the page using JLabel. if got any wrong pls tell me...
    sorry for my bad english...


  2. #2
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: how to output using JLabel?

    Hi qaromi,

    I have made some changes in the program. Please first compile and execute it. I have commented in front of the lines where i have made the changes. Please go through it.

    package projek;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;

    public class loan extends JFrame implements ActionListener{
    JButton calculate;
    JTextField pinjaman, interest;
    double hasil;
    JLabel loanAmount,interesting,jawapan; //See here i have Declared all the labels globally as we need them for
    //the whole class i.e. for all its methods.

    loan(){
    Container kotak=getContentPane();
    kotak.setLayout(new GridLayout(3,3));

    loanAmount=new JLabel("Jumlah pinjaman");
    kotak.add(loanAmount);
    pinjaman=new JTextField(10);
    kotak.add(pinjaman);

    interesting=new JLabel("Faedah(Riba)");
    kotak.add(interesting);
    interest=new JTextField(10);
    kotak.add(interest);

    calculate=new JButton("Kira");
    kotak.add(calculate);
    calculate.addActionListener(this);

    jawapan=new JLabel("Result: TOTAL LOAN WITH INTEREST IS "+hasil);
    kotak.add(jawapan);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setTitle("LOAN CALCULATION");
    pack();
    show();
    }


    public void actionPerformed(ActionEvent e){
    Object butangtekan=e.getSource();
    if(butangtekan==calculate){
    try{
    double untukloan=Double.parseDouble(pinjaman.getText());
    double faedah=Double.parseDouble(interest.getText()), riba; //See here i have removed the hasil variable
    // as it is already declared globally. so no need
    riba=(faedah*untukloan)/100;
    hasil=riba+untukloan;
    jawapan.setText("Result: TOTAL LOAN WITH INTEREST IS "+hasil); //setText(String) is a method which
    // sets the the given text in the given
    //component
    }
    catch(Exception a){
    JOptionPane.showMessageDialog(null, "Wrong input, must be numeric", "Error",JOptionPane.ERROR_MESSAGE);
    }
    }
    }

    public static void main(String args[]){
    loan n=new loan();
    }
    }

    Regards,
    guptapr

Similar Threads

  1. Replies: 3
    Last Post: August 19th, 2009, 12:30 PM
  2. Redirect error and output stream using java program
    By leenabora in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: June 16th, 2009, 05:12 AM
  3. Display output from a file using regular expression
    By jazz2k8 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 29th, 2008, 10:33 AM
  4. [SOLVED] Java program error in displaying Output
    By crazydeo in forum AWT / Java Swing
    Replies: 9
    Last Post: May 14th, 2008, 11:42 AM