Detecting current color in window - java

I am doing a beginner project and I have had a little issue with a radio button. This button's job is to change the theme of the window from light to dark and vice versa.
I'm not too sure on how to ask for Java to detect the value for the Color.decode() method. I want it to check if the current color is either "#21252B" or "#FFFFFF"
I expect it to look kind of like:
if(*however you are supposed to do it*.equals("#21252B")) {
frame.getContentPane().setBackground(Color.decode("#FFFFFF"));
darkMode.setBackground(Color.decode("#FFFFFF"));
} else {
frame.getContentPane().setBackground(Color.decode("#21252B"));
darkMode.setBackground(Color.decode("#21252B"));
}
What can I do?

I figured out what I had to do. Thanks to #AndrewThompson for the suggestion. If anyone needs an answer to a similar problem, here it is. Make
private boolean isDark = true //or false if you want from the get go.
Then, whenever you do your button do the following code
public void actionPerformed(ActionEvent arg0) {
if(isDark == true) {
lightTheme();
isDark = false;
} else {
darkTheme();
isDark = true;
}
after this you should be good to go.

Related

Is there any way where a javafx button can listen from different event handlers?

Am new to javafx. I wanted to write a simple javafx program that when clicking a button for the first time, the color of a rectangle should change to something else, e.g green, and when clicking it for the second time, the color should change back to the original color.
But the button is only allowing a single click only?
Help if there is any solution on this.
Here is a code.
You can try using simple boolean value to check if color has been changed before:
boolean flag = false;
#FXML
private void onButtonClick(){
if (flag){
//change to e.g. red
} else {
//change to e.g. green
}
flag = !flag;
}
This code is simple and if you want only to change between two colors it will be enough :)

how to change background color back and forth eg red and green when clicked using only 1 jbutton

so what I'm trying to do is creating a button that when clicks, turns the background color to be green. once its clicked on again, it turns red. And repeat basically. So im looking for some help that show me how I can do this.
this is what I've done so far
class colorText implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Go")) {
panel.setBackground(Color.GREEN);
}
}
}
I tried doing it by doing this but I get an error and was wondering either how to get around it or a new a way of doing it
if (e.getActionCommand() == Color.GREEN) {
panel.setBackground(Color.RED);
}
Your attempted solution doesn't work because (as we can see from the first example), e.getActionCommand() returns a String - and likely will always return "Go" when they click on your button.
What you need is some way of determining the state; telling the difference between "I should make the background red now" and "I should make the background green now". There are two ways of doing this that come to mind:
1) Check the current background colour
If the API lets you query the background colour and compare against a constant, you could check it like so:
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Go")) {
if (panel.getBackground() == Color.RED) {
panel.setBackground(Color.GREEN);
} else { // could check for GREEN background here
panel.setBackground(Color.RED)
}
}
}
Possible downsides to this approach:
You might not be able to (reliably) read back the colour that you set. Either it's not exposed in the panel's API, or the value you get back does not equate to the Color constants for some reason (e.g. the read value has an alpha channel while the constants do not)
This only works if this method is the only way that the background gets modified. If any other code were to modify the background, you'd not have an accurate record of what your previous button press did. This is potentially a big issue as it means this code doesn't play nicely with other functionality.
2) Store a flag and flip it each time
This approach involves storing an explicit field that lets you know what to set the background to next. This could just be a boolean, like so:
private boolean greenIsNext = true
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Go")) {
Color nextColour = greenIsNext ? Color.GREEN : Color.RED
panel.setBackground(nextColour);
// Flip the flag
greenIsNext = !greenIsNext
}
}
This is likely the cleaner way to handle this behaviour, as pressing the button will alternate between setting the background green and red regardless of what else is going on with the page.

Disable Button on Activity, after user interacts with Buttons

