LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-27-2004, 01:22 PM   #1
Risc91
Member
 
Registered: Jan 2002
Location: Macomb, MI
Distribution: Ubuntu|Red Hat ES|AIX
Posts: 161

Rep: Reputation: 30
java - Writing to a text area


I have a text area that I created in one class and I'm trying to write to that text area from another class. Everything seems to be fine except the text that my function writes doesn't show up in the text area. I'm not receiving any errors.

Here is the class that holds the text area I'm trying to write to:

Code:
public class ImageInspectorFrame extends JFrame {
    
    // txtArea size
    int rows = 15;
    int cols = 30;
    
    JButton connectButton,disconnectButton,inspectButton;
    JTextArea statusWindow = new JTextArea(rows,cols);
    
     public ImageInspectorFrame() {
                
        JMenuBar menuBar = new JMenuBar();
        JMenu menuFile = new JMenu();
        JMenuItem menuFileExit = new JMenuItem();
        
        menuFile.setLabel("File");
        menuFileExit.setLabel("Exit");
        
        // Add action listener.for the menu button
        menuFileExit.addActionListener
        (
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ImageInspectorFrame.this.windowClosed();
                }
            }
        ); 
        menuFile.add(menuFileExit);
        menuBar.add(menuFile);
        
        setTitle("ImageInspector");
        setJMenuBar(menuBar);
        setSize(new Dimension(400, 400));
        
        // Add window listener.
        this.addWindowListener
        (
            new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    ImageInspectorFrame.this.windowClosed();
                }
            }
        );  
    
    
    	//create buttons, txtfields
    
    	connectButton = new JButton("Connect");
    	disconnectButton = new JButton("Disconnect");
    	inspectButton = new JButton("Inspect!");
    	

//******This what I'm trying to write to *************
    	//statusWindow = new JTextArea(rows,cols);
    	statusWindow.setEditable(false);
    	statusWindow.setLineWrap(true);
    	statusWindow.setWrapStyleWord(true);
    	
    	//create panels to hold buttons/windows
    
    	JPanel buttonPanel = new JPanel();
    	JPanel statusPanel = new JPanel();
    	
    	buttonPanel.add(connectButton);
    	buttonPanel.add(inspectButton);
    	buttonPanel.add(disconnectButton);
    	    	
    	statusPanel.setBorder(BorderFactory.createCompoundBorder(
    							BorderFactory.createTitledBorder("Status"),
    							BorderFactory.createEmptyBorder(5,5,5,5)));
    							
    	statusPanel.add(statusWindow);
    	
    	JPanel mainPanel = new JPanel();
    	mainPanel.setLayout(new BoxLayout (mainPanel, BoxLayout.PAGE_AXIS));
    	mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    	
    	mainPanel.add(buttonPanel);
    	mainPanel.add(statusPanel);
    	
    	getContentPane().add(mainPanel);
    	
    	//add action listeners for buttons
    	connectButton.addActionListener(new ButtonListener());
    	inspectButton.addActionListener(new ButtonListener());
    	disconnectButton.addActionListener(new ButtonListener());
    	
    
    }

here's the class i'm trying to write from:

Code:
public class ImageInspectorConnect  {
	
	
	public void connect () {
		
		ImageInspectorFrame gui = new ImageInspectorFrame();
		
		try {
	    	
	    		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	
	    	} catch (Exception e) {
	     	 	System.out.println("Failed to load JDBC/ODBC driver.");
	      	 	return;
	    	  }
	    	  
	    try {
	    	
	      Connection con = DriverManager.getConnection( 
	      	"jdbc:odbc:Brandon", "user", "passwd");
	
	      System.out.println("Connected.");
	      
	      DatabaseMetaData dmd = con.getMetaData();
	      
	      /**
	      Statement stmt = con.createStatement();
	      String query = "SELECT status, statusDetail FROM SiteSeer WHERE (monitor = 'Gus: Problem Solver') AND (monitorcheckdate BETWEEN '3/5/02 00:00:00' AND '3/6/02 00:00:00')";
		  ResultSet rs = stmt.executeQuery(query);
		  while (rs.next()) {
		  	String status = rs.getString("status");
		  	String statusDetail = rs.getString("statusDetail");
		  	System.out.println("Problem Solver: " + status + " " + statusDetail); */
		  
		  
			
	      if (dmd == null) {
	        System.out.println ("No database meta data available");
	      } else {

//***I try to write this to the text area	
	      		
gui.statusWindow.append ("Database Product Name   : " + dmd.getDatabaseProductName());     
	        //System.out.println ("Database Product Version: " + dmd.getDatabaseProductVersion());
	        //System.out.println ("Database Driver Name    : " + dmd.getDriverName());
	        //System.out.println ("Database Driver Version : " + dmd.getDriverVersion());
	      }
	      
	      con.close(); 
	
	    } catch (Exception e) {
	      e.printStackTrace();	
	      }
	}
}

