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

Thread: Jlabel won't clear

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Jlabel won't clear

    Hello, I am trying to clear a jlabel so I can put the current page number up. Instead it writes each number right after another and the jlabel won't clear. I am using setText("") Here is the relevant code, if you need more code I can put up the entire program.



    private class ButtonHandler implements ActionListener
    		{
     
     
     
    	    public void actionPerformed(ActionEvent e)
     
    		{
    formatter8.format("%d",Page);
     
    		    JButton button = (JButton) e.getSource();
     
    		    if(button.equals(previousJButton)) //firstButton is the first class JButton that was defined
     
    		    {
                    if(Page==1){
    		        //JOptionPane.showMessageDialog(DataEntry.this,String.format("You pressed: %s",e.getActionCommand()));
                    label1.setText(formatter.toString());
    				label2.setText(formatter2.toString());
    				label3.setText(formatter3.toString());
    				label10.setText(formatter8.toString());
     
    			    }
    			    Page--;
    			    if(Page<0)
    			    {
    					Page=0;
    					label10.setText(formatter8.toString());
     
     
    				}
    			    if(Page==0){
    			    label1.setText(formatter.toString());
    			    label2.setText(formatter2.toString());
     
    				label3.setText(formatter3.toString());
                    formatter8.format("%d",Page);
     
     
    				label10.setText(formatter8.toString());
     
     
    			    }
    			    if(Page==1){
    				label1.setText(formatter5.toString());
    			    label2.setText(formatter6.toString());
    				label3.setText(formatter7.toString());
    				label10.setText("");
    				label10.setText(formatter8.toString());
     
    			    }
     
     
    		    }
     
    		     if(button.equals(nextJButton)) //secondButton is the second class JButton defined
     
    		    {
                  Page++;
                    if (Page==1){
    		        //Panel5.removeAllChildComponents().
    		        label1.setText(formatter5.toString());
    				label2.setText(formatter6.toString());
    				label3.setText(formatter7.toString());
                    label10.setText("");
                    label10.setText(formatter8.toString());
     
    			    }
    			    else{
     
    					label10.setText("");
    					label10.setText(formatter8.toString());
    				}
     
    			}
     
     
    		    }
     
    		}
    Last edited by jps; August 21st, 2013 at 01:27 AM. Reason: code tags


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,135
    My Mood
    Mellow
    Thanks
    187
    Thanked 659 Times in 646 Posts

    Default Re: Jlabel won't clear

    Please post your code in code tags.

    It's difficult, if not impossible, to determine a reason for the behavior you've described with the code you've posted. Please post a runnable example that demonstrates the problem. The exercise of creating an example may even lead you to a better understanding of the problem and lead you to the solution.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Jlabel won't clear

    This is the shortened program, below it is the test program which I keep in another java file. I will comment at the problem section.


    import java.util.Vector;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import java.awt.GridLayout;
    import javax.swing.JTextField;
    import javax.swing.JOptionPane;
    import java.util.Formatter;
    import java.util.Random;
     
     
    public class RunnableDataEntry extends JFrame implements FocusListener,KeyListener,ActionListener
    {
    	private JButton nextJButton;
     
    	private JLabel label1;
    	private JLabel label2;
    	private JLabel label3;
    	private JLabel label10;
     
    	private JPanel Panel1;
    	private JPanel Panel2;
    	private JPanel Panel3;
    	private JPanel Panel4;
    	private JPanel Panel5;
     
    	private BorderLayout layout;
    	private GridLayout gridLayout1;
    	private FlowLayout flowLayout1;
     
        private int Page=0;
     
        private int array[]=new int[24];
     
        Formatter formatter=new Formatter();
        Formatter formatter2=new Formatter();
        Formatter formatter3=new Formatter();
        Formatter formatter4=new Formatter();
        Formatter formatter5=new Formatter();
        Formatter formatter6=new Formatter();
        Formatter formatter7=new Formatter();
        Formatter formatter8=new Formatter();
     
        public RunnableDataEntry()
    	{
    		super("Runnable Data Entry");
     
    		layout = new BorderLayout(5,10);
            nextJButton=new JButton("Next Page");
    		flowLayout1= new FlowLayout();
    		setLayout(layout);
    		Panel1 = new JPanel();
    		Panel2=new JPanel();
    		Panel3=new JPanel();
    		Random randomNumber=new Random();
     
    		for(int counter=0;counter<array.length;counter++)
    		{
     
                array[counter]=randomNumber.nextInt(10);
     
    		}
     
            formatter.format("%d%d%d%d",array[0],array[1],array[2],array[3]);
    		formatter2.format("%d%d%d%d",array[4],array[5],array[6],array[7]);
    		formatter3.format("%d%d%d%d",array[8],array[9],array[10],array[11]);
    		formatter5.format("%d%d%d%d",array[12],array[13],array[14],array[15]);
    		formatter6.format("%d%d%d%d",array[16],array[17],array[18],array[19]);
    		formatter7.format("%d%d%d%d",array[20],array[21],array[22],array[23]);
    		formatter8.format("%d",Page);
     
     
    		label1=new JLabel(formatter.toString());
    		label2=new JLabel(formatter2.toString());
    		label3=new JLabel(formatter3.toString());
    		label10=new JLabel(formatter8.toString());
     
    		add(Panel3,BorderLayout.WEST);
    			Panel3.add(label10);
     
    		add(Panel1,BorderLayout.NORTH);
    			Panel1.setLayout(flowLayout1);
    			Panel1.add(label1);
    			Panel1.add(label2);
    			Panel1.add(label3);
     
    		add(Panel2,BorderLayout.SOUTH);
    			Panel2.add(nextJButton);
     
    		ButtonHandler handler=new ButtonHandler();
    		nextJButton.addActionListener(handler);
     
    	}
     
    		private class ButtonHandler implements ActionListener
    		{
            	public void actionPerformed(ActionEvent e)
            	{
    			//page always stays at 0, will not move to one and thus display in the label as 1.  Page is given the value 0 above, yet I increment it so that it will go to one when the button is pressed//	
                            Page++;
                    	if (Page==1)
                    	{
    		        		label1.setText(formatter5.toString());
    						label2.setText(formatter6.toString());
    						label3.setText(formatter7.toString());
                    		label10.setText("");
                    		label10.setText(formatter8.toString());
     
    			   		}
    			    	else
    			    	{
    						label10.setText("");
    						label10.setText(formatter8.toString());
    					}
     
    			}
            }
     
        public void focusGained(FocusEvent e)
        {
     
        }
     
     
        public void focusLost(FocusEvent e)
        {
     
        }
     
     
        public void keyTyped(KeyEvent e)
        {
     
        }
     
        public void keyPressed(KeyEvent e)
        {
     
        }
     
     
        public void keyReleased(KeyEvent e) {
     
        }
     
     
        public void actionPerformed(ActionEvent event)
        {
     
        }
     
    }
     
     
    import javax.swing.JFrame;
     
     
    public class RunnableDataEntryTest
    {
    	public static void main(String args[])
    	{
    		RunnableDataEntry runnabledataEntry = new RunnableDataEntry();
    		runnabledataEntry.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		runnabledataEntry.setSize(500,500);
    		runnabledataEntry.setVisible( true );
        }
    }
    Attached Files Attached Files
    Last edited by jps; August 21st, 2013 at 01:27 AM. Reason: code tags

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Jlabel won't clear

    Welcome to the forum.
    Please use code tags when posting code. Help can be found on the Announcements page
    public void actionPerformed(ActionEvent e)
    {
    //page always stays at 0
    Page++;
    //print the value of Page
    It is just not possible for Page to remain zero if the actionPerformed method is being called. Use a println to see the value of Page after the increment. Odds are it is greater than 1, not zero. Unless the method is not being called or the value of Page is being changed somewhere not shown in the code.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,135
    My Mood
    Mellow
    Thanks
    187
    Thanked 659 Times in 646 Posts

    Default Re: Jlabel won't clear

    There's nothing wrong with your logic (other than being redundant) but something wrong with your use of the Formatter object(s?). I'm not familiar with Formatter and don't wish to be, but I suspect the instance is fixed to Page = 0. I leave that investigation and solution discovery to you, but the code below will show you 1) that your logic is working, and 2) another approach to accomplish the same thing: (I changed the label text, because simply showing a number was boring.)
    	private class ButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			System.out.println( "in action listener, page = " + Page );
     
    			//page always stays at 0, will not move to one and thus display
    			// in the label as 1.  Page is given the value 0 above, 
    			// yet I increment it so that it will go to one when the button
    			// is pressed	
    			Page++;
     
    			if (Page==1)
    			{
    				label1.setText(formatter5.toString());
    				label2.setText(formatter6.toString());
    				label3.setText(formatter7.toString());
    				String labelText = ( "Page number: " + Page );
    				label10.setText( labelText );
     
    			}
    			else
    			{
    				label10.setText("");
    				String labelText = ( "Page number: " + Page );
    				label10.setText( labelText );
    			}
     
    		}
    	}

  6. #6
    Junior Member
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Jlabel won't clear

    That solved the problem. I don't really know much about formatter myself, but I think your right about it being the problem. Thanks for the help

Similar Threads

  1. JColorChooser won't change text color of JLabel
    By mwardjava92 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 4th, 2013, 06:37 PM
  2. Loop through JLabel and change JLabel
    By JoeBrown in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2012, 12:52 PM
  3. Adding JLabel on other Jlabel
    By mike416 in forum AWT / Java Swing
    Replies: 3
    Last Post: March 29th, 2012, 11:50 AM
  4. Applet clear
    By chonch in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 24th, 2011, 12:40 PM
  5. clear screen.
    By chronoz13 in forum Java Theory & Questions
    Replies: 10
    Last Post: December 5th, 2009, 06:27 AM