Problem with classes


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Problem with classes

Hybrid View

  1. #1
    Join Date
    Aug 2004
    Posts
    3

    Problem with classes

    Hi, I'm developping a simple JAVA Applet and I have a problem
    I have a class called LUIweb which is the main class. Here is a part of it.

    public class LUIweb extends JApplet implements Runnable {

    public void run() {
    DisplayMessage("JAVA rocks");
    }
    public void DisplayMessage(String message){
    jtextArea.append(message);
    middlePanel.removeAll();
    middlePanel.add(textScrollPane, BorderLayout.CENTER);
    middlePanel.validate();
    middlePanel.repaint();
    }
    }


    It displays the message "JAVA rocks" in my JTextArea. But when another class from the same package calls DisplayMessage, nothing is displayed in my JTextArea


    public class Data_net extends LUIweb{
    public void Test(){
    DisplayMessage("NON");
    }
    }

    Have you got any idea to fix that?

    Thank you for you help

  2. #2
    Join Date
    Jun 2004
    Posts
    199
    Don't see anything readily.

    Need more code.

    But do try adding an .invalidate(); before the validate.

  3. #3
    Join Date
    Aug 2004
    Posts
    3
    I already tried "invalidate()" before validate(), no good result

    Code:
    package applet;
    
    import java.awt.*;
    import java.util.Vector;
    import java.lang.String;
    import javax.swing.*;
    
    import applet.Language;
    public class LUIweb extends JApplet  implements Runnable {
    	Vector Soft;
    	Vector Version;
    	
        public JPanel middlePanel = new JPanel();
        public JTextArea textArea = new JTextArea();
        public JScrollPane textScrollPane = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        public Container container;
        
        public LUIweb(){
        	container = getContentPane();
    		textArea = new JTextArea(30,40);
            textArea.setEditable(false);
            middlePanel = new JPanel();
            //textArea.setPreferredSize(new Dimension(400,450));
            
            textScrollPane = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            middlePanel.add(textScrollPane, BorderLayout.CENTER);
            container.add(middlePanel, BorderLayout.CENTER);
        }
        
    	public void init(){
    
    		container = getContentPane();
    		textArea = new JTextArea(30,40);
            textArea.setEditable(false);
            middlePanel = new JPanel();
            //textArea.setPreferredSize(new Dimension(400,450));
            
            textScrollPane = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            middlePanel.add(textScrollPane, BorderLayout.CENTER);
            container.add(middlePanel, BorderLayout.CENTER);  
          	}
    	
    	
    	public void start() {
            (new Thread(this)).start();
        }
    
        // Arr?t de l'applet
        public void stop()  {
            while(true);
        }
    
        // Un nouveau thread pour la r?actualisation de l'applet
        public void run() {
        	DisplayMessage(Language.CONNECT_DB);
    
    		Data_net Donnee_net=new Data_net();
    		DisplayMessage("Connecting to "+Donnee_net.GetHost()+" with user "+Donnee_net.GetUser()+"...\n");
    Donnee_net.Test();
        }
        public void ShowWish(Vector Softs, Vector Versions,char Action){
        	DisplayMessage(Language.WISH+"\n");
        	for (int i=0;i<Softs.size();i++){
        		String message=(String) Softs.get(i);    		message=message.concat((String) Versions.get(i));
        		String[] t = message.split("\n");
        	    message = t[0];
        	    for (int j=1; j < t.length; j++)
        	      message = message.concat("_".concat(t[j])+"\n");
        	    DisplayMessage("\t"+message);
        	}
        }
        public void DisplayMessage(String message){
        	this.textArea.append(message);
        	this.middlePanel.removeAll();
        	this.middlePanel.add(textScrollPane, BorderLayout.CENTER);
    		// repaint
        	this.middlePanel.validate();
        	this.middlePanel.repaint();
        }
    	
    	public void main(String[] arguments) throws NullPointerException {
    
    	}
    }
    Code:
    package applet;
    
    import com.maverick.ssh.*;
    import com.maverick.ssh1.Ssh1Client;
    import com.maverick.ssh2.*;
    import com.sshtools.net.*;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.Vector;
    
    import javax.swing.*;
    
    
    public class Data_net extends LUIweb{
    	public Data_net(){
    		DisplayMessage("TEST");
    
    		AskHost();
    		AskUser();
    	}
    	public void AskHost(){
    		String inputValue = JOptionPane.showInputDialog(Language.ASKHOST);
    		hostname=inputValue;
    		int idx = hostname.indexOf(':');
    		if(idx > -1) {
    			port = Integer.parseInt(hostname.substring(idx+1));
    			hostname = hostname.substring(0, idx);
    
    		}
    	} 
    	public void AskPass(){
    		Frame frame = new Frame();
    		while (frame.password==null);
    		password=new String(frame.password);
    	}
    	public String GetPass(){
    		return password;
    	}
    	public String GetHost(){
    		return hostname;
    	}
    	public String GetUser(){
    		return username;
    	}
    	public void AskUser(){
    
    		String inputValue = JOptionPane.showInputDialog(Language.ASKUSER);
    		username = inputValue;
    		if(username==null || username.trim().equals(""))
    			username = System.getProperty("user.name");
    	}
            public void Test(){
                    DisplayMessage("NON");
            }
    }
    So here are two of my classes. Every thing is working but the call of DisplayMessage in Data_net...

    I hope it'll help you to find a solution

  4. #4
    Join Date
    Jun 2004
    Posts
    199
    Ok. Lots of issues. I'll skip those. But it looks like the problem (and the issues hide it) is that you don't call the super class' constructor or the init method.

  5. #5
    Join Date
    Aug 2004
    Posts
    3
    Ok, I've fix it thanks to a friend.

    in my LUIweb class, I call a method from Data_net with the reference from LUIweb like that:
    Code:
    Donnee_net.Test(this);

    what gives in Data_net class:

    Code:
    public void Test(LUIweb instance){
                    instance.DisplayMessage("NON");
    }

    Here we go!!

    Nevertheless thanks for your help mnuttall.

  6. #6
    Join Date
    Jun 2004
    Posts
    199
    Glad you got it working.

    But you probably shouldn't have as a parameter to a method the class in which the method exists. Again, not seeing all the code makes it difficult. Calling the super's constructor would definitely be better.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center