ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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();
}
}
}
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.