Im having trouble disabling the Buttons for a particular question in my mQuestionsBank array. I created a mQuestionsAnswered boolean array with the size of the mQuestionsBank array to keep track of the questions that have been answered. Now, when the user interacts with either the "True" or "False" button, mQuestionsAnswered[mCurrentIndex] gets set to true, therefore disabling both of the buttons whether if they are right or wrong. Heres my code
Method to Enable Buttons image
Method to Check Answer image
True and False Button onClickListeners image
This is the code from your first picture:
private void buttonEnabler(){
if (...) {
...
} else
mTrueButton.setEnabled(true);
mFalseButton.setEnabled(true);
}
You're missing brackets on the else case. That means that this code "really" looks like this:
private void buttonEnabler(){
if (...) {
...
} else
mTrueButton.setEnabled(true);
}
mFalseButton.setEnabled(true);
}
In other words, mFalseButton will always be enabled, even when you don't want it to be. To fix it, add the brackets surrounding the else lines:
private void buttonEnabler(){
if (...) {
...
} else {
mTrueButton.setEnabled(true);
mFalseButton.setEnabled(true);
}
}
In your method buttonEnable, there's the if else error
Use else with {} brackets always, unless the statements to be executed is just one as illustrated below...
if (true)
say 'hello
else
be quiet
Or
if(true) {
say 'hello'
say 'how may I help you're
} else {
say statement 3
say statement 4
}

Methods for detecting use of a button in java

I was wondering if there was a method for JButtons that is, and I'm just guessing here, essentially a boolean that would be assigned a true or false value depending on whether or not a button is clicked. I understand that there are actionListeners and keyListeners and MouseListeners and a plethora of listeners, but I am searching for a method that would do something like this:
public boolean ButtonClickDetector (just pretend it's real)
{
if(JButton.isClicked())
{
return true;
}
else
{
return false;
}
I need this so that I can increase an integer only when a button is clicked. I have thought about just putting integer++; into the actionPerformed but I feel if I had a boolean, the code would function better, be less prone to error, and perhaps a bit more efficient. Please note, I am a high schooler and do not have oodles and oodles of coding experience so PLEASE dumb down your answers. When answering PLEASE PROVIDE AN EXAMPLE AND EXPLANTION OF WHAT YOU ARE SHOWING ME. Accompanying an example and explantion with documentation would be wonderful. Thanks.
No, there are no methods in the JButton class that allows you to perform such a check. You must use some kind of listener. Check the JavaDoc for JButton to see what methods are available.
http://docs.oracle.com/javase/7/docs/api/javax/swing/JButton.html
You can use e.getSource() to see where the event came from.
JButton button = new JButton();
int i = 0;
public void actionPerformed(ActionEvent e) {
if(e.getSource == button) {
i++;
}
}
This will allow you to only increase if that specific button was pressed.

cant change key icon on softKeyboard

i am trying to learn android keyboard api.
Using the softKeyboard example, i am trying to change the key icon, i can change everything, but the icon never change.
i am doing it in the LatinKeyboardView, at the onLongPress method, using this line:
copia.icon = getResources().getDrawable(R.drawable.tecladir);
but the icon doesn't change.
Even after using
this.invalidateAllKeys();
to force redraw of the keys, the icon is still unchanged.
Complete code of onLongPress as following:
#Override
protected boolean onLongPress(Key key) {
key.icon = getResources().getDrawable(R.drawable.tecladir);
//tecladir is one image i have
key.text = "batata";
key.label = "batata";
this.invalidateAllKeys();
//default code of method
if (key.codes[0] == Keyboard.KEYCODE_CANCEL) {
getOnKeyboardActionListener().onKey(KEYCODE_OPTIONS, null);
return true;
} else {
return super.onLongPress(key);
}
}
Am i missing something?
Ok, found the solution.
before assign a new icon, the label must be null, otherwise the icon doesn't change, the correct way is:
key.label = null;
key.icon = getResources().getDrawable(R.drawable.tecladir);

Categories

Resources