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

Thread: JFrame not repainting

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Location
    Bangladesh
    Posts
    27
    Thanks
    3
    Thanked 1 Time in 1 Post

    Question JFrame not repainting

    Hi all, i am new to this forum and also new to Java. I am trying to create a Java GUI with Absolute Layout (don't ask me why its a long story). I tried to place the components on a JPanel but they didn't show. Then just used setBounds and adding to a JFrame finally showed the components. So i removed the idea of panel. now i am stuck with clearing the window. i mean i have tried frame.validate() and frame.repaint() to frame.paint(frame.getGraphics()) etc etc.. the controls keep responding, so they are removed alright, but the problem is they are on the screen still smiling. here is the code and any solution wud be appreciated greatly. Thanx in advance.

    import javax.swing.* ;
    import javax.swing.JOptionPane ;
    import java.awt.event.* ;
    import java.awt.* ;
    import java.net.* ;
    import java.io.* ;
    import java.awt.Component ;
     
     
    /* the main client class */
    class Client implements ActionListener, MouseMotionListener, MouseListener{
     
    	/* the graphical elements all in one */
    	JFrame wClient = null ;
    	JLabel lblUser = null ;
    	JTextField txtUser = null ;
    	JLabel lblPassword = null ;
    	JPasswordField txtPassword = null ;
    	JButton cmdLogin = null ;
    	JCheckBox chkRemember = null ;
    	JOptionPane j = new JOptionPane() ;
    	JLabel lblSignUp = null ;
    	JLabel lblNoAccount = null ;	
    	public Client(){
    		wClient = new JFrame("Unicorn Mail Service") ;
    		wClient.setLayout(null) ;
    		wClient.setSize(710, 710) ;
    		wClient.addMouseMotionListener(this) ;
    		drawLogin() ;
    		wClient.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
    		wClient.setVisible(true) ;
    	}
     
    	private void drawLogin(){
    		lblUser = new JLabel("User Name :") ;
    		lblUser.setBounds(350, 10, 75, 25 ) ;
    		txtUser = new JTextField(15) ;
    		txtUser.setBounds(425, 10, 100, 25) ;
     
    		lblPassword = new JLabel("Password :") ;
    		lblPassword.setBounds(530, 10, 65, 25 ) ;
    		txtPassword = new JPasswordField(15) ;
    		txtPassword.setBounds(600, 10, 100, 25 ) ;
     
    		cmdLogin = new JButton("Login") ;
    		cmdLogin.setBounds(605, 40, 70, 25) ;
    		cmdLogin.addActionListener(this) ;
     
    		chkRemember = new JCheckBox("Remember Me ?") ;
    		chkRemember.setBounds(350, 40, 120, 25) ;
     
    		lblNoAccount = new JLabel("Don't Have an Account ??") ;
    		lblNoAccount.setBounds(350, 80, 150, 25) ;
     
    		lblSignUp = new JLabel("Sign up Now !!") ;
    		lblSignUp.setBounds(500, 80, 150, 25) ;
    		lblSignUp.addMouseMotionListener(this) ;
    		lblSignUp.addMouseListener(this) ;
     
     
     
    		wClient.add(lblUser) ;
    		wClient.add(lblPassword) ;
    		wClient.add(txtUser) ;
    		wClient.add(txtPassword) ;
    		wClient.add(cmdLogin) ;
    		wClient.add(chkRemember) ;
    		wClient.add(lblNoAccount) ;
    		wClient.add(lblSignUp) ;
     
    	}
     
    	/* the action listener methods */
    	public void actionPerformed(ActionEvent e){
    		if (e.getSource() == cmdLogin){
    			String user = txtUser.getText() ;
    			String pass = txtPassword.getText() ;
    			if (user.equals(""))
    				j.showMessageDialog(null, "User Name Missing", "Unicorn", j.ERROR_MESSAGE) ;
    			if (pass.equals(""))
    				j.showMessageDialog(null, "Password Name Missing", "Unicorn", j.ERROR_MESSAGE) ;
    			return ;		
     
    		}
     
    	}
     
    	/* The mouse motion listener methods */	
    	public void mouseMoved(MouseEvent e){
    		if (e.getSource() == lblSignUp){
    			lblSignUp.setFont(new Font("Consolas", Font.ITALIC, 14)) ;
    			lblSignUp.setForeground(new Color(0, 0, 128)) ;
    		}else{
    			lblSignUp.setFont(new Font("Arial", Font.BOLD, 12)) ;
    			lblSignUp.setForeground(new Color(0, 0, 0)) ;
    		}
    	}
    	public void mouseDragged(MouseEvent e){
     
     
    	}
     
    	/* the mouse listener methods */
    	public void mouseClicked(MouseEvent e){
    		if (e.getSource() == lblSignUp){
    			Component [] c = wClient.getComponents() ;
    			for (int i = 0 ; i < c.length ; i++)
    				wClient.remove(c[i]) ;
    			wClient.setVisible(false) ;
    			wClient.setVisible(true) ;
     
    			//wClient.paint(wClient.getGraphics()) ;
    			//wClient.paint(wClient.getGraphics()) ;
    			//wClient.validate() ;
    			//wClient.repaint() ;
    			//wClient.repaint(1000, 0, 0, 710, 710) ;
    		}
    	}
    	public void mouseExited(MouseEvent e){
     
    	}
    	public void mouseEntered(MouseEvent e){
     
    	}
    	public void mouseReleased(MouseEvent e){
     
    	}
    	public void mousePressed(MouseEvent e){
     
    	}
    }
     
    class UnicornClient{
    	public static void main(String [] args){
    		javax.swing.SwingUtilities.invokeLater(new Runnable(){
    			public void run(){
    				new Client() ;
    			}
    		}) ;
    	}
    }


  2. #2
    Junior Member
    Join Date
    Nov 2010
    Location
    Bangladesh
    Posts
    27
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: JFrame not repainting

    Sorry guys to bother u, solved it. for reference actually its a silly mistake, when i add components, its actually added to a default pane called JRootPane. so to get the actual components
    Components [] c = wClient.getRootPane().getContentPane().getComponents() ;
    for (int i = 0 ; i < c.length ; i++) wClient.remove(c[i]) ;
    wClient.invalidate() ;
    wClient.validate() ;
    wClient.repaint() ;

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,159
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: JFrame not repainting

    You should almost definitely be using a real layout manager and a CardLayout to do the switching between panels.

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Location
    Bangladesh
    Posts
    27
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: JFrame not repainting

    Quote Originally Posted by KevinWorkman View Post
    You should almost definitely be using a real layout manager and a CardLayout to do the switching between panels.
    Yeah, but in this case i have to do it without layouts anyway thanx for the suggestion, will try to learn more about layout managers.

Similar Threads

  1. Print out a JFrame
    By ellias2007 in forum AWT / Java Swing
    Replies: 8
    Last Post: June 17th, 2010, 06:15 AM
  2. JFrame Edit
    By n00b123 in forum AWT / Java Swing
    Replies: 1
    Last Post: May 19th, 2010, 08:23 PM
  3. repainting a jframe containing two jpanels
    By musasabi in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 11th, 2010, 10:31 PM
  4. JPanel in JFrame
    By maele in forum AWT / Java Swing
    Replies: 2
    Last Post: March 8th, 2010, 03:12 AM
  5. JFrame help
    By Uden in forum AWT / Java Swing
    Replies: 0
    Last Post: August 14th, 2009, 01:37 PM