Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!

Vanessa Astle

Ranch Hand
+ Follow
since May 08, 2007
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Vanessa Astle

Hey,

My professor had a contest earlier in the year for who could make the best program. Alot of the judging was based on aesthetic appeal.

So if you're interested in using graphics, a game might be fun. I made a memory game (very overdone, though). Another girl made a room modeling system where a user could specify the size of a room and select furniture objects that could be rotated and dragged around the room. The winner made a version of "Collapse" that had different levels of difficulty.
13 years ago
Hey!

1.I'm pretty sure you can store the pictures anywhere as long as you put the full directory in the code.

So, ImageIcon anImage = new ImageIcon("/home/ImageFiles/imageForCard.jpg");

But, if you want, you can store the image files in the same folder as your code is located and then you will only have to put the file's name.

So, ImageIcon anImage = new ImageIcon("imageForCard.jpg");

2. If you want the objects on your GUI to be buttons, and you want those buttons to display images, I think the code you have found would be the code to use.

I actually just finished a card (memory/matching) game GUI earlier in the year, and my professor recommended that I use the acm.graphics package. I found it really helpful. Especially because a card game is so graphical, and you will probably care more about the pictures and images than the other SWING compontents.

jtf.acm.org -They've got loads of tutorials and demo's. It's very easy-to-use!!
13 years ago
Thanks so much for your answer Stan! That's definitely what I was looking for and it will save me a giant mess and keep my program OO.
That's correct. A thread that gives birth to itself. It's really just recursion.
Okay, so say I'm dealing with nested threads. (I'm just making a simple one up). And I've got a variable that is changing. When my last nested thread is complete, how can I return that variable to the class that contains the main method?

public class ECounter
{
public static void main(String[] args)
{
Execution anExecution = new Execution(0,"sleepwalker");
Thread aThread = new Thread(anExecution);
aThread.start();
int eCount = /*Here is where I want the number of e's that were
counted in the nested threads.*/
}
}

public class Execution implements Runnable
{
private int counter;
private String word;

public Execution(int counter, String word)
{
this.counter = counter;
this.word = word;
}

public void run()
{
if(word.startsWith("e"))
counter++;
word = word.substring(1,(word.length()-1));

if(word.length() == 0)
{
/*Here is the point when I want a value returned. I want to return
the counter to the main class, when the String word is empty. But I
have no idea how, since I can't change the "void" return type for
the run method to "int"!*/
}
else
{
Execution nextExecution = new Execution(counter,word);
Thread nextThread = new Thread(nextExecution);
nextThread.start();
}
}
}

