|
Because I tried setting it to the name of the radiobutton but that didn't work either.
rbTable[i].setActionCommand(Integer.toString(i) + " Times");
|
|
|
|
|
Do you know what the command is for?
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
That is not the way to do it I am afraid. Visual components use event handlers which are signalled when the events (e.g. pushing a button) occurs. So you need to use the Button (Java Platform SE 7 ) addActionListener[^] method so your code gets called at the relevant time.
I would again strongly suggest you go to The Java™ Tutorials[^] and work through them a few times. Trying to learn a language and framework by posting questions here (or any other forum) will take you much longer to get to where you want.
|
|
|
|
|
Thanks Richard
I have and do look at the Java Tutorials but don't always find them helpful even though they explain the concepts of components.
I did eventually figure it out, the code is below for anyone else to look through,
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
AbstractButton aButton = (AbstractButton)itemEvent.getSource();
int state = itemEvent.getStateChange();
String name = aButton.getText();
for (int i = 1; i < rbTable.length; i++)
{
if (state == ItemEvent.SELECTED) {
if (name.equals(rbTable[i].getText())) {
userText.setText(rbTable[i].getText());
}
}
}
}
};
|
|
|
|
|
MallardsReach wrote: don't always find them helpful even though they explain the concepts of components. What about all the sample code that's included? Seriously, you really should persevere with them. They teach things in a structured order and you get all the basic concepts well established before moving on to the more advanced stuff. I used the published version of these years ago when I first started working in Java, and have gone back and repeated the online versions a number of times.
|
|
|
|
|
Hi Richard,
Can't argue and have no wish to, with the points you make about the sample code,
The thing is they are stand alone ie
Here's a method, here's how to call it.
Here' some radio buttons, here's how to find the one selected.
And they work fine the problem is when your project gets more added to it and includes many components and then it's trying to get them to work with each other.
I am going to get 'Java: The Complete Reference, Eleventh Edition' as soon as things get back to normal.
Until then I have to rely on the tutorials and forums to guide me.
|
|
|
|
|
Talk about making things complicated!
Here is the final code, no user defined method was needed, I just had to place the results for pressing a button in the 'itemStateChanged method'
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
AbstractButton aButton = (AbstractButton)itemEvent.getSource();
int state = itemEvent.getStateChange();
String name = aButton.getText();
for (int i = 1; i < rbTable.length; i++)
{
if (state == ItemEvent.SELECTED) {
if (name.equals(rbTable[i].getText())) {
displayTable.setText("");
int firstNum, answerNum;
for (firstNum = 1; firstNum <=12; firstNum ++)
{
answerNum = firstNum * i;
displayTable.append(" " + firstNum + " x " + i + " = " + answerNum +"\n");
}
}
}
}
}
};
|
|
|
|
|
Looked at many examples of Method and they all seem easy until I start to place them in my code.
No matter where I insert the method or how I rewrite it I still get a problem.
Below is my Method followed by my code. What am I not doing?
void getSecond(int secondNum){
displayTable.setText("\n");
int firstNum, answerNum;
for (firstNum = 1; firstNum <=12; firstNum ++){
answerNum = firstNum * secondNum;
displayTable.append(firstNum + " x " + secondNum + " = " + answerNum +"\n");
}
import javax.swing.*;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyTables
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Tables");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.RED));
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel)
{
panel.setLayout(null);
JTextArea displayTable = new JTextArea("Text Area");
displayTable.setEditable(false);
displayTable.setColumns(20);
displayTable.setFont(new Font("Times New Roman", 1, 14));
displayTable.setLineWrap(true);
displayTable.setRows(14);
displayTable.setWrapStyleWord(true);
displayTable.setBounds(200,20,230,260);
displayTable.setBorder(BorderFactory.createLineBorder(Color.GREEN));
displayTable.setMaximumSize(new java.awt.Dimension(5, 22));
panel.add(displayTable);
JLabel userLabel = new JLabel("Enter a Number");
userLabel.setHorizontalAlignment(SwingConstants.CENTER);
userLabel.setFont(new Font("Verdana", Font.ITALIC, 16));
userLabel.setBounds(10,20,180,25);
userLabel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(10,50,165,25);
panel.add(userText);
JButton loginButton = new JButton("Enter");
loginButton.setBounds(10, 80, 80, 25);
panel.add(loginButton);
loginButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
displayTable.setText("Button Pressed");
}
});
}
}
|
|
|
|
|
MallardsReach wrote: I still get a problem. That is one of the most common reports that we see here, and also the most useless. Unless you tell us exactly what the problem is (including exact error messages), and where it occurs, then we can do very little. Please try to help us to help you.
|
|
|
|
|
Sorry Richard, honestly thought I had given the error.
If I place the code here
public class MyTables
{
void getSecond(int secondNum){
displayTable.setText("\n");
int firstNum, answerNum;
for (firstNum = 1; firstNum <=12; firstNum ++){
answerNum = firstNum * secondNum;
displayTable.append(firstNum + " x " + secondNum + " = " + answerNum +"\n");
}
public static void main(String[] args)
I get this message
Quote: c:\Java>javac MyTables.java
MyTables.java:18: error: illegal start of expression
public static void main(String[] args)
^
1 error
If I put it at the end
loginButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
displayTable.setText("Button Pressed");
}
void getSecond(int secondNum){
displayTable.setText("\n");
int firstNum, answerNum;
for (firstNum = 1; firstNum <=12; firstNum ++){
answerNum = firstNum * secondNum;
displayTable.append(firstNum + " x " + secondNum + " = " + answerNum +"\n");
}
});
}
}
I get 3 error messages
Quote: c:\Java>javac MyTables.java
MyTables.java:69: error: illegal start of type
});
^
MyTables.java:70: error: ')' expected
}
^
MyTables.java:71: error: reached end of file while parsing
}
^
3 errors
|
|
|
|
|
Look at the code, it has two opening brace characters ({ ), one for the method, and one for the for loop. But it only has one closing brace, for the for loop. So you just need to add the one that closes the method block.
|
|
|
|
|
That's done it, although I am annoyed as I knew the curly bracket was missing but when I added it I got different errors.
After your reply I removed the method placed it in a new page and added the bracket so that it had 2 opening and 2 closing, placed it back in my project and it worked.
I do remember that every opening ( or { had to have a matching one closing.
Can now carry on with the project.
Thank you
|
|
|
|
|
The thing to remember is when you start any code block is to add the braces first. You can then fill in the actual code afterwards:
int someMethod(int parameter)
{
}
int someOtherMethod(int parameter)
{
while (some expression)
{
}
if (some boolean expression
{
}
}
But always remember, making mistakes is how we all learned ...
|
|
|
|
|
Thanks Richard that is a good piece of advice that I will remember.
But always remember, making mistakes is how we all learned ...
And this gives me some encouragement as it seems I'm making quite a few.
|
|
|
|
|
OK, so I'm back to using notepad++ in a panel I have a TextBox, Label and Buton all of which show but I have a TextArea that doesn't show, tried various ways but nothing has worked.
private static void placeComponents(JPanel panel)
{
panel.setLayout(null);
JTextArea displayTable = new JTextArea("Text Area");
displayTable.setEditable(false);
displayTable.setColumns(20);
displayTable.setFont(new Font("Times New Roman", 1, 14));
displayTable.setLineWrap(true);
displayTable.setRows(14);
displayTable.setWrapStyleWord(true);
displayTable.setBorder(BorderFactory.createLineBorder(Color.GREEN));
displayTable.setMaximumSize(new java.awt.Dimension(5, 22));
panel.add(displayTable);
|
|
|
|
|
|
I am using an example code that I found line JavaPoint which works and does what I would want it to do, so I thought I would use it in my Netbeans project.
It took a while to figure out what did what as there were no comments and obscure naming eg buttons name is 1.
I now have it working and have renamed things there is one problem I can't figure and it's a call to a method called set()
The program starts with a question and 4 possible radiobutton answers you select an answer then click button next which shows question 2.
For some reason It doesn't show question 1 when the form first loads. Tried various things but none worked for me.
The Next Button Code:
private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {
if(check())
count=count+1;
current++;
set();
if(current==9)
{
JOptionPane.showMessageDialog(this,"Correct Answers = "+count);
btnNext.setEnabled(false);
}
}
First section of the set() Method code
void set()
{
rbSetQuestion.setSelected(true);
if(current==0)
{
lblQuestions.setText("Question1: Which one among these is not a datatype");
rbChoice_1.setText("int");rbChoice_2.setText("Float");rbChoice_3.setText("boolean");rbChoice_4.setText("char");
}
if(current==1)
{
lblQuestions.setText("Question2: Which class is available to all the class automatically");
rbChoice_1.setText("Swing");rbChoice_2.setText("Applet");rbChoice_3.setText("Object");rbChoice_4.setText("ActionEvent");
}
|
|
|
|
|
Where is current initialised? Also, it would be better to put the text of your questions and answers in some sort of List, and pass the current variable to the set method. All set then needs to do is select the values of question and answers based on the List index.
|
|
|
|
|
Hi Richard,
Thanks for taking the time to look and answer.
Here is the start of the code. Once I get it to work I will then read up on list. Just wanted to solve why its not working.When running the example in cmd does.
public PracticeTest() {
initComponents();
}
@SuppressWarnings("unchecked")
int count=0,current=0;
set();
{
counter = 0
set()
}
|
|
|
|
|
The last five lines of that do not make any sense.
The first line is (or should be) a method declaration (but it is not), hence the error message. A call to a method cannot exist at class level, it must be inside an executable block.
The next four define a method that has no name, so can never be executed.
|
|
|
|
|
That's done it
Not sure about the first five lines as I'm using Netbeans and it puts those lines in.
But this is the that solved it.
Quote: A call to a method cannot exist at class level, it must be inside an executable block.
So all I needed was,
private void rbSetQuestionFocusGained(java.awt.event.FocusEvent evt) {
set();
}
As rbSetQuestion is selected at the start, the set() method is executed.
Now to have a look at lists.
|
|
|
|
|
MallardsReach wrote: Not sure about the first five lines as I'm using Netbeans Which is a good reason to abandon Netbeans and use a simple editor*. That way you know that all the code is what you have written, rather than something the framework thinks you need. Once you have really mastered Java then go back to Netbeans, but until then it is only getting in your way. I speak from experience of trying to learn Visual C++ and MFC at the same time.
*notepad, notepad++, eclipse, or even Visual Studio Code, will all do the job. There are probably also many others that I have never looked at.
|
|
|
|
|
Once again Richard, Thanks for the advice.
I guess like most beginners I couldn't understand how writing code in a text editor and running it in cmd made a visual program. I did start with notepad ++ and compiled and run some programs but the result only showed itself in the cmd window, what use is that? then after looking around and gaining a little more understanding, I looked at examples where the code was written, compiled and run.
Lo and behold a window popped up with a button on it!
I think Netbeans makes you feel like you know but as this thread has pointed out, it's just an illusion.
So back to notepad ++ and back to a step at a time. Will also look at the other editors available.
|
|
|
|
|
I've looked at various articles and examples none of which are working for me.
I have a jPanel inside of which is a jlabel.
In the MouseDragged event I have the following code.
int x=evt.getX();
int y=evt.getY();
jLabel3.setLocation(x, y);
jLabel3.repaint();
The Label moves but it flickers and sometimes show a copy of it self.
Any help in the right direction would be appreciated.
|
|
|
|
|
The problem is that dragging produces many events so your code will be repeatedly moving the label a couple of points, and repainting it. You need to limit the number of repaints somehow. It's a long time since I wrote any GUI code in Java, so I am not sure exactly how this is best achieved. I expect Google will find some samples that help, or even some of the example code found in The Java™ Tutorials[^].
|
|
|
|