Any java programmers here? I'm getting some strange behavior in Netbeans. Here is the code that executes if any key is pressed (evt is the event):
jButton7.setBackground(c);
if (evt.getKeyCode() == 192)
{
System.out.print("I'm here");
jButton4.setBackground(c);
}
If I press any key, jButton7 turns blue. If I press the key with keyCode 192, "I'm here" is displayed on the monitor but jButton4 DOESN'T change color.
It's like the conditional statement is making setBackground() unable to execute.
Java has nothing to do with science/mathematics. It's not even used in scientific computation since it's so slow compared to C/C++. GTFO and never come back.
>>8427599
Place a what where to get into which function to see if who does what?
Please explain. I'm a beginner to Java.
>>8427593
stupid question, but is the button already blue? what colour are you expecting it to change to?
are you 100% sure jButton4 is referencing the thing you think it is?
it's hard to know any more without any extra context.
>>8427619
Let me give you some more information.
-All the buttons are non-blue, and they're supposed to turn blue when pressed.
-This works:
>switch (evt.getKeyChar())
>{
>case '~': jButton4.setBackground(c);
>break;
>}
~ is keyCode 192, btw.
The problem is that I want the buttons to change color based on the key pressed, not the character.
Also, I just tried this:
if (evt.getKeyChar() == '~')
{
System.out.print("I'm here");
jButton4.setBackground(c);
}
and it works.
>>8427593
>jButton7
>192
>jButton4
Learn to choose meaningful variable names.
>>8427593
>using netbeans
>not killing youself
>>8427608
Fuck off, java has nothing to do with /sci/
>>8428160
>fourth button
>jButton4 isn't meaningful
>>8427593
>java
>you actually have to memorize all those messy named functions
Jesus.
>>8429083
>>you actually have to memorize all those messy named functions
>Wat is documentation
I program Swing all the time. I don't know a lot of the methods by heart. A lot of the IDE's, like Eclipse, have build in documentation and even suggests the methods you're trying to find.
>>8427669
>The problem is that I want the buttons to change color based on the key pressed, not the character.
Writing a piano/synth?
Also, haven't tested it (you should post a mcve). But I always refresh the widow when I do any changes to the GUI. You can put next method in any component and call it after a color change happens:
public void refreshWindow(){
JFrame frame = (JFrame) getRootPane().getParent();
frame.revalidate();
frame.repaint();
}
>>8429083
>>you actually have to memorize all those messy named functions
You really don't. They are quite obvious. Consider for example, when you want to create beans that create beans that create only singletons. Well it would logically follow that if you just read those words in order, you would arrive at the AbstractSingletonProxyFactoryBean. It's a lot like phonetic spelling.
>>8429075
good description: "subject field"
bad description: "third text field"
As a rule of thumb, if your variable names end with numbers, it should either be refactored into real names or an array/list.