GUI questions
Hey! I'm currently doing a Java assignment at my university, but it's spring break and I'm stuck. I have a couple of questions that I'm sure are newbie questions. Don't be mad!
Okay. I'm making a GUI right now, and we're supposed to run the GUI in its own (the event dispatch?) thread. As far as I've gathered, that's achieved with an anonymous class like so:
Correct? Is this what I want to do every time I open a dialog from the main thread, like simple JOptionPanes and stuff (but if I were to open one from a window I already opened in the first way, that wouldn't be needed, or would it?)?
Okay, so here's my main question. When I've run the statement above, is there any way I can tell the SomeWindow (arbitrary name, of course) I've created to do something from the main thread? Specifically, if someone chooses a MenuItem in the SomeWindow, I want to create a new instance of a top-level class (I've done this bit) and then tell the SomeWindow (this is what I can't do) to print out something inside that object. How do I do that? I've tried something along the lines of this, but that doesn't let me talk to the SomeWindow object:
ssss) gives the task to the other thread (unless you correct me), maybe the SomeWindow isn't pointed at by theWindow until that thread returns, or something, or until invokeLater() returns, or oh man I have no idea what I'm talking about really. Do I need to think less MVC? I'm a good student and entertain the professors when they rave on about MVC and UML and any and all buzzwords. (I don't want to create the window in View's constructor because I want to have View objects without several windows.)
Thanks for any and all help!
Okay. I'm making a GUI right now, and we're supposed to run the GUI in its own (the event dispatch?) thread. As far as I've gathered, that's achieved with an anonymous class like so:
SwingUtilities.invokeLater(new Runnable() { public void run() { new SomeWindow(); } });Correct? Is this what I want to do every time I open a dialog from the main thread, like simple JOptionPanes and stuff (but if I were to open one from a window I already opened in the first way, that wouldn't be needed, or would it?)?
Okay, so here's my main question. When I've run the statement above, is there any way I can tell the SomeWindow (arbitrary name, of course) I've created to do something from the main thread? Specifically, if someone chooses a MenuItem in the SomeWindow, I want to create a new instance of a top-level class (I've done this bit) and then tell the SomeWindow (this is what I can't do) to print out something inside that object. How do I do that? I've tried something along the lines of this, but that doesn't let me talk to the SomeWindow object:
class Main {
public static void main(String[] a) {
view = new View();
view.createThatWindow();
view.printThis(new Thing());
}
}
class Thing {
// SOMETHING HAPPENS HERE
}
class View {
SomeWindow theWindow;
void createThatWindow() {
SwingUtilities.invokeLater(new Runnable() { public void run() { theWindow = new SomeWindow(); } });
}
void printThis(Thing thing) {
theWindow.heyPrintThis(thing); // Here I get a null-pointer exception :(
}
class SomeWindow extends JFrame {
SomeWindow() {
// CONSTRUCTOR THINGS GO HERE I GUESS
setVisible(true);
}
void heyPrintThis(Thing thing) {
// THEN PRINT SOME
}
}
}Is my thinking way off? Because the SwingUtilities.invokeLater(anonymousClasThanks for any and all help!
