Java Change Button Background Color [duplicate] - java

This question already has an answer here:
Java Swing button colors [duplicate]
(1 answer)
Closed 7 years ago.
I am using netbeans to create a desktop application, and I am trying to change the background color of a JButton, btn1 using the code
btn1.setBackground(Color.Red)
But this does not work. Instead of changing the fill color of the button, only the border color is changed. How can I change the background fill?
Thanks

See this related question and answer.
In particular, you can use setForeground() to change the color of a button's text, but the corresponding setBackground() doesn't read well on some platforms. Using a Border is one alternative; a colored panel, shown below, is another.
You can also try calling setOpaque(true) on your button.

Related

how to set a listener for the selected text in TextView in java? [duplicate]

This question already has answers here:
How to set up a listener on the selected text in TextView
(3 answers)
Closed 1 year ago.
i want a Help just which way i follow to get a listener for the selected text in TextView,
what i want is when the user select a specified text, a button of options will appear.
i hope you help me
Have you tried ActionListener? It will be called when user interacts or performs any action on that particular component. You can get the selected content by component.getSelectedText() or similar method. Do not write separate listeners for mouse and keyboard operations as it might interfere with one another and cause problems.

java swing contentPane.remove remove the panels but graphics stay on screen [duplicate]

This question already has answers here:
Unable to remove JLabel from JPanel after adding programmatically
(2 answers)
Closed 3 years ago.
so I have this perfectly normal code
the thing is that these elemnts get removed from the panel (logically) but the graphics stay for example the button stop interacting it just freez there .
and when I pass a jlabel above (I have drag and drop jlabel) these elements they get wiped.
contentPane.remove(Quitter);
contentPane.remove( PanelInit);
Anyone here have an idea how to fix it ? Thanks in advance .
using the repaint function fixed the whole thing .
hope this help anyone in the future.
contentPane.repaint();

How to write a code for default close button in JFrame? [duplicate]

This question already has answers here:
setDefaultCloseOperation to show a JFrame instead
(3 answers)
Closed 8 years ago.
I want to write a code to default close button in JFrame. I just want to ask whether "to save the content or not?" using another JDialog or JOptionPane when click on close button.
Where do I have to write the code? As default close button is not in design of JFrame, and no such event found for JFrame window too, how can I add event listener to that button?
Check out Closing an Application for a couple of solutions:
using a WindowListener and overriding the windowClosing(...) event to display your option pane.
using a simple API so you only need to provide a message or write an ActionListener for more complicated processing.

How do I scale a .JPEG to fit inside a button that rests in a GridView? [duplicate]

This question already has answers here:
resize jpeg image in java
(3 answers)
Closed 9 years ago.
I have a number of Buttons that are dynamically added to a GridView using an Adapter. These currently fit inside the GridView correctly, but...
I would like to add an image inside each button. I have banana.jpg stored in my drawable folder and I use the following code to set it inside the button:
button.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.banana));
However, I have two problems:
The buttons seem to stretch to the size of the image.
The image doesn't actually appear.
Is there some way to add an image to a button such that the current size of the button will scale the image down so that it fits? (Or, is it possible to calculate the size I will need the image to be so I can resize it manually?)
You have to use ImageButton instead of Button,
If you want to use Button may be This answer can help you.

Netbeans GUI builder - to show a panel over another panel [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
How can I stack/overlay jPanels in Java?
I want to create a panel over another so that after clicking next button from first panel second panel gets open above it in same frame.
How can I do this using Netbeans GUI builder?
What you really want is a CardLayout , Have a look at this : How to Use CardLayout.
If you are using the Matisse feature of Netbeans, which uses GroupLayout, you may have to write some code to overcome your problem.
While creating your GUI keep both the panels on top of each other, keep the default visibility of your 2nd panel as false. Then when the button is clicked, set the 1st panel's visibility to false and change the 2nd panel's visibility to true.

Categories

Resources