Last edited by Risc91; 05-28-2004 at 10:14 AM.
 
Old 05-27-2004, 03:03 PM   #2
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 53
Do you need to call update() or repaint() on the text area?
 
Old 05-28-2004, 04:55 AM   #3
ludeKing
Member
 
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181

Rep: Reputation: 30
I did the same thing in a Java assignment a while back and you need to use the setText() method.

textArea.setText("blah");

Thats how I did it.
I had a method in the class with the text area, and when I wanted to write from a nother class to the textarea I called the method that did it in the other class.
 
Old 05-28-2004, 07:31 AM   #4
Risc91
Member
 
Registered: Jan 2002
Location: Macomb, MI
Distribution: Ubuntu|Red Hat ES|AIX
Posts: 161

Original Poster
Rep: Reputation: 30
Hmm...the setText did not work. I appreciate the suggestion though. After you mentioned it I thought that would work for sure.

I think Komakino might be onto something though. I'm going to look into this a little further. I'm thinking my understanding of objects and classes maybe a little off. It seems to me that the instance of the statusWindow that I'm working with is not the same one being displayed in the first code snippet.

If anyone else has any suggestions please post!

TIA

Last edited by Risc91; 05-28-2004 at 07:56 AM.
 
Old 05-28-2004, 08:22 AM   #5
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Ubuntu
Posts: 208

Rep: Reputation: 31
Try writing a little test case that brings it down to the bare essentials.

I was able to update a text area (that has the default access level)
from another class. Perhaps you're just not getting metadata? Oh,
you do have your append statement commented out! Please modify
the posted code to show exactly what you're really running.

Code:
public class TestTextArea {

   public static void main( String[] args ) {

      TextAreaFrame taf = new TextAreaFrame();

      taf.textArea.append( "This is some text." );
      taf.textArea.append( "This is some more text." );
   }

}
Code:
import javax.swing.*;

public class TextAreaFrame {

   JTextArea textArea = null;

   public TextAreaFrame() {

      JFrame frame = new JFrame();
      frame.setSize( 300 , 300 );
      frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

      textArea = new JTextArea( 10 , 20 );
      textArea.setEditable( false );
      textArea.setLineWrap(true);
      textArea.setWrapStyleWord(true);

      JPanel panel = new JPanel();
      panel.add( textArea );

      frame.getContentPane().add( panel );

      frame.show();

   }
}

Last edited by eric.r.turner; 05-28-2004 at 08:56 AM.
 
Old 05-28-2004, 10:18 AM   #6
Risc91
Member
 
Registered: Jan 2002
Location: Macomb, MI
Distribution: Ubuntu|Red Hat ES|AIX
Posts: 161

Original Poster
Rep: Reputation: 30
Sorry about that. I stuck that in there during the post so people would know where I was trying to do the append. I changed it so it properly reflects the code now.

It does return metadata because I can print it to the console just fine. I can write to the text field from a class thats within the base class just fine. But anything outside of that is a no go.

Damn, based on your code, I feel like I'm doing the exact same thing.

Last edited by Risc91; 05-28-2004 at 10:21 AM.
 
Old 05-29-2004, 12:58 PM   #7
aaa
Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Rep: Reputation: 46
What thread are you calling setText from? It should be on the event-dispatch thread. You can try:
Code:
SwingUtilities.invokeLater(new Runnable(){
    public void run(){
    gui.statusWindow.append ("Database Product Name   : " + dmd.getDatabaseProductName());
    }
});
This will invoke on the event-dispatch thread. After a Swing component appears on screen, it should only be modified from the event-dispatch.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
vb.net reading and writing to a text file simultaneously mrobertson Programming 3 09-08-2005 12:30 PM
Writing to a text file vb.net mrobertson Programming 5 08-04-2005 12:37 PM
Can't enter text in certain Java text fields TheBelush Linux - Software 4 04-27-2005 05:29 PM
Writing Java applets munchies Programming 2 09-05-2004 08:44 AM
Text editor for writing bgfay Linux - Software 3 07-19-2004 01:32 AM


All times are GMT -5. The time now is 07:54 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration