Search code examples
javaswingjbuttonjtextfield

Formatting of components in Swing


I have just started programming in Swing. I am making a Fitness application using Swing. I have 2 questions:

  1. Whenever I click 'Calculate Scales' it should display the results only once, but it is displaying as many times as I click. How can I solve this?
  2. How can I set limit of JTextField up to 3 digits in my code?

Here is my code. Correct me if I am going wrong somewhere. Suggest me where I can make improvements.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;//since obsolete
class H1 extends JFrame implements ActionListener
{
 JLabel l1,l2,l3,l4,l5;
 JTextField t1,t2,t3;
 JRadioButton r1,r2;
ButtonGroup bg1;
JLabel bmr;
  JLabel bmi;
int click=0;
JButton b1,b2;Container c1;
 public static void main(String args[])
 {

  H1 j1 =new H1();
  j1.setTitle("Personal  Scales ");
  j1.setSize(1000,1000);
  j1.setVisible(true);
  j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  j1.pack();
 }

H1()
{

c1=this.getContentPane();
setLayout(new BoxLayout(c1,BoxLayout.Y_AXIS));


JPanel j1 = new JPanel();
j1.setLayout(new FlowLayout());
t1 = new JTextField(10);
l1=new JLabel("Height");
j1.add(l1);
j1.add(t1);
c1.add(j1);


JPanel j2 = new JPanel();
j2.setLayout(new FlowLayout());
l2=new JLabel("Weight");
t2 = new JTextField(10);
j2.add(l2);
j2.add(t2);
c1.add(j2);



JPanel j3 = new JPanel();
j3.setLayout(new FlowLayout());
l3=new JLabel("Age");
t3 = new JTextField(10);
j3.add(l3);
j3.add(t3);
c1.add(j3);


JPanel j4 = new JPanel();
j4.setLayout(new FlowLayout());
l4=new JLabel("Sex");
bg1=new ButtonGroup();
r1=new JRadioButton("M");
r2=new JRadioButton("F");
bg1.add(r1);//button grouping is done to avoid multiple selection of radio button
bg1.add(r2);
j4.add(l4);
j4.add(r1);
j4.add(r2);
c1.add(j4);


JPanel j5 = new JPanel();
j5.setLayout(new FlowLayout());

b1=new JButton("Calculate Scales");//creating instance of JButton  
j5.add(b1);
c1.add(j5);
b1.addActionListener(this);

}

public void actionPerformed(ActionEvent e)
 { 

  click +=1;
  //JLabel bmr;
  //JLabel bmi;
  long ht=Long.parseLong(t1.getText());
  long wt=Long.parseLong(t2.getText());
  long age=Long.parseLong(t3.getText());
  //JPanel j6 = new JPanel();

  //j6.setLayout(new FlowLayout());


  double bmi1 = wt/Math.pow((ht * 0.01),2);

  String bmi2 = Double.toString(bmi1);//since double  cant be added to container 

  bmi=new JLabel("BMI     "+bmi2); 

  //c1.add(j6);
 // JPanel j7 = new JPanel();
  //j7.setLayout(new FlowLayout());

  double bmr1;String bmr2;

if(r1.isSelected())
  bmr1 = 66.47 +(13.75 * wt) + (5.003 * ht) - (6.775 * age);

else

  bmr1 = 655.1 +(9.563 * wt) + (1.85 * ht) - (4.676 * age);


 bmr2=Double.toString(bmr1);
 bmr=new JLabel("BMR     "+bmr2);

if(click>1)
  {
   c1.remove(bmi);
   c1.remove(bmr);
  }

/*j6.add(bmi);
j6.add(bmr);*/

  c1.add(bmi);
  c1.add(bmr);

}
}

Solution

  • I'm also newer to java, so correct me if I'm wrong and cause of that, I can't write you a comment and have to write an answer ;)

    As I can see it, you make a new JLabel each time you click the Button.
    Try to use eg. bmi.setText("BMI here" + bmi2); for your result description through setText() you only change the Text in this label. And also as variable declaration use JLabel bmi = null;

    Maybe you also can add a JPanel to display your result to get the JLabel a fixed position and initializing it.

    I hope that I can help you ;)
    Zorian