Thanks so much for your time!!!
A NullPointerException occurs when a variable has not been initialized. Say I created a Cat object (I'm just making up an object) reference variable, and didn't assign it a value, then tried to use that reference variable:

Cat snowball; //This is bad, I did not initialize cat.
CatFood tuna = new CatFood(); //This is good, I initialized CatFood.
snowball.feed(tuna); //When I call the method feed on my cat variable, the computer doesn't actually have a cat to feed.

So you must make sure that you initialize ALL of your variables, even if it is only to something useless, like "null".
13 years ago
I think that this invokeLater() method is exactly what I've been looking for, and I just didn't know it!

"This method should be used when an application thread needs to update the GUI." (from the Java API Method Summary) - has been the issue I've been dealing with all along.

Thanks so much! I'm so excited to edit my code, and hopefully I can get the GUI running like I want it to!
AutomataFrame is a class which I've created. I do follow the MVC pattern in my code, which was the purpose of the getter methods. I'm trying to get my controller to retrieve user input from my GUI, and then hand that information on to my model. This is where I'm coming in to problems.

On the GUI, the user clicks a button. The button is called "Graph Setup". This JDialog box has 4 JTextFields (accompanied by labels), a "Finished" button and a "Cancel" button. A user places the appropriate information in the text fields. If they click "Cancel", none of the information is retained and the JDialog is closed. The user goes back to a default frame. If the user clicks the "Finished" button, the information is to be retained and sent back to the controller which will then send that information to the model classes.

Well, that's how I want the flow of control to work. However, the getter methods that are being called get their information after the Graph Setup Dialog pops up, instead of waiting until the user types information into the textfields. This way, the getter methods are always returning default values (or null, if that be the case).

I'm using Eclipse, and the program thus far is over 20 classes long. That is why I tried to restrict what I posted to be only the relevant code.

Regardless, I am not worried about solving this particular problem. I know that there is more than one way to do things, so I'm sure if I approached the problem from a different angle, I'd be able to work things out. But this is something I deem worth finding out about, regardless. I'm not as thread savvy as I would like to be, so I thought I would inquire about the problem brought up from my particular situation. Is it possible to create a Thread from an object of a class that implements ActionListener instead of Runnable?



The questions I wanted to get through with my extensive explanation was simply:

My IDE, and several reference books, tell me that a class has to implement Runnable in order to create a Thread from an instance of that class.

Thread aThread = new Thread(new AClassThatImplementsRunnable());

So that you can call start() on the thread.

But what if I don't want to call start() on the thread. I want the thread to run on its own when, say, a button is clicked. That way, I wouldnt' have to worry about knowing when to initialize and start the thread. But I could control when it would run after it had been started. Since the thread runs when the button is clicked, it would work from an actionPerformed() method as opposed to a run() method.

Is there any way of doing this?
Yes I am. The javadoc I posted in front of the samples of code point out what I was describing in my problem. I only posted the relevant code.

This is a section of the code from "the method that initialized the class":
GraphSetupPopup graphSetupPopup = new GraphSetupPopup(frame,popupGenerator,nodeList,transitionList);
graphWidth = graphSetupPopup.getGraphWidth();
graphHeight = graphSetupPopup.getGraphHeight();
nodeWidth = graphSetupPopup.getNodeWidth();
nodeHeight = graphSetupPopup.getNodeHeight();

And this is part of the constructor that creates the popup. I just took out the popup stuff, but left the text fields that my reference variables are being set from:
public GraphSetupPopup(AutomataFrame frame,PopupGenerator popupGenerator,ArrayList<Node> nodeList,
ArrayList<Transition> transitionList)
{graphWidthField = new JTextField(6);
graphHeightField = new JTextField(6);
nodeWidthField = new JTextField(6);
nodeHeightField = new JTextField(6);

JButton finishedButton = new JButton("Finished");
finishedButton.addActionListener(new FinishedButtonListener());
Hey! I've created a method that initializes a class. In this class, the constructor creates an popup with a series of text fields to fill out and then has a "Finished" button with an action listener attached. When the finished button is clicked, a number of variables are set from null to the values in the text fields. Then, the method that initialized the class calls getter methods from the class for those variables.

My problem is that the getter methods are being called after the constructor for the class has executed, but not before the action performed method for my action listener has been executed. I'm having issues threading it because of the action listener.

Here's some tidbits of code that are the focus of my problem:



Any suggestions would be greatly appreciated! I hope I explained my problem clearly/thoroughly!

Thanks for your time!
Alright, I decided to use the GridBagLayout class and it helped me out with the layout quite a bit. Thanks for your help!
13 years ago
I'm trying to create a popup where a user has to choose options from a few different fields.

According to "The Definitive Guide to Java Swing" and the numerous other textbooks on swing that I have piled around me, a JOptionPane is "a special class for creating a panel to be placed in a pop-up window". And a Dialog is "a class that represents the standard pop-up window". Yet, in Java JOptionPane's seem sort of interchangeable with dialogs. I am being told that I can place a JOptionPane "in" a JDialog or JInternal Frame by making a reference of those types and assigning them to the JOptionPane object.

I just wanted an easier way to make a popup. I really didn't want to have to deal with layouts, and the JOptionPane class seemed like an ideal way to get a readymade panel, insert it into some sort of frame/dialog popup, and go from there!

Any insight would be fabulous! Thanks so much!
13 years ago
Just create a ButtonListener class that implements the ActionListener class. Then create an actionPerformed(ActionEvent e) method, and within that method instantiate a JDialog, or whatever kind of popup you were wanting. That way you can attach a ButtonListener object to a button, and each time the button is pressed the actionPerformed(ActionEvent e) method will be called.
13 years ago
Is it possible to add more than one JOptionPane to a dialog?
13 years ago