Buttons Background Dynamically changed - java

I have to change the background of a JButton on some action.
I have two classes. The first one is an Action subclass and the second is a GUI class.
In the GUI class I have put buttons inside panels just like a matrix.
On some action, which is captured in the Action class, I want to inform the GUI class to change the background of a JButton.
How can I achieve this?

Say you want to change your button background to blue.
You do this:
JButton button = new JButton("Click to Change Background");
button.setBackground(Color.BLUE);

Related

How to stay in the same frame with Java swing

I am trying to make a Quiz game and my idea is that i have a main frame class with buttons and a label like this
So when I click the Start button i want to stay in the same frame just delete all these buttons and label and replace them with the question and answers to it. How can I accomplish this? Read something about repaint() method and removeAll() but what is the best practice. I come up with this(dunno is it the right thing to do in the situation) but call removeAll() in the actionListener of the start button and create the new frame in this listener? Or can I make another class e.g called StartFrame and then in the actionListener of the start button call removeAll() and then MainFrame.this = new StartFrame() assuming that my class holding the initial state of the game like in the pic is called MainFrame

How to access JFrame components from a JPanel class?

I'm working on a GUI assignment and I encountered a problem that I cannot figure out. I have a JFrame with multiple JPanels, one of those JPanels contains a CardLayout with multiple JPanels. Since we are more people working on the project we decided to make a separate class (that extends JPanel) for each panel that's gonna be inside the CardLayout.
The problem is accessing components of the JFrame from the JPanel classes.
To give you an example, I have a JLabel somewhere in the JFrame that serves as a status bar and I want to change the text of the status bar when a button is pressed on the SaleMain panel (a class SaleMain that extends JPanel, contained in the CardLayout).
Another example, inside another panel EditCustomer (also a JPanel class, included in the CardLayout), I'd like to have a button with an action listener that will change the current panel (the one containing the button) to a different panel from the CardLayout.
Hope I made it as clear as possible, thank you guys in advance for helping me :)
The model / view / controller pattern (MVC) is useful for creating a GUI. By separating your model from your view, you can concentrate on one part of your GUI at a time.
You create a model for your GUI that contains the information you want to present on your GUI.
I have a JLabel somewhere in the JFrame that serves as a status bar and I want to change the text of the status bar when a button is pressed on the SaleMain panel
Put the text in your model, and in the action listener for the button, you have the text put in the status bar.
I'd like to have a button with an action listener that will change the current panel (the one containing the button) to a different panel from the CardLayout.
Then do so. The action listener is a controller that can change the view.
Take a look at my article, Dice Game, to see how a Java Swing application implements the MVC pattern and JPanel switching.

Changing setText from multiple classes in java

I'm busy with an application which runs in a JFrame (using BorderLayout), and has:
A status bar at the bottom
Fixed Buttons on the left
Menu on top (which change relating to specific function),
Buttons on the right (which change relating to specific function).
I'm setting it up that for every button on the left, a separate class file will be created for its functions and procedures. At this stage it's about 8 extra classes.
How to go about changing the values on the buttons and menu for each class, by that specific class?
If you're creating a separate class for each button, I would make sure that the class extends JButton. By doing this, to change the values on the buttons, you still have access to all the normal JButton methods like setText();
So hopefully your custom button classes would look something like this...
public class MyButton1 extends JButton {
public MyButton1(String label){
super(label);
}
// your other methods go here
}
To create the button you would do this, which would set the button label...
MyButton1 button1 = new MyButton1("Hello");
And you can still call JButton methods if you want to change the label later, like this...
button1.setText("Goodbye");

Displaying a UI after clicking a button

I want to create a new UI within another UI which should be displayed on clicking a button using Java Swing. What should I do?
In my program on clicking next button I need to display a new UI. How can I link the new ui to the already existing one?
Do you mean wizard like? You can place all your UIs in CardLayout and switch them. Or remove old and add new calling
container.revalidate();
container.repaint();
One approach is to make the second UI not visible from the start.
setVisible(false);
and when you click the button make it visible:
setVisible(true);

Swing JButton don't change background color onclick

I want the button on click it never change the background color, by default the color will change to light blue grey color.
So this is my code
JButton b = new JButton();
b.setBackground(SystemColor.control);
I want the button when on click it will never change the background color.
The painting of the button depends on LAF. You can do one of the following:
Define your own UI delegate and assign it to the button.
Disadvantages: complex, breaks LAF.
Extend JButton and implement paintComponent().
Disadvantages: You will need to paint button's label yourself.
Remove the button's background painting altogether by invoking setContentAreaFilled(false) and then add the button in the opaque panel with the needed background color.
Advantages: button is unchanged, LAF isn't broken.
Disadvantages: you need to create a panel for each button, and adjust it to fit the button exactly, which can be quite difficult, depending on the layout.
You could modify the colors that is used for the background and foreground. How you do it depends on what Look And Feel you are using.
If you are using Nimbus, there is a list of colors here, and here is an article about how you change the colors. And here is another article that can be helpful.

Categories