Results 1 to 13 of 13
  1. #1
    hendrix79 is offline Member
    Join Date
    Dec 2008
    Posts
    28
    Rep Power
    0

    Exclamation How to clear JDesktopPane

    Dear friends,

    i have a big trouble. I need to clear everything on jDesktopPane.
    This is my code

    Java Code:
    package utilities;
    
    import java.awt.BorderLayout;
    import javax.swing.ImageIcon;
    import javax.swing.JDesktopPane;
    import javax.swing.JLabel;
    import java.awt.Rectangle;
    
    public class Sfondo extends javax.swing.JFrame {
    
    	public JDesktopPane sfondo = new JDesktopPane();
    	public JLabel immagineSfondo = new JLabel();
    	public JLabel immagineAgenda = new JLabel();
    	
    	public Sfondo() {
    			super();
    			try {
    				javax.swing.UIManager.setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
    				getContentPane().add(sfondo, BorderLayout.CENTER);
    				sfondo.add(immagineSfondo);
    				sfondo.add(immagineAgenda);
    				immagineSfondo.setIcon(new ImageIcon(getClass().getClassLoader().getResource("immagini/sig.gif")));
    				immagineSfondo.setBounds(new Rectangle(510, 330, 400, 220));
    				immagineAgenda.setIcon(new ImageIcon(getClass().getClassLoader().getResource("immagini/agenda.png")));
    				immagineAgenda.setBounds(new Rectangle(0, 310, 315, 225));
    				sfondo.setForeground(new java.awt.Color(0,128,255));
    				sfondo.setBackground(new java.awt.Color(113,208,255));
    				sfondo.setSize(760, 560);
    			} catch(Exception e) {
    				e.printStackTrace();
    			}
    					
    	}
    	
    
    }
    this is my GestInterfaccia.java class: this is a pattern, a sort of frontController that will handle the communication and the path between classes.

    Java Code:
    package patterns;
    
    import java.awt.BorderLayout;
    import javax.swing.SwingUtilities;
    
    
    import avvio.Login;
    import avvio.Registrazione;
    
    public class GestInterfaccia {
    
    	public static void Registrazione(){
    		
    		new Registrazione();
    		
    	}
    	
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				Login inst = new Login();
    				inst.setLocationRelativeTo(null);
    				inst.setVisible(true);
    			}
    		});
    	}
    
    }
    this is Login.java class: this is the class that is called by the main method.

    Java Code:
    package avvio;
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JDesktopPane;
    import javax.swing.JLabel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import utilita.Sfondo;
    import javax.swing.WindowConstants;
    
    public class Login extends utilita.Sfondo {
    
    	private static final long serialVersionUID = 6897844918257163716L;
    	private JButton tasto_conferma;
    	private JButton registra;
    	public static JPasswordField campo_password;
    	private JButton reimposta;
    	public static JTextField campo_nome_utente;
    	private JLabel password;
    	private JLabel nome_utente;
    	
    	public Login() {
    		super();
    		initGUI();
    	
    	}
    	
    	private void initGUI() {
    		try {
    				setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    			
    			{
    		
    				{
    					nome_utente = new JLabel();
    					sfondo.add(nome_utente);
    					nome_utente.setText("Nome utente");
    					...
    				}
    				{
    					password = new JLabel();
    					sfondo.add(password);
    					password.setText("Password");
    					...
    				}
    				{
    					campo_nome_utente = new JTextField();
    					sfondo.add(campo_nome_utente);
    					...
    				}
    				{
    					tasto_conferma = new JButton();
    					sfondo.add(tasto_conferma);
    					tasto_conferma.setText("Conferma");
    					tasto_conferma.addActionListener(new ActionListener() {
    						public void actionPerformed(ActionEvent evt) {
    							tasto_confermaActionPerformed(evt);
    						}
    					});
    				}
    				{
    					campo_password = new JPasswordField();
    					sfondo.add(campo_password);
    				
    				}
    				{
    					registra = new JButton();
    					sfondo.add(registra);
    					registra.setText("Registrati");
    					registra.setBounds(482, 277, 63, 22);
    					registra.addActionListener(new ActionListener() {
    						public void actionPerformed(ActionEvent evt) {
    							registraActionPerformed(evt);
    						}
    					});
    				}
    			}
    			pack();
    			this.setSize(760, 560);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    	
    	private void registraActionPerformed(ActionEvent evt) {
    		System.out.println("registra.actionPerformed, event="+evt);
    		
    		patterns.GestInterfaccia.Registrazione();
    	}
    	
    	private void tasto_confermaActionPerformed(ActionEvent evt) {
    		System.out.println("registra.actionPerformed, event="+evt);
    	    patterns.Controlli.ConfermaLogin();
    	}
    
    }
    So, my question is:
    When i click on "Registrati" the button calls GestInterfaccia.Registrazione() which calls Registrazione();

    How can i clean the background "Sfondo"?
    I would like to mantain the container jOptionPane but i would like to "erase" what else is on the screen (like buttons, text fields and labels).

    Any help appreciated!!!
    Thanks.

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    simple way may be to create a default initializer method that does setBackground, setSize, etc for you and have another method that adds your buttons, labels, etc. That way you can just call the init method and "redraw", I guess would be what it's doing, the base layout.

  3. #3
    hendrix79 is offline Member
    Join Date
    Dec 2008
    Posts
    28
    Rep Power
    0

    Default

    Yes, that's what i need, but... i don't know how to do it. Can you show me an example?
    Really big Thanks!!!

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    Java Code:
    public void setInit(JDesktopPane sfondo){
    getContentPane().add(sfondo, BorderLayout.CENTER);
    				sfondo.add(immagineSfondo);
    				sfondo.add(immagineAgenda);
    				immagineSfondo.setIcon(new ImageIcon(getClass().getClassLoader().getResource("immagini/sig.gif")));
    				immagineSfondo.setBounds(new Rectangle(510, 330, 400, 220));
    				immagineAgenda.setIcon(new ImageIcon(getClass().getClassLoader().getResource("immagini/agenda.png")));
    				immagineAgenda.setBounds(new Rectangle(0, 310, 315, 225));
    				sfondo.setForeground(new java.awt.Color(0,128,255));
    				sfondo.setBackground(new java.awt.Color(113,208,255));
    				sfondo.setSize(760, 560);
    
    }
    Java Code:
    public void setButtons(JDesktopPane sfondo){
    //sfondo.addButton or whatever it is in here
    }
    Set your Registrati button to call the blank constructor and setInit() method

  5. #5
    hendrix79 is offline Member
    Join Date
    Dec 2008
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by xcallmejudasx View Post
    Set your Registrati button to call the blank constructor and setInit() method
    ... sorry if i look dumb, maybe i am... but... how can i do this?

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    in your registraActionPerformed just add setInit() to it

    Basically what happens if you create your layout, buttons and all, and once the person clicks the button it calls the setInit() method to redraw everything blank.

  7. #7
    hendrix79 is offline Member
    Join Date
    Dec 2008
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by xcallmejudasx View Post
    in your registraActionPerformed just add setInit() to it

    Basically what happens if you create your layout, buttons and all, and once the person clicks the button it calls the setInit() method to redraw everything blank.
    I tried but... i get this:

    setInit cannot be resolved to a type

    ... what can i do?
    Thanks!

  8. #8
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    you have to write the setInit() method before you can use it.

    public void setInit(){
    crap goes here
    }

  9. #9
    hendrix79 is offline Member
    Join Date
    Dec 2008
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by xcallmejudasx View Post
    in your registraActionPerformed just add setInit() to it

    Basically what happens if you create your layout, buttons and all, and once the person clicks the button it calls the setInit() method to redraw everything blank.
    In fact i wrote it in my Sfondo.java class file, but when i call it in Registrazione.java (or Login.java) it makes that error... :confused:

  10. #10
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    If your trying to access that method outside the class it's written in you need to do ClassName.setInit(); So when you're calling it from Registrazione it would be Sfondo.setInit()

  11. #11
    hendrix79 is offline Member
    Join Date
    Dec 2008
    Posts
    28
    Rep Power
    0

    Default

    Errr.... i tried, but i got this error

    The method setInit(JDesktopPane) in the type Sfondo is not applicable for the arguments ()

    Sorry... but i can't understand. Really. I'm starting to think i'm really dumb.

  12. #12
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    your missing the JDesktopPane argument. When you call setInit you need to send sfondo with it. ie, setInit(sfondo); or whatever your JDesktopPane object is called

  13. #13
    hendrix79 is offline Member
    Join Date
    Dec 2008
    Posts
    28
    Rep Power
    0

    Default

    Ok, i tried but it doesn't work.
    Now it's late for me (Italy here).
    Tomorrow i will try everything again. Just delete it and again.
    I hope you could continue to help, your help is really really precious.

Similar Threads

  1. JDesktopPane demo
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 08:43 PM
  2. How to add SystemTray to JDesktopPane
    By elisabethkron@yahoo.co.uk in forum Advanced Java
    Replies: 0
    Last Post: 04-07-2008, 09:37 PM
  3. clear cache
    By Jadellll in forum New To Java
    Replies: 0
    Last Post: 03-20-2008, 10:27 AM
  4. Help, someone clear up Interfaces for me
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 03:26 AM
  5. I want to do is clear the console
    By paul in forum Advanced Java
    Replies: 7
    Last Post: 08-03-2007, 07:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •