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

Thread: JFrames not coming up?

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default JFrames not coming up?

    Ok, so I've had my three classes, and nothing seems to be running for me. It compiles well, just my swing components don't appear.

    Here's the code:
    GUI Class
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Random;
     
    public class StudentGUI extends JFrame
    	implements ActionListener
    	{
    		static JButton[] action;
    		static JFrame[] frame;
    		static JPanel[] panel;
    		static JTextArea[] area;
     
    		public StudentGUI()
    		{
    			super("StudentGrades.java");
    			action = new JButton[10];
    			action[0] = new JButton("Construct");
    			action[0].addActionListener(this);
    			action[0].setMnemonic(KeyEvent.VK_C);
     
    			for(int i = 1; i < action.length; i++)
    			{
    				action[i] = new JButton("Action " + i);
    				action[i].addActionListener(this);
    			}
    			area = new JTextArea[8];
    			for(int i = 0; i < area.length; i++)
    			{
    				area[i] = new JTextArea();
    				area[i].setLineWrap(true);
    				area[i].setEditable(false);
    			}
    			Container c = getContentPane();
    			JPanel buttonPanel = new JPanel(new FlowLayout());
    			for(int i = 0; i < action.length; i++)
    			{
    				buttonPanel.add(action[i]);
    			}
    			c.setBackground(Color.white);
    			c.add(buttonPanel, BorderLayout.CENTER);
    			panel = new JPanel[9];
    			for(int i = 0; i < panel.length;)
    			{
    				panel[i] = new JPanel(new FlowLayout());
    			}
     
     
    			frame  = new JFrame[9];
    			for(int i = 0; i < frame.length; i++)
    			{
    				frame[i] = new JFrame("Action Window");
    				frame[i].getContentPane();
    				frame[i].setBounds(300,300,100,100);
    				frame[i].setResizable(true);
    				frame[i].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				frame[1].setVisible(true);
    			}
    			for(int i = 0; i < panel.length; i++)
    			{
    				panel[i].add(area[i]);
    				frame[i].add(panel[i], BorderLayout.CENTER);
    			}
     
    		}
    		public void actionPerformed(ActionEvent e)
    		{
    			JButton button = (JButton)e.getSource();
    			Random r = new Random();
    			StudentGrades sg = null;
    			int[] scores = null;
    			int[] sorted = null;
     
    			if(button == action[0])
    			{
    				sg = new StudentGrades(r.nextInt(21)+1);
    			}
    			if(button == action[1])
    			{
    				scores = sg.getTestGrades();
    				frame[0].setVisible(true);
    				for(int i = 0; i < scores.length; i++)
    				{
    					area[0].append(scores[i] + " ");
    				}
     
    			}
    			if(button == action[2])
    			{
    				frame[0].setVisible(false);
    				sorted = sg.selectionSortB(scores);
    				frame[1].setVisible(true);
    					for(int i = 0; i < sorted.length; i++)
    				{
    					area[1].append(sorted[i] + " ");
    				}
     
    			}
    			/*if(button == action[3])
    			{	frane[1].setVisible(false);
    				frame[2].setVisible(true);
    				area[2].append("The array is " + sg.isEqual(sorted));
    			}*/
    			if(button == action[4])
    			{
    				frame[2].setVisible(false);
    				frame[3].setVisible(true);
    				int highest = sg.highestGrade(sorted);
    				area[3].append("The Highest grade is " + highest);
    			}
    			if(button == action[5])
    			{
    				frame[3].setVisible(false);
    				frame[4].setVisible(true);
    				int lowest = sg.lowestGrade(sorted);
    				area[4].append("The Lowest grade is " + lowest);
    			}
    			if(button == action[6])
    			{
    				frame[4].setVisible(false);
    				frame[5].setVisible(true);
    				double average = sg.averageGrade(sorted);
    				area[5].append("The average grade is " + average);
    			}
    			if(button == action[7])
    			{
    				frame[5].setVisible(false);
    				frame[6].setVisible(true);
    				int median = sg.medianGrade(sorted);
    				area[6].append("The median grade is " + median);
    			}
    			if(button == action[8])
    			{
    				frame[6].setVisible(false);
    				frame[7].setVisible(true);
    				int mode = sg.modeGrade(sorted);
    				area[7].append("The mode is " + mode);
    			}
    		}
    	}

    Method class
    import java.util.Random;
    public class StudentGrades
    {
    	private int[] studentGrades;
    	public StudentGrades(int students)
    	{
    		Random r = new Random();
    		studentGrades = new int[students];
    		for(int i = 0; i < studentGrades.length; i++)
    		{
    			studentGrades[i] = r.nextInt(101);
    		}
     
    	}
    	public int[] getTestGrades()
    	{
    		int[] testGrades = new int[studentGrades.length];
    		for(int i = 0; i < studentGrades.length; i++)
    		{
    			testGrades[i]= studentGrades[i];
    		}
    		return testGrades;
    	}
    	public int[] selectionSortB(int[] nums)
       {
       		int size = nums.length;
          //  Same as version A, but sorts in ascending order
     
          int first, current, least, temp;
     
          for(first = 0; first < size; first = first + 1)
          {
             least = first;
             for(current = first+1; current < size; current = current + 1)
             {  if (nums[current] < nums[least])
                {  least = current; }
             }
             temp = nums[least];
             nums[least] = nums[first];
             nums[first] = temp;
          }
          return nums;
       }
       /*public boolean isEqual(int[] arr)
       {
       }*/
       public int highestGrade(int[] arr)
       {
       		int max = 0;
       		for(int i = 0; i < arr.length; i++)
       		{
       			if(arr[i] > max)
       			{
       				max = arr[i];
       			}
     
       		}
       		return max;
       }
       public int lowestGrade(int[] arr)
       {
       		int min = arr[0];
       		for(int i = 1; i < arr.length; i++)
       		{
       			if(arr[i] < min)
       				min = arr[i];
     
       		}
       		return min;
       }
       public double averageGrade(int[] arr)
       {
       		double total = 0.0;
       		for(int i = 0; i < arr.length; i++)
       		{
       			total += (double)arr[i];
       		}
       		double avg = total / arr.length;
       		return avg;
       }
       public int medianGrade(int[] arr)
       {
       		int median = (arr[0] + arr[arr.length - 1]) / 2 ;
       		return median;
       }
       public int modeGrade(int[] arr)
       {
       		int[] count = new int[arr.length];
       		for(int i = 0; i < arr.length; i++)
       		{
       			count[i]++;
       		}
       		int max = 0;
       		for(int i = 0; i < count.length; i++)
       		{
       			if(count[i] > max)
       			{
       				max  = count[i];
       			}
       		}
       		return count[max];
     
       }
     
    }

    Client class
    public class Student
    {
    	public static void main(String[] args)
    	{
     
    		StudentGUI sgu = new StudentGUI();
    		sgu.setBounds(400,400,100,100);
    		sgu.setVisible(true);
    		sgu.setDefaultCloseOperation(sgu.EXIT_ON_CLOSE);
    		sgu.setResizable(true);
     
     
    	}
    }

    Thanks for the help.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: JFrames not coming up?

    I believe you have to set the components to be visible too with setVisible();

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JFrames not coming up?

    I added that for all my JFrames, included the inherited one.

    It seems it just sits there when I run, nothing comes up.
    Last edited by scooty199; October 28th, 2010 at 03:49 PM.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: JFrames not coming up?

    Did you set the textAreas and JButtons to be visible too?

    Also, I think I found a typo:

    frame[1].setVisible(true);


    try frame[i].setVisible(true);.

    Also, I believe you could get away with not having button in your actionListener() and instead having

    if (e.getActionCommand().equals(action[0].getText()))

    I know that

    if (e.getActionCommand().equals("Run")))
    will go into affect if a button that has the text "Run" is clicked.
    Last edited by javapenguin; October 28th, 2010 at 03:59 PM.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JFrames not coming up?

    Are there exceptions thrown? If so please post the entire stack trace...for instance, calling
    frame[1].setVisible(true);
    in one of the initialization loops should result in a NullPointerException

  6. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JFrames not coming up?

    I noticed that, and it was fixed. oddly it didn't throw a nullpointer exception.

    And I'll try that e.getActionCommand()

  7. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JFrames not coming up?

    Alright I got it displaying, now I'm getting a NullPointException at

     scores = sg.getTestGrades();

  8. #8
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JFrames not coming up?

    Any help at all?

  9. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: JFrames not coming up?

    Initialize sp.

    You must never have initialized it.

  10. #10
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: JFrames not coming up?

    Simple. A NullPointerException indicates that you are attempting to dereference a variable that hasn't been initialized, or has had its value set to null. If the line you posted is giving rise to the exception, sg is null.

    Trace the code execution and find out why a value hasn't been assigned to sg.

    db

    edit :humph: No response for nearly 2 hours and the penguin gets in a minute before me

  11. #11
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JFrames not coming up?

    I know I initialized sg. I always hit the construct button before I hit any of the action buttons.

    When I hit my Action 1 button, it gives me a NPE for scores.
    I know scores is set to null, but why won't it take the returned array?

  12. #12
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: JFrames not coming up?

    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  13. #13
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: JFrames not coming up?

    Um, ok, I have a few questions and comments for you and for some of the people helping:

    First off, the reason the JFrame would not be showing is that you haven't used the JFrame.setVisible(true) method. That was said by javapenguin I believe. What isn't necessary is to set each component visible. By default, they are visible. The JFrame however, is not visible by default (dunno why).

    You also have a few unnecessary method calls. Specifically, this in your GUI class:
    frame[i].getContentPane();
    frame[i].setBounds(300,300,100,100);
    frame[i].setResizable(true);
    ...
    frame[1].setVisible(true);

    I'm unsure why you included these calls. The JFrame.getContentPane() method returns a Container, but you are not capturing that Container, so it is just getting garbage collected. The JFrame.setBounds() method sets the boundary or something of the JFrame. Why not just use the JFrame.setSize(int width,int height) method? I suspect the x and y coordinate of 300 and 300 you are sending in the JFrame.setBounds() method might give some unexpected results. It is also unnecessary to call the JFrame.setResizable(true) method, as JFrame are already resizable by default. Lastly, are you aware that you are only setting the second JFrame in your array visible with the call frame[1].setVisible(true);?

    Now we get into an area that I'm not positive about, but shooting guesses is always fun.
    This chunk of code:
    Container c = getContentPane();
    			JPanel buttonPanel = new JPanel(new FlowLayout());
    			for(int i = 0; i < action.length; i++)
    			{
    				buttonPanel.add(action[i]);
    			}
    			c.setBackground(Color.white);
    			c.add(buttonPanel, BorderLayout.CENTER);
    The API reads as follows for the Container.add() method you are using:
    Note: If a component has been added to a container that has been displayed, validate must be called on that container to display the new component. If multiple components are being added, you can improve efficiency by calling validate only once, after all the components have been added.
    This probably means that you need to call the Container.validate() method after adding all of your components. However, this might not be the problem, just guessing.

    Work on those issues, I think there were some more I noticed, but I've forgotten them...
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  14. #14
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JFrames not coming up?

    I've gotten the JFrames fixed. I entirely eliminated the use of the JFrame array.

Similar Threads

  1. Multiple JFrames
    By Scottj996 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 15th, 2010, 05:24 AM