Many classes, one GUI?
I have a main class that calls a public GUI class the sets up all the components and basically controls the entire program though listeners. I have other classes where when an exception occurs, I'd like to get a JOptionPane to popup showing the error message. Despite my best efforts though, I cannot get the variables for the JEditorPane and JOptionPane over to my other classes so I can call them. I have tried making these variables public, then public static, then writing methods inside the gui class to get() these variables, but always run into some type of error. What's the best way to do this?
[612 byte] By [
subnetrxa] at [2007-10-3 2:34:24]

Sorry, you're not being very clear. Please let me know what errors you always run into. Your GUI class should have getXXX() methods for any GUI components that you need to expose to other classes.
Well, that probably answers my question right there. I looked in the API for JFrame and I see getContentPane, getJMenuBar and getLayeredPane. I have a JOptionPane and a JEditorPane on top of a JScrollPane. Are there any examples of modifying the GUI in different classes in a tutorial somewhere?
Message was edited by:
subnetrx
Don't you keep references to the widgets you create in your GUI class?
private JOptionPane optionPane = ...;
public getOptionPane() {
return optionPane;
}
yes, I have these setup in my main class (TidyCapture):
javax.swing.JEditorPane getEditorPane()
{
return jEditorPane1;
}
public javax.swing.JOptionPane getOptionPane()
{
return jOptionPane1;
}
So in my main method I call:
TidyCapture TC1 = new TidyCapture();
TC1.setVisible(true);
But when I call TC1.getOptionPane() from another class, I get an unresolved symbol on the TC1. I can't initialize a new version of TidyCapture since that would just re-initialize the entire GUI.
It sounds like you need to make TC1 a static instance of your Main class, and add a static getter so other classes can get ahold of that instance.Main.getTidyCaptureInstance().getOptionPane(). ...
Don't make TC1 static. Pass the reference to the other class's constructor. That is, do this in your other class:
public class MyOtherClass
{
private TidyCapture myTidyCapture;
public MyOtherClass(TidyCapture tidy)
{
myTidyCapture = tidy;
}
public void someOtherMethod()
{
JOptionPane optionPane = myTidyCapture.getOptionPane();
// call methods on the optionPane
}
}
MLRona at 2007-7-14 19:33:23 >

> Don't make TC1 static. Pass the reference to the> other class's constructor. What's wrong with making it static? What I suggested is exactly what the Java dev team did when they created the DateFormat class....
> > Don't make TC1 static. Pass the reference to the
> > other class's constructor.
>
> What's wrong with making it static? What I suggested
> is exactly what the Java dev team did when they
> created the DateFormat class....
Don't know exactly what that means (I didn't study DateFormat to find out). But, I don't think static is the right way to go here. What if there are multiple instances of the other class, that need different TidyCapture instances? You shouldn't make things static just to make it "easier" to reach them.
MLRona at 2007-7-14 19:33:23 >

> Don't know exactly what that means (I didn't study
> DateFormat to find out).
DateFormat fmt = DateFormat.getDateTimeInstance();
They also do it here:
Toolkit.getDefaultToolkit();
And here:
Thread.currentThread();
I guess it makes sense if only one instance will exist at a time.
> What if there are
> multiple instances of the other class, that need
> different TidyCapture instances?
That's a good point, thanks for the clarification.
Okay, I see you got my point.
I don't know how DateFormat works--it does say that it is not thread safe. The fact that getDateTimeInstance is a static method doesn't mean that the DateFormat reference returned is always a reference to the same object.
But, definitely Thread.currentThread() isn't the same as your suggestion for the TidyCapture object TC1. Thread.currentThread() is a static method, but it doesn't always return the same value--it varies depending on what is actually the current thread.
MLRona at 2007-7-14 19:33:23 >

