Results 1 to 12 of 12
  1. #1
    furieux is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Help, I can't make the JButton to work properly

    As in the title, I can't initialize pressing Vrd1/2 to give me the result in Vrd1/2Field

    ---if (e.getSource() == V1) {
    double Vrd1 = computeVrd1(bet, b, h, a, n, d);
    Vrd1Field.setValue(new Double(Vrd1)); ---


    ---if (e.getSource() == V2) {
    double Vrd2 = computeVrd2(h, met, ns, ss, rs, a, d);
    Vrd2Field.setValue(new Double(Vrd2)); ---


    I would apreciate any help with this

    Java Code:
    package paczka;
    
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    
    import java.beans.PropertyChangeListener;
    import java.beans.PropertyChangeEvent;
    
    import java.text.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    
    public class Help extends JPanel
                                        implements PropertyChangeListener, ActionListener{
    
    
        private static final long serialVersionUID = 1L;
        //Values for the fields
        private static  double bet;
        private static  double b;
        private static  double h;
        private static  int a;
        private static  int d;
        private static  int n;
        private static  double met;
        private static  int ss;
        private static  int rs;
        private static  int ns;
        
        
        //Values for the buttons
        private static JButton V1;
    	private static JButton V2;
    
    
    
        //Labels to identify the fields
        private static  JLabel betLabel;
        private static JLabel bLabel;
        private static  JLabel hLabel;
        private static  JLabel aLabel;
        private static  JLabel nLabel;
        private static  JLabel dLabel;
        private static  JLabel Vrd1Label;
        private static  JLabel metLabel;
        private static  JLabel ssLabel;
        private static  JLabel rsLabel;
        private static  JLabel nsLabel;
        private static  JLabel Vrd2Label;
    
        //Strings for the labels
        private static String betString = "Klasa betonu B - (np. 15: B15, ... 37: B37) ";
        private static String bString = "Szerokosc belki [mm] :  ";
        private static String hString = "Wysokosc belki [mm] :  ";
        private static String aString = "Otulenie [mm] :  ";
        private static String nString = "Liczba pretow zbrojenia glownego : ";
        private static String dString = "Srednica pretow zbrojenia glownego [mm]  :  ";
        private static String Vrd1String = "Vrd1 [kN] :  ";
        private static String metString = "Klasa Stali 1: A0, 2: AI, 3: AII, 4: AIII, 5: AIIIN : ";
        private static String nsString = "Liczba ramion strzemion :  ";
        private static String ssString = "Srednica strzemion [mm] :  ";
        private static String rsString = "Rozstaw stzremion [mm] :  ";
        private static String Vrd2String = "Vrd2 [kN] :  ";
    
        //Fields for data entry
        private static JFormattedTextField betField;
        private static JFormattedTextField bField;
        private static JFormattedTextField hField;
        private static JFormattedTextField aField;
        private static JFormattedTextField nField;
        private static JFormattedTextField dField;
        private static JFormattedTextField Vrd1Field;
        private static JFormattedTextField metField;
        private static JFormattedTextField nsField;
        private static JFormattedTextField ssField;
        private static JFormattedTextField rsField;
        private static JFormattedTextField Vrd2Field;
    
        //Formats to format and parse numbers
    
        private static NumberFormat Vrd1Format;
        private static NumberFormat Vrd2Format;
       
        //Create and set up number formats. These objects also
        //parse numbers input by user.
        @SuppressWarnings("unused")
    	private void setUpFormats() {
           
            Vrd1Format = NumberFormat.getNumberInstance();
            Vrd1Format.setMaximumFractionDigits(2);
            Vrd2Format = NumberFormat.getNumberInstance();
            Vrd2Format.setMaximumFractionDigits(2);
        }
        
        
        
        
        public Help() {
        	
    	    	double Vrd1 = computeVrd1(bet, b, h, a, n, d);
    	        double Vrd2 = computeVrd2(h, met, ns, ss, rs, a, d);
        	
        	  betLabel = new JLabel(betString);
              betLabel.setBounds(10, 50, 300, 20);
              bLabel = new JLabel(bString);
              bLabel.setBounds(10, 80, 300, 20);
              hLabel = new JLabel(hString);
              hLabel.setBounds(10, 110, 300, 20);
              aLabel = new JLabel(aString);
              aLabel.setBounds(10, 140, 300, 20);
              nLabel = new JLabel(nString);
              nLabel.setBounds(10, 170, 300, 20);
              dLabel = new JLabel(dString);
              dLabel.setBounds(10, 200, 300, 20);
              Vrd1Label = new JLabel(Vrd1String);
              Vrd1Label.setBounds(10, 240, 300, 20);
              metLabel = new JLabel(metString);
              metLabel.setBounds(410, 50, 300, 20);
              nsLabel = new JLabel(nsString);
              nsLabel.setBounds(410, 80, 300, 20);
              ssLabel  = new JLabel(ssString);
              ssLabel.setBounds(410, 110, 300, 20);
              rsLabel = new JLabel(rsString);
              rsLabel.setBounds(410, 140, 300, 20);
              Vrd2Label = new JLabel(Vrd2String);
              Vrd2Label.setBounds(410, 180, 300, 20);
             
           
              
              //Create the text fields and set them up.
    
              betField = new JFormattedTextField();
              betField.setValue(new Double(bet));
              betField.setColumns(10);
              betField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      betField.setText("");
                  }});
              betField.setBounds(270, 50, 50, 20);
    
              metField = new JFormattedTextField();
              metField.setValue(new Double(met));
              metField.setColumns(10);
              metField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      metField.setText("");
                  }});
              metField.setBounds(670, 50, 50, 20);
    
              bField = new JFormattedTextField();
              bField.setValue(new Double(b));
              bField.setColumns(10);
              bField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      bField.setText("");
                  }});
              bField.setBounds(270, 80, 50, 20);
    
              hField = new JFormattedTextField();
              hField.setValue(new Double(h));
              hField.setColumns(10);
              hField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      hField.setText("");
                  }});
              hField.setBounds(270, 110, 50, 20);
    
              aField = new JFormattedTextField();
              aField.setValue(new Integer(a));
              aField.setColumns(10);
              aField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      aField.setText("");
                  }});
              aField.setBounds(270, 140, 50, 20);
    
              nField = new JFormattedTextField();
              nField.setValue(new Integer(n));
              nField.setColumns(10);
              nField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      nField.setText("");
                  }});
              nField.setBounds(270, 170, 50, 20);
    
              dField = new JFormattedTextField();
              dField.setValue(new Integer(d));
              dField.setColumns(10);
              dField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      dField.setText("");
                  }});
              dField.setBounds(270, 200, 50, 20);
    
              nsField = new JFormattedTextField();
              nsField.setValue(new Integer(ns));
              nsField.setColumns(10);
              nsField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      nsField.setText("");
                  }});
              nsField.setBounds(670, 80, 50, 20);
    
              ssField = new JFormattedTextField();
              ssField.setValue(new Integer(ss));
              ssField.setColumns(10);
              ssField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      ssField.setText("");
                  }});
              ssField.setBounds(670, 110, 50, 20);
    
              rsField = new JFormattedTextField();
              rsField.setValue(new Integer(rs));
              rsField.setColumns(10);
              rsField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      rsField.setText("");
                  }});
              rsField.setBounds(670, 140, 50, 20);
    
              Vrd1Field = new JFormattedTextField(Vrd1Format);
              Vrd1Field.setColumns(10);
              Vrd1Field.setValue(new Double(Vrd1));
              Vrd1Field.setEditable(false);
              Vrd1Field.setForeground(Color.blue);
              Vrd1Field.setFont(new Font("Zmienna" , Font.BOLD, 15));
              Vrd1Field.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      Vrd1Field.setText("");
                  }});
              Vrd1Field.setBounds(270, 240, 70, 20);
    
              Vrd2Field = new JFormattedTextField(Vrd2Format);
              Vrd2Field.setColumns(10);
              Vrd2Field.setValue(new Double(Vrd2));
              Vrd2Field.setEditable(false);
              Vrd2Field.setForeground(Color.blue);
              Vrd2Field.setFont(new Font("Zmienna" , Font.BOLD, 15));
              Vrd2Field.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      Vrd2Field.setText("");
                  }});
              Vrd2Field.setBounds(670, 180, 70, 20);
    
    
              //Tell accessibility tools about label/textfield pairs.
              betLabel.setLabelFor(betField);
              bLabel.setLabelFor(bField);
              hLabel.setLabelFor(hField);
              aLabel.setLabelFor(aField);
              nLabel.setLabelFor(nField);
              dLabel.setLabelFor(dField);
              Vrd1Label.setLabelFor(Vrd1Field);
              metLabel.setLabelFor(metField);
              nsLabel.setLabelFor(nsField);
              ssLabel.setLabelFor(ssField);
              rsLabel.setLabelFor(rsField);
              Vrd2Label.setLabelFor(Vrd2Field);
    
          
              V1 = new JButton(" Vrd1 ");
              V1.setBounds(270, 280, 70, 20);
              V1.addActionListener(this);
              V1.setToolTipText(" Oblicz Vrd1 ");
                
              V2 = new JButton(" Vrd2 ");
              V2.setBounds(670, 220, 70, 20);
              V2.addActionListener(this);
              V2.setToolTipText(" Oblicz Vrd2 ");
              
        }
             
     
    
            private static void createAndShowGUI() {
    
            JFrame frame = new JFrame("Scinanie belek wg. PN-B-03264:2002 v.1.6");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(null);
            
            //Add contents to the window.
            frame.add(new Help());
            frame.add(betLabel);
            frame.add(bLabel);
            frame.add(hLabel);
            frame.add(aLabel);
            frame.add(dLabel);
            frame.add(nLabel);
            frame.add(Vrd1Label);
            frame.add(metLabel);
            frame.add(nsLabel);
            frame.add(ssLabel);
            frame.add(rsLabel);
            frame.add(Vrd2Label);
    
    
            frame.add(betField);
            frame.add(bField);
            frame.add(hField);
            frame.add(aField);
            frame.add(dField);
            frame.add(nField);
            frame.add(Vrd1Field);
            frame.add(metField);
            frame.add(nsField);
            frame.add(ssField);
            frame.add(rsField);
            frame.add(Vrd2Field);
       
            frame.add(V1);
            frame.add(V2);
            
            //Display the window.
            
            frame.pack();
            frame.setSize(800, 380);
            frame.setVisible(true);
            
      
            }
    
            static double computeVrd1(double bet, double b, double h, double a, int n, int d) {
                double answer, dh, A1, fctd = 0;
    
                if (bet == 15){
                fctd = 0.73;
                } else if (bet == 20){
                fctd = 0.87;
                } else if (bet == 25){
                fctd = 1.00;
                } else if (bet == 30){
                fctd = 1.20;
                } else if (bet == 37){
                fctd = 1.33;
                } else if (bet == 45){
                fctd = 1.47;
                } else if (bet == 50){
                fctd = 1.67;
                } else if (bet == 55){
                fctd = 1.80;
                } else if (bet == 60){
                fctd = 1.93;
                }
    
                dh = h - a - d / 2;
                A1 = (1.2 + 40 * (n * d) / (b * dh));
                answer =((0.35 * 1.0 * fctd * A1) * b * dh) / 1000;
                return answer;
            }
    
            static double computeVrd2(double h, double met, double ns, double ss, double rs, double a, double d) {
                double answer1, dh1, fyd = 0;
    
                if (met == 1){
                    fyd = 190;
                } else if (met == 2){
                    fyd = 210;
                } else if (met == 3){
                    fyd = 310;
                } else if (met == 4){
                    fyd = 350;
                } else if (met == 5){
                    fyd = 420;
                }
    
                dh1 = h - a - d / 2;
                answer1 = (((ns * 3.14159265 * (ss * ss) / 4 * fyd ) / rs) * 0.9 * dh1) / 1000;
                return answer1;
            }
    
            
            public void actionPerformed(ActionEvent e)
            {
          	  if (e.getSource() == V1) {
          	  double Vrd1 = computeVrd1(bet, b, h, a, n, d);
          	    Vrd1Field.setValue(new Double(Vrd1));
          	  }
          	  else {
                JOptionPane.showMessageDialog(Help.this, "Nie liczy sie", "Test", JOptionPane.ERROR_MESSAGE);
                 	}
          	  if (e.getSource() == V2) {
              double Vrd2 = computeVrd2(h, met, ns, ss, rs, a, d);
                Vrd2Field.setValue(new Double(Vrd2));
                }
          	  else {
                JOptionPane.showMessageDialog(Help.this, "Nie liczy sie", "Test", JOptionPane.ERROR_MESSAGE);
          	  		}    
            }  
    
            
            public static void main(String[] args) {
            
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
               
                    createAndShowGUI();
                }
            });
        }
    
    
          
    
    
    		@Override
    		public void propertyChange(PropertyChangeEvent evt) {
    			// TODO Auto-generated method stub
    			
    		}
    
    
    }
    Last edited by furieux; 01-07-2012 at 02:54 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Help, I can't make the JButton to work properly

    Please Edit your post and wrap your code with[code]<YOUR CODE HERE>[/code] to preserve formatting.
    Unformatted code is hard to read.

    If you want anything to happen when you press a button, you need to add an actionlistener to that button.

    Add a println to your action listener method that will print a message if it is called.
    Last edited by Norm; 01-07-2012 at 02:55 PM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help, I can't make the JButton to work properly

    As an aside, none of the variables in your program above should be static, and your GUI should be created in a non-static context. In other words, components shouldn't be added together within a static method, but instead within the class's constructor or some other instance method (for example an "initComponents" method).

    Edit
    If this were my application, I'd have the goal of the class be to create a JPanel which holds the whole GUI, and I'd use the layout managers to assist me in doing this and would avoid absolute positioning. In my static createAndShow method, I'd create an instance of this class (again either it's a JPanel or it has a method that allows me to extract a JPanel), and then add it to a JFrame and display the JFrame. That last part would be the only GUI-building that I'd do in the static method.
    Last edited by Fubarable; 01-07-2012 at 03:50 PM.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Help, I can't make the JButton to work properly

    Did you add some printlns to show where the execution flow goes and to show the values of the variables that are being used?
    What was printed out when you executed the program?

  5. #5
    furieux is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Help, I can't make the JButton to work properly

    Quote Originally Posted by Norm View Post
    Did you add some printlns to show where the execution flow goes and to show the values of the variables that are being used?
    What was printed out when you executed the program?
    The program executed without errors, the problem seems to be that when i press Vrd1 button the Vrd1Field is not bieng set:
    Vrd1Field.setValue(new Double(Vrd1));
    when I change it:
    Vrd1Field.setValue(10); I recive 10 in the Vrd1Field.

    I noticed that I cannot acces any variables in this case:
    Vrd1Field.setValue(b); gives the same result as Vrd1Field.setValue(new Double(Vrd1));
    I must define b = 10; to get it working.
    There is something wrong with accesing variables but I cant find out what.

    I have the same program without JButtons, Vrd1 and Vrd2 are calculated when one of the TextFields is edited.

    Fubarable:
    I used to have non static varaibles, but when i changed GridLayout to AbsoluteLayout in the code it forces me to change to static.
    I'm really a newbie in java (structural enginer) and I'm working with try and test methods to make the code work.
    I'll check "initComponents" mothod and try to put it in use.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help, I can't make the JButton to work properly

    Quote Originally Posted by furieux View Post
    Fubarable:
    I used to have non static varaibles, but when i changed GridLayout to AbsoluteLayout in the code it forces me to change to static.
    No, you are misunderstanding the error message and correcting the wrong thing. Please understand that you absolutely do not have to use static variables but instead need to understand how static and non-static worlds work and interact. The solution is not to make everything static but to not try to access non-static variables in a static context. I strongly urge you to read more in your tutorials and texts about this.

  7. #7
    furieux is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Help, I can't make the JButton to work properly

    on it,
    thanks for the clue :)

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help, I can't make the JButton to work properly

    Please let us know if you solve this, or if you are still stuck. If still stuck, then please let us know what still confuses you.

    Much luck!

  9. #9
    furieux is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Help, I can't make the JButton to work properly

    ok, I think I managed to "to access non-static variables in a static context" like you said, but I'm still stuck with the non-working properly buttons.

    I tryed all the ideas I had earlier but still

    Any more hints :) ?



    Java Code:
    package paczka;
    
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    
    import java.beans.PropertyChangeListener;
    import java.beans.PropertyChangeEvent;
    
    import java.text.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    
    public class Help extends JPanel
                                        implements PropertyChangeListener, ActionListener{
    
    
        private static final long serialVersionUID = 1L;
        //Values for the fields
        private double bet;
        private double b;
        private double h;
        private int a;
        private int d;
        private int n;
        private double met;
        private int ss;
        private int rs;
        private int ns;
        
        
        //Values for the buttons
        private JButton V1;
    	private JButton V2;
    
    
    
        //Labels to identify the fields
        private JLabel betLabel;
        private JLabel bLabel;
        private JLabel hLabel;
        private JLabel aLabel;
        private JLabel nLabel;
        private JLabel dLabel;
        private JLabel Vrd1Label;
        private JLabel metLabel;
        private JLabel ssLabel;
        private JLabel rsLabel;
        private JLabel nsLabel;
        private JLabel Vrd2Label;
    
        //Strings for the labels
        private String betString = "Klasa betonu B - (np. 15: B15, ... 37: B37) ";
        private String bString = "Szerokosc belki [mm] :  ";
        private String hString = "Wysokosc belki [mm] :  ";
        private String aString = "Otulenie [mm] :  ";
        private String nString = "Liczba pretow zbrojenia glownego : ";
        private String dString = "Srednica pretow zbrojenia glownego [mm]  :  ";
        private String Vrd1String = "Vrd1 [kN] :  ";
        private String metString = "Klasa Stali 1: A0, 2: AI, 3: AII, 4: AIII, 5: AIIIN : ";
        private String nsString = "Liczba ramion strzemion :  ";
        private String ssString = "Srednica strzemion [mm] :  ";
        private String rsString = "Rozstaw stzremion [mm] :  ";
        private String Vrd2String = "Vrd2 [kN] :  ";
    
        //Fields for data entry
        private JFormattedTextField betField;
        private JFormattedTextField bField;
        private JFormattedTextField hField;
        private JFormattedTextField aField;
        private JFormattedTextField nField;
        private JFormattedTextField dField;
        private JFormattedTextField Vrd1Field;
        private JFormattedTextField metField;
        private JFormattedTextField nsField;
        private JFormattedTextField ssField;
        private JFormattedTextField rsField;
        private JFormattedTextField Vrd2Field;
    
        //Formats to format and parse numbers
    
        private NumberFormat Vrd1Format;
        private NumberFormat Vrd2Format;
       
        //Create and set up number formats. These objects also
        //parse numbers input by user.
        @SuppressWarnings("unused")
    	private void setUpFormats() {
           
            Vrd1Format = NumberFormat.getNumberInstance();
            Vrd1Format.setMaximumFractionDigits(2);
            Vrd2Format = NumberFormat.getNumberInstance();
            Vrd2Format.setMaximumFractionDigits(2);
        }
        
        
        
        
        public Help() {
        	
    	    	double Vrd1 = computeVrd1(bet, b, h, a, n, d);
    	        double Vrd2 = computeVrd2(h, met, ns, ss, rs, a, d);
        	
        	  betLabel = new JLabel(betString);
              betLabel.setBounds(10, 50, 300, 20);
              bLabel = new JLabel(bString);
              bLabel.setBounds(10, 80, 300, 20);
              hLabel = new JLabel(hString);
              hLabel.setBounds(10, 110, 300, 20);
              aLabel = new JLabel(aString);
              aLabel.setBounds(10, 140, 300, 20);
              nLabel = new JLabel(nString);
              nLabel.setBounds(10, 170, 300, 20);
              dLabel = new JLabel(dString);
              dLabel.setBounds(10, 200, 300, 20);
              Vrd1Label = new JLabel(Vrd1String);
              Vrd1Label.setBounds(10, 240, 300, 20);
              metLabel = new JLabel(metString);
              metLabel.setBounds(410, 50, 300, 20);
              nsLabel = new JLabel(nsString);
              nsLabel.setBounds(410, 80, 300, 20);
              ssLabel  = new JLabel(ssString);
              ssLabel.setBounds(410, 110, 300, 20);
              rsLabel = new JLabel(rsString);
              rsLabel.setBounds(410, 140, 300, 20);
              Vrd2Label = new JLabel(Vrd2String);
              Vrd2Label.setBounds(410, 180, 300, 20);
             
           
              
              //Create the text fields and set them up.
    
              betField = new JFormattedTextField();
              betField.setValue(new Double(bet));
              betField.setColumns(10);
              betField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      betField.setText("");
                  }});
              betField.setBounds(270, 50, 50, 20);
    
              metField = new JFormattedTextField();
              metField.setValue(new Double(met));
              metField.setColumns(10);
              metField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      metField.setText("");
                  }});
              metField.setBounds(670, 50, 50, 20);
    
              bField = new JFormattedTextField();
              bField.setValue(new Double(b));
              bField.setColumns(10);
              bField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      bField.setText("");
                  }});
              bField.setBounds(270, 80, 50, 20);
    
              hField = new JFormattedTextField();
              hField.setValue(new Double(h));
              hField.setColumns(10);
              hField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      hField.setText("");
                  }});
              hField.setBounds(270, 110, 50, 20);
    
              aField = new JFormattedTextField();
              aField.setValue(new Integer(a));
              aField.setColumns(10);
              aField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      aField.setText("");
                  }});
              aField.setBounds(270, 140, 50, 20);
    
              nField = new JFormattedTextField();
              nField.setValue(new Integer(n));
              nField.setColumns(10);
              nField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      nField.setText("");
                  }});
              nField.setBounds(270, 170, 50, 20);
    
              dField = new JFormattedTextField();
              dField.setValue(new Integer(d));
              dField.setColumns(10);
              dField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      dField.setText("");
                  }});
              dField.setBounds(270, 200, 50, 20);
    
              nsField = new JFormattedTextField();
              nsField.setValue(new Integer(ns));
              nsField.setColumns(10);
              nsField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      nsField.setText("");
                  }});
              nsField.setBounds(670, 80, 50, 20);
    
              ssField = new JFormattedTextField();
              ssField.setValue(new Integer(ss));
              ssField.setColumns(10);
              ssField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      ssField.setText("");
                  }});
              ssField.setBounds(670, 110, 50, 20);
    
              rsField = new JFormattedTextField();
              rsField.setValue(new Integer(rs));
              rsField.setColumns(10);
              rsField.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      rsField.setText("");
                  }});
              rsField.setBounds(670, 140, 50, 20);
    
              Vrd1Field = new JFormattedTextField(Vrd1Format);
              Vrd1Field.setColumns(10);
              Vrd1Field.setValue(new Double(Vrd1));
              Vrd1Field.setEditable(false);
              Vrd1Field.setForeground(Color.blue);
              Vrd1Field.setFont(new Font("Zmienna" , Font.BOLD, 15));
              Vrd1Field.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      Vrd1Field.setText("");
                  }});
              Vrd1Field.setBounds(270, 240, 70, 20);
    
              Vrd2Field = new JFormattedTextField(Vrd2Format);
              Vrd2Field.setColumns(10);
              Vrd2Field.setValue(new Double(Vrd2));
              Vrd2Field.setEditable(false);
              Vrd2Field.setForeground(Color.blue);
              Vrd2Field.setFont(new Font("Zmienna" , Font.BOLD, 15));
              Vrd2Field.addFocusListener(new FocusListener() {
                  @Override public void focusLost(FocusEvent e) {}
                  @Override public void focusGained(FocusEvent e) {
                      Vrd2Field.setText("");
                  }});
              Vrd2Field.setBounds(670, 180, 70, 20);
    
    
              //Tell accessibility tools about label/textfield pairs.
              betLabel.setLabelFor(betField);
              bLabel.setLabelFor(bField);
              hLabel.setLabelFor(hField);
              aLabel.setLabelFor(aField);
              nLabel.setLabelFor(nField);
              dLabel.setLabelFor(dField);
              Vrd1Label.setLabelFor(Vrd1Field);
              metLabel.setLabelFor(metField);
              nsLabel.setLabelFor(nsField);
              ssLabel.setLabelFor(ssField);
              rsLabel.setLabelFor(rsField);
              Vrd2Label.setLabelFor(Vrd2Field);
    
          
              V1 = new JButton(" Vrd1 ");
              V1.setBounds(270, 280, 70, 20);
              V1.addActionListener(this);
              V1.setToolTipText(" Oblicz Vrd1 ");
                
              V2 = new JButton(" Vrd2 ");
              V2.setBounds(670, 220, 70, 20);
              V2.addActionListener(this);
              V2.setToolTipText(" Oblicz Vrd2 ");
              
        }
             
        private void createAndShowGUI() {
    
            JFrame frame = new JFrame("Scinanie belek wg. PN-B-03264:2002 v.1.6");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(null);
            
            //Add contents to the window.
            frame.add(new Help());
            frame.add(betLabel);
            frame.add(bLabel);
            frame.add(hLabel);
            frame.add(aLabel);
            frame.add(dLabel);
            frame.add(nLabel);
            frame.add(Vrd1Label);
            frame.add(metLabel);
            frame.add(nsLabel);
            frame.add(ssLabel);
            frame.add(rsLabel);
            frame.add(Vrd2Label);
    
    
            frame.add(betField);
            frame.add(bField);
            frame.add(hField);
            frame.add(aField);
            frame.add(dField);
            frame.add(nField);
            frame.add(Vrd1Field);
            frame.add(metField);
            frame.add(nsField);
            frame.add(ssField);
            frame.add(rsField);
            frame.add(Vrd2Field);
       
            frame.add(V1);
            frame.add(V2);
            
            //Display the window.
            
            frame.pack();
            frame.setSize(800, 380);
            frame.setVisible(true);
             
            }
      
            
      
            
    
            static double computeVrd1(double bet, double b, double h, double a, int n, int d) {
                double answer, dh, A1, fctd = 0;
    
                if (bet == 15){
                fctd = 0.73;
                } else if (bet == 20){
                fctd = 0.87;
                } else if (bet == 25){
                fctd = 1.00;
                } else if (bet == 30){
                fctd = 1.20;
                } else if (bet == 37){
                fctd = 1.33;
                } else if (bet == 45){
                fctd = 1.47;
                } else if (bet == 50){
                fctd = 1.67;
                } else if (bet == 55){
                fctd = 1.80;
                } else if (bet == 60){
                fctd = 1.93;
                }
    
                dh = h - a - d / 2;
                A1 = (1.2 + 40 * (n * d) / (b * dh));
                answer =((0.35 * 1.0 * fctd * A1) * b * dh) / 1000;
                return answer;
            }
    
            static double computeVrd2(double h, double met, double ns, double ss, double rs, double a, double d) {
                double answer1, dh1, fyd = 0;
    
                if (met == 1){
                    fyd = 190;
                } else if (met == 2){
                    fyd = 210;
                } else if (met == 3){
                    fyd = 310;
                } else if (met == 4){
                    fyd = 350;
                } else if (met == 5){
                    fyd = 420;
                }
    
                dh1 = h - a - d / 2;
                answer1 = (((ns * 3.14159265 * (ss * ss) / 4 * fyd ) / rs) * 0.9 * dh1) / 1000;
                return answer1;
            }
    
            
            public void actionPerformed(ActionEvent e)
            {
          	  if (e.getSource() == V1) {
          	  double Vrd1 = computeVrd1(bet, b, h, a, n, d);
          	    Vrd1Field.setValue(new Double(Vrd1));
          	  }
          	  else {
                JOptionPane.showMessageDialog(Help.this, "Nie liczy sie glabie", "Test", JOptionPane.ERROR_MESSAGE);
                 	}
          	  if (e.getSource() == V2) {
              double Vrd2 = computeVrd2(h, met, ns, ss, rs, a, d);
                Vrd2Field.setValue(new Double(Vrd2));
                }
          	  else {
                JOptionPane.showMessageDialog(Help.this, "Nie liczy sie glabie", "Test", JOptionPane.ERROR_MESSAGE);
          	  		}    
            }  
    
            
            public static void main(String[] args) {
            
            	final Help createAndShowGUIX = new Help();
            	
            	
                
            	
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
               
                    createAndShowGUIX.createAndShowGUI();
                }
            });
        }
    
    
          
    
    
    		@Override
    		public void propertyChange(PropertyChangeEvent evt) {
    			// TODO Auto-generated method stub
    			
    		}
    
    
    }

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help, I can't make the JButton to work properly

    Quote Originally Posted by furieux View Post
    ok, I think I managed to "to access non-static variables in a static context" like you said, ...
    I would do things differently. My createAndShowGUI would still be static, but again, I'd build my GUI inside of the class's constructor. Your class extends a JPanel and yet you add nothing to this JPanel. So instead of adding all your stuff directly to the JFrame, add the components to the class itself in its constructor. Then in the static createAndShowGUI method, create a JFrame and add an instance of the class to the JFrame. In fact, you already do this, but since the current class has nothing added to it, its wasted code. For instance I'd do something like this:

    Java Code:
       private static void createAndShowGUI() {
    
          JFrame frame = new JFrame("Scinanie belek wg. PN-B-03264:2002 v.1.6");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          // frame.setLayout(null); // you don't want the JFrame's contentPane to be null.
    
          // leave this:
          // Since you'll be adding stuff to the Help itself, this will make sense.
          frame.add(new Help()); 
    
    
    
          // but get rid of all of this:
          // and instead add these components to Help itself in its constructor.
          // frame.add(betLabel);
          // frame.add(bLabel);
          // frame.add(hLabel);
          // frame.add(aLabel);
          // frame.add(dLabel);
          // frame.add(nLabel);
          // frame.add(Vrd1Label);
          // frame.add(metLabel);
          // frame.add(nsLabel);
          // frame.add(ssLabel);
          // frame.add(rsLabel);
          // frame.add(Vrd2Label);
    
          // frame.add(betField);
          // frame.add(bField);
          // frame.add(hField);
          // frame.add(aField);
          // frame.add(dField);
          // frame.add(nField);
          // frame.add(Vrd1Field);
          // frame.add(metField);
          // frame.add(nsField);
          // frame.add(ssField);
          // frame.add(rsField);
          // frame.add(Vrd2Field);
    
          // leave this:
          frame.add(V1);
          frame.add(V2);
    
          // Display the window.
    
          frame.pack();
          frame.setSize(800, 380);
          frame.setVisible(true);
    
       }
    but I'm still stuck with the non-working properly buttons.

    I tryed all the ideas I had earlier but still

    Any more hints :) ?
    I don't see anywhere where you try to debug things as Norm suggests. Where are your System.out.println statements that tell you what is going on as the program is running?

  11. #11
    furieux is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Help, I can't make the JButton to work properly

    working on this, thanks

  12. #12
    furieux is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Help, I can't make the JButton to work properly

    Working :D,

    forgot to add "PropertyChangeEvent"
    tip with System.out.println was very helpfull.

    Thanks for your help Fubarable and Norm

Similar Threads

  1. actions dont work properly, calculator
    By lordjb in forum New To Java
    Replies: 14
    Last Post: 02-03-2011, 08:00 AM
  2. class that wont work out properly.
    By vampire-elf in forum New To Java
    Replies: 7
    Last Post: 09-07-2010, 02:39 AM
  3. Replies: 3
    Last Post: 05-06-2009, 01:11 AM
  4. [SOLVED] JButton does not display ImageIcon properly
    By Singing Boyo in forum New To Java
    Replies: 1
    Last Post: 04-17-2009, 04:47 AM
  5. Eclipse Documentation does not work properly.
    By krawetko in forum Eclipse
    Replies: 0
    Last Post: 10-05-2008, 11:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •