|
-
December 12th, 2009, 05:00 PM
#1
A little help with an Applet problem
I have an applet problem. It happens with nearly all of them that I write (Code snippets can be given on request).
Whenever I have a button click event, literally nothing happens until I click on the outer edge of the applet to resize the window. Then whatever event I have selected happens (either remove(something) or con.add(something)). Every time. I don't really understand why it's happening. Has anyone had this happen to them or maybe give me some advice on how to get the actual button click to take care of it (you know, without adding the extra clicks)?
Thanks!
-
December 13th, 2009, 02:05 AM
#2
Re: A little help with an Applet problem
Nothing huh? Then can anyone tell me if they have even heard of this problem? Has anyone even seen this before? I just want to see if it's something on my end. I am running Windows XP in a virtual machine to compile the java applet, maybe that has something to do with it?
-
December 13th, 2009, 08:41 AM
#3
Re: A little help with an Applet problem
You need to provide more information. What are you trying to do when the button is pressed? Does the button action listener run when you press the button? It sounds like you're not repainting the applet, but without seeing the relevant code, it's hard to say.
If you can post a minimal example that shows this behaviour, we should be able to help.
The cheapest, fastest, and most reliable components of a computer system are those that aren't there...
G. Bell
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
December 14th, 2009, 04:47 PM
#4
Re: A little help with an Applet problem
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JMadLib extends JApplet implements ActionListener
{
Container con = getContentPane();
JButton AddNoun1Button = new JButton("Add word");
JButton AddNoun2Button = new JButton("Add word");
JButton AddAdjectiveButton = new JButton("Add word");
JButton AddVerbButton = new JButton("Add word");
JTextField WordTxtBox = new JTextField(10);
Font headlineFont = new Font("Helvetica", Font.BOLD, 36);
JLabel Heading = new JLabel("Welcome to Mad Libs");
JLabel FirstWord = new JLabel("Enter a noun.");
JLabel SecondWord = new JLabel("Enter another noun.");
JLabel ThirdWord = new JLabel("Enter an adjective.");
JLabel FourthWord = new JLabel("Enter a past tense verb.");
JLabel Complete = new JLabel("Completed Rhyme");
JLabel CompletedRhyme1 = new JLabel();
JLabel CompletedRhyme2 = new JLabel();
String noun1 = "";
String noun2 = "";
String adjective = "";
String verb = "";
public void init()
{
con.add(Heading);
Heading.setFont(headlineFont);
con.add(FirstWord);
con.add(WordTxtBox);
con.add(AddNoun1Button);
con.setLayout(new FlowLayout());
AddNoun1Button.addActionListener(this);
AddNoun2Button.addActionListener(this);
AddAdjectiveButton.addActionListener(this);
AddVerbButton.addActionListener(this);
}
public void stop()
{
WordTxtBox.setText("");
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == AddNoun1Button)
{
noun1 = WordTxtBox.getText();
remove(AddNoun1Button);
remove(FirstWord);
remove(WordTxtBox);
con.add(SecondWord);
con.add(WordTxtBox);
con.add(AddNoun2Button);
WordTxtBox.setText("");
WordTxtBox.requestFocus();
}
if (source == AddNoun2Button)
{
noun2 = WordTxtBox.getText();
remove(AddNoun2Button);
remove(SecondWord);
remove(WordTxtBox);
con.add(ThirdWord);
con.add(WordTxtBox);
con.add(AddAdjectiveButton);
WordTxtBox.setText("");
WordTxtBox.requestFocus();
}
if (source == AddAdjectiveButton)
{
adjective = WordTxtBox.getText();
remove(AddAdjectiveButton);
remove(ThirdWord);
remove(WordTxtBox);
con.add(FourthWord);
con.add(WordTxtBox);
con.add(AddVerbButton);
WordTxtBox.setText("");
WordTxtBox.requestFocus();
}
if (source == AddVerbButton)
{
verb = WordTxtBox.getText();
remove(Heading);
remove(AddVerbButton);
remove(FourthWord);
remove(WordTxtBox);
con.add(Complete);
Complete.requestFocus();
CompletedRhyme1.setText("Mary had a little " + noun1 + " It's " + noun2 + " was " + adjective);
CompletedRhyme2.setText("as snow. And everywhere that Mary " + verb + " The " + noun1 + " was sure to go.");
con.add(CompletedRhyme1);
con.add(CompletedRhyme2);
Complete.setFont(headlineFont);
}
}
}
Here it is. A simple Mad libs game that has this problem occur. Any time you click on a button, nothing will happen until you resize it.
-
December 14th, 2009, 07:39 PM
#5
Re: A little help with an Applet problem
So, what are you trying to do when the button is pressed? Does the button action listener run when you press the button?
[didn't I ask this already?]
The sooner you start to code, the longer the program will take...
R. Carlson
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
December 14th, 2009, 08:51 PM
#6
Re: A little help with an Applet problem
Yes, I thought I made it clear, I guess not - I'll try to explain it better.
When the button is pressed, it removes some labels, adds some labels, passes the information in the textbox to a variable (noun1, noun2, verb, adjective) and yes, as far as I know, the action listener is running. Running the code will hopefully show the problem.
Here's what happens:
When the user enters a word in the text box and clicks the button, nothing happens until you click on the edges of the applet to resize it. Once it registers the resize, it behaves normally. Things get removed and things get added. However, it happens on every button click. The program flows like this: Prompts user for input -> User types input into text box and clicks button -> Label for prompt gets removed and another label is re-added that prompts user to enter another word -> User clicks again to add word -> Label for prompt gets removed and another label is re-added that prompts user to enter another word -> User clicks and all labels are removed and the Mad Lib is shown in it's completed form.
The problem that I'm currently having is whenever a button click is registered, nothing happens with the applet until you click on the edges to resize the app. No exceptions are thrown.
-
December 15th, 2009, 03:43 AM
#7
Re: A little help with an Applet problem
OK, if you're adding or removing components from a container, you need to call validate() on it to get it to lay everything out again.
We think too much about effective methods of teaching and not enough about effective methods of learning. No matter how good teaching may be, each student must take the responsibility for his own education...
J. Carolus S.J.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
December 15th, 2009, 11:05 AM
#8
Re: A little help with an Applet problem
Awesome, that worked perfectly. Problem solved by adding validate(); to the different actions.
Thank you!
-
December 15th, 2009, 07:18 PM
#9
Re: A little help with an Applet problem
Glad to help 
If you understand what you're doing, you're not learning anything...
Anon.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|