Thanks for the help. What I am running into is this, when I try to pass the instance of TidyCapture to my listener method in the TidyCapture class, I get an unresolved symbol. Should I use the this keyword? When I use it, the program is compiling fine, but nothing is getting printed to the screen, eventhough my command line shows exactly what I want. Here is the method calling it in which I'm trying to pull all the anchor tags of an xhtml document:
public String getUrls(Document xmldoc)
{
String xmlString = "null";
javax.swing.JEditorPane jEditorPane1 = TC1.getEditorPane();
XPathContext context = new XPathContext("html", "http://www.w3.org/1999/xhtml");
Nodes titles = xmldoc.query("//html:a", context);
for (int i = 0; i < titles.size(); i++)
{
System.out.println(titles.get(i).toXML());
int position = jEditorPane1.getText().length();
try
{
jEditorPane1.getDocument().insertString(position, titles.get(i).toXML(), null);
}
catch(javax.swing.text.BadLocationException e)
{
System.out.println(e.getMessage());
}
}
return xmlString;
}
> when I try to pass the instance of TidyCapture to my> listener method in the TidyCapture class, I get an> unresolved symbol.I'm confused by that, do you want an object to listen to itself?
You never set xmlString to anything besides the literal String "null", as far as I can see. Are you missing something?
I don't know where you are getting the "unresolved symbol" with your listener and TidyCapture. You'll have to post more code and the exact error message for anyone else to be able to figure out what is wrong.
MLRona at 2007-7-14 19:33:23 >

yeah, ignore XMLString, I've been working on this and I was returning that to my TidyCapture class so that I could put it on the JEditorPane, but now I'm trying to do that directly inside the class.
So here's a bit more code, basically my structure looks like this:
public class TidyCapture
{
public TidyCapture()
{
initComponents(); //setup GUI
}
private void startAction(Actionevent evt)
{
TidyParser TidyParser1 = new TidyParser(this);
Document tidydoc = TidyParser1.tagParser(userURL);
String xmldoc = tidydoc.toXML();
jEditorPane1.setText(xmldoc);
String xmlString = TidyParser1.getUrls(tidydoc);
String bodyString = TidyParser1.getBody(xmldoc);
int position = jEditorPane1.getText().length();
try
{
jEditorPane1.getDocument().insertString(position, bodyString, null);
}
catch(javax.swing.text.BadLocationException e)
{
System.out.println(e.getMessage());
}
}
public static main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
TidyCapture TC1 = new TidyCapture();
TC1.setVisible(true);
}
});
}
//GUI Variables declaration
}
TidyParser is the class I'm trying to pass the GUI components to.
Did you make a TidyParser constructor to match what I called "MyOtherClass" in Reply #6? If so, passing "this" (in startAction, as you do) to the constructor should work.
yep, copied it pretty much word for word. When I call things like getEditorPane in the TidyParser class, I don't get errors so it must be working. If you look at this block though:
System.out.println(titles.get(i).toXML());
int position = jEditorPane1.getText().length();
try
{
jEditorPane1.getDocument().insertString(position, titles.get(i).toXML(), null);
}
catch(javax.swing.text.BadLocationException e)
{
logger.debug(e.getMessage());
}
It prints to the command line but not to the screen. Also the jEditorPane1.getDocument line works, because I can call it in the TidyCapture class and it prints to screen.
And jEditorPane1 is (using the names from my example) myTidyCapture.getEditorPane()?
I've never used Documents/JEditorPanes, so I don't know if you are missing anything. Hard to tell with small snippets of code, but your whole code is probably way too much to try to sort out here. Is your latest reply part of TidyParser?
If you do this in TidyCapture:
System.out.println(TC1.jEditorPane1);
and this in TidyParser:
System.out.println(myTidyParser.getEditorPane());
do they give the same result? It may look a little funny, but you should be able to tell if they are the same.
after my last build where I took out some of the old stuff and some system.out.println's, it started working. I went ahead and tried your example anyway though and they are exactly the same, so it looks like my problem is fixed ( or at least this one :) ).
Okay. Glad it works. Post again if you have more trouble.