Results 1 to 7 of 7
  1. #1
    Billaguana is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default wont show entire JLabel

    So, I am creating an applet which will keep count of a team's score but when I run it it starts fine but has a problem no matter what I click.

    Here is my code:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;
    import java.lang.Object.*;
    
    public class Mouse1 extends Applet
    {
       int width, height;
    
       private JLabel ending;
       private JLabel t1ss = new JLabel("");
       private JLabel t2ss = new JLabel("");
       private JLabel t3ss = new JLabel("");
       private JLabel t4ss = new JLabel("");
    	private JButton pt1, pt2, pt3, pt4, mt1, mt2, mt3, mt4, teams;
    	int t1s, t2s, t3s, t4s;
    	String br = "<br>";
    
       public void init() {
       	
       		 width = getSize().width;
          height = getSize().height;
    
    		setLayout(new FlowLayout());
    		
    		pt1 = new JButton("Team 1");
    		pt1.addActionListener(new ListenerOne());
    		pt1.setBackground(Color.green);
    		add(pt1);
    		
    		pt2 = new JButton("Team 2");
    		pt2.addActionListener(new ListenerTwo());
    		pt2.setBackground(Color.green);
    		add(pt2);
    		
    		pt3 = new JButton("Team 3");
    		pt3.addActionListener(new ListenerThree());
    		pt3.setBackground(Color.green);
    		add(pt3);
    		
    		pt4 = new JButton("Team 4");
    		pt4.addActionListener(new ListenerFour());
    		pt4.setBackground(Color.green);
    		add(pt4);
    		
    		mt1 = new JButton("Team 1");
    		mt1.addActionListener(new ListenerFive());
    		mt1.setBackground(Color.red);
    		add(mt1);
    		
    		mt2 = new JButton("Team 2");
    		mt2.addActionListener(new ListenerSix());
    		mt2.setBackground(Color.red);
    		add(mt2);
    		
    		mt3 = new JButton("Team 3");
    		mt3.addActionListener(new ListenerSeven());
    		mt3.setBackground(Color.red);
    		add(mt3);
    		
    		mt4 = new JButton("Team 4");
    		mt4.addActionListener(new ListenerEight());
    		mt4.setBackground(Color.red);
    		add(mt4);
    		
    		ending = new JLabel("Score:");
    		ending.setForeground(Color.black);
    		add(ending);
       }
    
       private class ListenerOne implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			t1s = t1s + 1;
    			ending.setText("<html>Score:" + br + "Team 1: " + t1s + br + "Team 2: " + t2s + br + "Team 3: " + t3s + br + "Team 4: " + t4s + "</html>");
    		}
    	}
    	private class ListenerTwo implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			t2s = t2s + 1;
    			ending.setText("<html>Score:" + br + "Team 1: " + t1s + br + "Team 2: " + t2s + br + "Team 3: " + t3s + br + "Team 4: " + t4s + "</html>");
    		}
    	}
    	private class ListenerThree implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			t3s = t3s + 1;
    			ending.setText("<html>Score:" + br + "Team 1: " + t1s + br + "Team 2: " + t2s + br + "Team 3: " + t3s + br + "Team 4: " + t4s + "</html>");
    		}
    	}
    	private class ListenerFour implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			t4s = t4s + 1;
    			ending.setText("<html>Score:" + br + "Team 1: " + t1s + br + "Team 2: " + t2s + br + "Team 3: " + t3s + br + "Team 4: " + t4s + "</html>");
    		}
    	}
    	private class ListenerFive implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			t1s = t1s - 1;
    			ending.setText("<html>Score:" + br + "Team 1: " + t1s + br + "Team 2: " + t2s + br + "Team 3: " + t3s + br + "Team 4: " + t4s + "</html>");
    		}
    	}
    	private class ListenerSix implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			t2s = t2s - 1;
    			ending.setText("<html>Score:" + br + "Team 1: " + t1s + br + "Team 2: " + t2s + br + "Team 3: " + t3s + br + "Team 4: " + t4s + "</html>");
    		}
    	}
    	private class ListenerSeven implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			t3s = t3s - 1;
    			ending.setText("<html>Score:" + br + "Team 1: " + t1s + br + "Team 2: " + t2s + br + "Team 3: " + t3s + br + "Team 4: " + t4s + "</html>");
    		}
    	}
    	private class ListenerEight implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			t4s = t4s - 1;
    			ending.setText("<html>Score:" + br + "Team 1: " + t1s + br + "Team 2: " + t2s + br + "Team 3: " + t3s + br + "Team 4: " + t4s + "</html>");
    		}
    	}
    }
    ands it looks like the first image on start and second image no matter which button I press.
    Attached Thumbnails Attached Thumbnails wont show entire JLabel-temp.png   wont show entire JLabel-temp1.png  

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default

    After you make your JLabel take up multiple rows, its parent doesn't know that it needs to update its size.

    Calling validate() after changing the text of the JLabel seems to mostly do the trick.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Billaguana is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    okay, thanks for the reply but I put
    Java Code:
    ending.validate();
    after each of the setText and it still doesnt work. Got any other ideas?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default

    That's not what I meant. You have to call validate() on ending's parent Container.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Billaguana is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    o, okay. huh? haha sorry, I am just learning java so a little hint to where I should put it please.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default

    A Component's parent Container is the Container to which you add it.

    To which Container do you add the ending JLabel (hint: you call its add function)? That's the Container you have to call validate() on. And you have to do it whenever your html changes the size of the ending label.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Billaguana is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    ok, I know its been a while but I got the earlier code working by doing something else but I have come to creating yet another applet and am encountering the same prob. I never did figure out what the Parent container is considering I never called one I think. If you dont mind just telling me what it is. Thanks.

Similar Threads

  1. Last JLabel I add to JPanel doesnt show up
    By Ambergahill in forum New To Java
    Replies: 6
    Last Post: 01-29-2014, 08:07 PM
  2. Replies: 3
    Last Post: 05-27-2010, 10:54 AM
  3. JLabel wont appear in front of background image
    By Andolph23 in forum AWT / Swing
    Replies: 5
    Last Post: 05-27-2010, 01:42 AM
  4. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-06-2010, 11:02 PM
  5. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 06:20 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
  •