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 17 of 17

Thread: Help in showing the elements

  1. #1
    Junior Member
    Join Date
    Mar 2018
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help in showing the elements

    I write this code in one class but it doesn't show any elements ( button , labels ) all elements , but when I put it in two classes one the main and another one it works fine .

    Here is the code :

    package averageprogram;
     
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
     
    public class AverageProgram extends JFrame{
    private int num = 0;
         private String userCo = "";
        private JButton[] buttons;
        private JButton btn = new JButton("Click");
         private JButton btn1 = new JButton("Calculate Average");
        private  Container cp = getContentPane();
         private  JTextField userInput = new JTextField(30);
         private  JLabel label = new JLabel("Please enter the number of Courses: ");
         private  JTextField[] userInput1;
         private  JTextField[] userInput2;
     
         private  JLabel[] label1;
         private  JLabel[] label2;
         private  JLabel label3 = new JLabel("");
          private JPanel panel = new JPanel();
          private JPanel panel1 = new JPanel();
     
            private  int s1= 70;
            private int s2= 88;
            private int s3= 77;
            private double total = s1+ s2+ s3;
     
     
        public static void main(String[] args) {
     
     
          JFrame frame = new JFrame("frame");
         frame.setSize(600, 400);       
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("program");
        }
     
          public AverageProgram()
        {
     
             cp.add(panel);
         cp.add(panel1,BorderLayout.SOUTH);
     
             panel.add(label);
           panel.setLayout(new FlowLayout());
             panel.add(userInput);
            panel.add(btn);
          panel1.add(btn1);
     
     
     
     
     
     
            ButtonWatcher handler = new ButtonWatcher();
            btn.addActionListener(handler);
     
            ButtonWatcher1 handler1 = new ButtonWatcher1();
            btn1.addActionListener(handler1);
     
     
        }
         private class ButtonWatcher implements ActionListener {
     
            public void actionPerformed(ActionEvent a)
            {
     
                   try {
                 userCo = userInput.getText();
     
                    num = Integer.parseInt(userCo);
              label1 = new JLabel[num];
                   label2 = new JLabel[num];
                   userInput1 = new JTextField[num];
                   userInput2 = new JTextField[num];
                   }
                   catch (Exception exception) {
                                        JOptionPane.showMessageDialog(null, "Error Input");
                                    }
                for(int i = 0; i<num; i++)
            {
                  label1[i] = new JLabel("Course Name " + i);
     
                 userInput1[i] = new JTextField(10);
                   userInput1[i].setHorizontalAlignment(JTextField.CENTER);
                    label2[i] = new JLabel("Course Grade " + i);
                  userInput2[i] = new JTextField(10);
                    userInput2[i].setHorizontalAlignment(JTextField.CENTER);
            }
     
                 for(int i=0; i < num; ++i)
            {
               panel.add(label1[i]);
                   panel.add(userInput1[i]);
     
                panel.add(label2[i]);
                     panel.add(userInput2[i]);
     
            }
     
               SwingUtilities.updateComponentTreeUI(cp);  
            }
     
        }
     
          private class ButtonWatcher1 implements ActionListener {
     
            public void actionPerformed(ActionEvent a)
            {
           try
           {
                     if (userInput.getText().isEmpty()) {
                            String message = "You must enter a number of your courses first";
                            JOptionPane.showMessageDialog(new JFrame(), message, "Dialog",
                                    JOptionPane.ERROR_MESSAGE);
                     }
     
                     else
                     {
                int numOfCourses = 3;
                double avg = 0.0;
                double temp = 0.0;
     
                  for(int i = 0; i<num; i++)
            {
     
                numOfCourses++;
                temp = Double.parseDouble(userInput2[i].getText());
                total  = total + temp;
            }
     
                     panel.add(label3);
                     avg = total / numOfCourses;
                      avg =Math.round(avg * 10.0) / 10.0;
                     label3.setText("The result is : " + avg + " %" );
     
     
            }
            }
            catch (Exception exception) {
                                        JOptionPane.showMessageDialog(null, "Error Input");
                                    }
            }
     
     
    }
     
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,786
    Thanks
    53
    Thanked 2,428 Times in 2,379 Posts

    Default Re: Help in showing the elements

    The code's formatting is poor making it hard to read and understand. The }s should be in the same column as the start of the statement with the pairing {

    Please post properly formatted code.

    Where does the code create an instance of AverageProgram? Is that where the missing components are?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2018
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help in showing the elements

    It really take so much time for me to wrote it . sorry for bad formatting .
    If you copy the code and paste it , and run it , it will show only blank form without any elements . I don't know what I missing ...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,786
    Thanks
    53
    Thanked 2,428 Times in 2,379 Posts

    Default Re: Help in showing the elements

    I don't know what I missing ...
    Did you miss this:
    Where does the code create an instance of AverageProgram? Is that where the missing components are?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2018
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help in showing the elements

    I use only one class , why I should create an instance ...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,786
    Thanks
    53
    Thanked 2,428 Times in 2,379 Posts

    Default Re: Help in showing the elements

    why I should create an instance ...
    Are all the components you are asking about defined and used in the AverageProgram class? If there isn't an instance of that class, those components won't be shown.
    When the main method is executed, it needs to create an instance of AverageProgram to build the GUI you are interested in.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2018
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help in showing the elements

    Can you help me more , I don't get it really .
    I write this in the main method ?


     AverageProgram test = new AverageProgram();

    and what's next ?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,786
    Thanks
    53
    Thanked 2,428 Times in 2,379 Posts

    Default Re: Help in showing the elements

    and what's next ?
    what happened with that code when you executed it?

    Look at the steps the main method did to display the (empty) JFrame. Add those steps following the creation of the AverageProgram instance.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2018
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help in showing the elements

    Nothing happened, If I add these code in the main method it show me there is error :

      cp.add(panel);
         cp.add(panel1,BorderLayout.SOUTH);
     
             panel.add(label);
           panel.setLayout(new FlowLayout());
             panel.add(userInput);
            panel.add(btn);
          panel1.add(btn1);
     
     
            ButtonWatcher handler = new ButtonWatcher();
            btn.addActionListener(handler);
     
            ButtonWatcher1 handler1 = new ButtonWatcher1();
            btn1.addActionListener(handler1);


    --- Update ---

    Can anyone please help me ...

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,786
    Thanks
    53
    Thanked 2,428 Times in 2,379 Posts

    Default Re: Help in showing the elements

    If I add these code in the main method
    That is not what I suggested. The main method has these statements:
    public static void main(String[] args) {
          JFrame frame = new JFrame("frame");    // Replace this one
         frame.setSize(600, 400);                        //  These are what is needed
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("program");
        }
    The first statement should be replaced with one that creates a new AverageProgram instance:
    AverageProgram frame = new AverageProgram();

    --- Update ---

    it show me there is error :
    When you get errors you need help with you need to copy and paste them here.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2018
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help in showing the elements

    Ok now it's show all the elements thank you , but there is one more problem the style if the program is changed look before :
    https://i.imgur.com/01w485N.png

    and after :
    https://i.imgur.com/2XZ6SGm.png

    why this happened?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,786
    Thanks
    53
    Thanked 2,428 Times in 2,379 Posts

    Default Re: Help in showing the elements

    What is the event that the changed GUI is after?
    Can you explain what was done to the GUI shown in the "before" image that led to the GUI to be what was shown in the "after" image.
    Does this happen every time that user interaction with the program takes place?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2018
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help in showing the elements

    You must understand first I was used two classes one for the main and one contain all the JFrame components , after I use one class (the main inside it )
    this problem happened ...

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,786
    Thanks
    53
    Thanked 2,428 Times in 2,379 Posts

    Default Re: Help in showing the elements

    Ok, you are talking about 2 different programs. I thought you had one program that changed its layout when it was running.

    Programs can use different layout managers in different ways to control how the components are positioned in the GUI.
    I imagine the two programs are using layout managers in different ways.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Mar 2018
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help in showing the elements

    No now I used one class only , and this is the whole code : (can you please tell me where to change to make the layout looks like in the above picture ? ...

    package averageprogram;
     
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
     
    public class AverageProgram extends JFrame{
     
    private int num = 0;
         private String userCo = "";
        private JButton[] buttons;
        private JButton btn = new JButton("Click");
         private JButton btn1 = new JButton("Calculate Average");
        private  Container cp = getContentPane();
         private  JTextField userInput = new JTextField(30);
         private  JLabel label = new JLabel("Please enter the number of Courses that you have completed in Fall17/18 : ");
         private  JTextField[] userInput1;
         private  JTextField[] userInput2;
     
         private  JLabel[] label1;
         private  JLabel[] label2;
         private  JLabel label3 = new JLabel("");
          private JPanel panel = new JPanel();
          private JPanel panel1 = new JPanel();
     
            private  int s1 = 70;
            private int s2 = 88;
            private int s3 = 77;
            private double total = s1+ s2+ s3;
     
     
        public static void main(String[] args) {
     
           AverageProgram test = new AverageProgram();
         test.setSize(600, 400);       
            test.setVisible(true);
            test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            test.setTitle("Name");
     
     
        }
     
          public AverageProgram()
        {
          cp.add(panel);
         cp.add(panel1,BorderLayout.SOUTH);
     
           panel.setLayout(new FlowLayout());
             panel.add(label);
     
             panel.add(userInput);
            panel.add(btn);
          panel1.add(btn1);
     
     
            ButtonWatcher handler = new ButtonWatcher();
            btn.addActionListener(handler);
     
            ButtonWatcher1 handler1 = new ButtonWatcher1();
            btn1.addActionListener(handler1);
     
     
     
        }
         private class ButtonWatcher implements ActionListener {
     
            public void actionPerformed(ActionEvent a)
            {
     
                   try {
                 userCo = userInput.getText();
     
                    num = Integer.parseInt(userCo);
              label1 = new JLabel[num];
                   label2 = new JLabel[num];
                   userInput1 = new JTextField[num];
                   userInput2 = new JTextField[num];
                   }
                   catch (Exception exception) {
                                        JOptionPane.showMessageDialog(null, "Error Input");
                                    }
                for(int i = 0; i<num; i++)
            {
                  label1[i] = new JLabel("Course Name " + i);
     
                 userInput1[i] = new JTextField(10);
                   userInput1[i].setHorizontalAlignment(JTextField.CENTER);
                    label2[i] = new JLabel("Course Grade " + i);
                  userInput2[i] = new JTextField(10);
                    userInput2[i].setHorizontalAlignment(JTextField.CENTER);
            }
     
                 for(int i=0; i < num; ++i)
            {
     
               panel.add(label1[i]);
                   panel.add(userInput1[i]);
     
                panel.add(label2[i]);
                     panel.add(userInput2[i]);
     
            }
     
               SwingUtilities.updateComponentTreeUI(cp);  
            }
     
        }
     
          private class ButtonWatcher1 implements ActionListener {
     
            public void actionPerformed(ActionEvent a)
            {
           try
           {
                     if (userInput.getText().isEmpty()) {
                            String message = "You must enter a number of your courses first";
                            JOptionPane.showMessageDialog(new JFrame(), message, "Dialog",
                                    JOptionPane.ERROR_MESSAGE);
                     }
     
                     else
                     {
                int numOfCourses = 3;
                double avg = 0.0;
                double temp = 0.0;
     
                  for(int i = 0; i<num; i++)
            {
     
                numOfCourses++;
                temp = Double.parseDouble(userInput2[i].getText());
                total  = total + temp;
            }
     
                     panel.add(label3);
                     avg = total / numOfCourses;
                      avg =Math.round(avg * 10.0) / 10.0;
                     label3.setText("The result is : " + avg + " %" );
     
     
            }
     
            }
            catch (Exception exception) {
                                        JOptionPane.showMessageDialog(null, "Error Input");
                                    }
            }  
    }   
    }

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,786
    Thanks
    53
    Thanked 2,428 Times in 2,379 Posts

    Default Re: Help in showing the elements

    make the layout looks like in the above picture ?
    Can you post the code for the program that was used for the picture?

    Look at the tutorial about layout managers: https://docs.oracle.com/javase/tutor...out/index.html
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    ahmed2009 (April 10th, 2018)

  18. #17
    Junior Member
    Join Date
    Mar 2018
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help in showing the elements

    Ok thank you a lot man , I will try to solve it . Thanks so much for helping .

Similar Threads

  1. jtable showing the table content but not showing the table headers
    By vimivimal in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 10th, 2017, 01:27 PM
  2. Replies: 9
    Last Post: October 14th, 2013, 06:17 AM
  3. GUI not showing its J-elements???
    By dynamix24 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 3rd, 2013, 01:07 AM
  4. Replies: 3
    Last Post: March 6th, 2012, 03:50 AM
  5. [SOLVED] ArrayList object's elements confusing??? doesnt replace the elements? set() method?
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 21st, 2011, 01:20 PM

Tags for this Thread