So I'm writing a program in which I would eventually like a JButton to change color when pressed. So far, here is my dilemma:
-I cannot get the JButton I currently have set up to show the background color, despite toggling the true/false of setOpaque() and setContentAreaFilled(). I would like to be able to do this before even attempting to add an ActionListener.
-I would like to use something similar to JToggleButton() to change the color, however I would like to do this with just a background instead of an icon.
I am using a mac, and I'm wondering if it has something to do with the default button settings, but I don't know how to change/override these.
I would post a screenshot, but unfortunately my reputation isn't high enough yet.
Here is an example of what I am getting:
import java.awt.*;
import javax.swing.*;
public class Example extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame main = new JFrame("Example");
JPanel content = new JPanel();
JButton button = new JButton("Press");
button.setBackground(Color.orange);
button.setContentAreaFilled(false);
button.setOpaque(true);
content.add(button);
main.setContentPane(content);
main.setVisible(true);
main.setSize(40,60);
main.setLocation(500, 200);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
For those (maybe on a windows or linux platform) who can't see what I'm talking about, the button appears and the layout behind the button (not the border or the JPanel, just a small rectangle around the button) is colored to what was specified.
Thank you in advance for your time.
Also, I know that inside the ActionListener the getSource() method will return the text associated with the button. Is there any way to use something similar to change the color of the button from inside the ActionListener or is this idea just a redundancy for something that can be done in a much simpler fashion.
Much appreciated.
i think you want play something around with the look and feel.
try
button.setUI(new MetalButtonUI());
or try some other background color supported UI for the button.
I would eventually like a JButton to change color when pressed
here are two different ways (note JButton has an arrays of colors in UIManager)
override events from ButtonModel (ChangeListener or the same methods are implemented e.g. isPressed, isArmed in JButtons API), accelators are valid for mouse and KeyEvens (selection or focusInWindow)
override BasicsButtonUI (for real project)
I am using a mac, and I'm wondering if it has something to do with the
default button settings, but I don't know how to change/override
these.
depends of Look and Feel that your Java uses (Quaqua - default on OSX or standards by Oracle Metal, Nimbus ...)
I think that button.setOpaque(true); is unnecessary.
Remove it, or change it to: button.setOpaque(false); and see what happened
Related
Currently the code only hides the JLabel. I'm not sure why it's not making it visible when I click the button again. Hopefully this is an easy fix
contentPane.add(btnSwap);
btnHide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
lblHello.setVisible(false);
}
});
contentPane.add(btnHide);
btnHide.setBounds(185, 199, 89, 23);
lblHello.setVisible(true);
}
I'm not sure why it's not making it visible when I click the button again.
Why should it, since all the ActionListener does (the code that's called on button press) is to continually set the label invisible?
A solution is to simply toggle its visibility:
lblHello.setVisible(!lblHello.isVisible());
Note that to be safe, it's best to revalidate and repaint the container after making such changes, so:
btnHide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
lblHello.setVisible(!lblHello.isVisible());
revalidate();
repaint();
}
});
as this will rid the GUI of "dirty" pixels that can occur from adding and especially from removing visible components.
A word on this:
btnHide.setBounds(185, 199, 89, 23);
This suggests that you're using null layouts with setBounds(...). While this often seems to newbie Swing coders the best way to create complex GUI's, it will come back to haunt them later, since this will mean that the GUI will look OK on one platform and one platform only, and that if later you want to enhance or improve the GUI, it can only be done with much difficulty and risk of bugs. Much better is to learn and use the layout managers.
Another recommendation:
If your desire is to change the appearance of the GUI on button press, then also have a look at the CardLayout (please check the CardLayout Tutorial) as this can be a way to cleanly and easily swap views
And a better recommendation:
Since a JLabel only shows its text or its icon or both, the best way to make it "invisible" is to remove its text and its icon, as noted by Andrew Thompson below:
// get rid of its text
lblHello.setText("");
// and if needed
lblHello.setIcon(null);
This won't work for text components such as JTextFields and JTextAreas or other components that have more "heft" than a JLabel including pretty much all other user-interaction components.
Try :
btnChangeLabelVisibilityButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
lblHello.setVisible(!lblHello.isVisible());
}
});
I'm trying to set a variable to be a new JPanel and then add it once a button is pressed, but it is not working and I don't know why.
code:
private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
remove(scriptPanel);
scriptPanel = new GemPanel();
add(scriptPanel);
validate();
repaint();
pack();
}
GemPanel is just a JPanel class I made. When I press the next button, it re-sizes the frame to be as small as possible and nothing actually happens. If I re-size it to normal, the original scriptPanel is still there.
What gives?
Instead of trying to remove and add entire panels, a better, less problem prone approach would be to use a CardLayout that will allow to swap views. You can see more at How to use Cardlayout
Also, by the looks of your method signature, it seems you're using the Netbeans builder too. You may also want to take a look at How to Use CardLayout with Netbeans Gui Builder
First of all... I'd like to say that I am not interested in using the card layout for this... Unless it's necessary (which means that not using the card layout would be result in unnecessary workarounds and complex code). This is for learning purposes after all and I will look into the card layout very soon enough anyway...
Okay so my question is pretty basic GUI layout I guess. My code is not working and this whole layouting confuses me quite a lot...
I'm having trouble how to make the transition between JPanels like this:
I have one window. I press a button, the old window is replaced by another window. I press a button and that window will be replaced by another window.
I'd like to add that I am skipping a lot of irrelevant code in my example below...
I start off with a JFrame:
public class StartWindow extends JFrame{
public StartWindow(){
//Add JButton & ActionListener
//When the button is pressed:
add(new NextWindow());
}
public static void main(String [] args){
new StartWindow();
}
}
then I have several JPanels...
public class NextWindow extends JPanel{
public NextWindow(){
//Add a JButton & ActionListener
//When the button is pressed:
add(new NextWindow2());
remove(this);
//This does not work. Nothing happens.
}
}
public class NextWindow2 extends JPanel{ // Stuff and so on}
So, I'd like to know a proper way to handle this situation!
You are adding a panel to itself. You need to remove the panel from the JFrame, add the new one to it, and call revalidate() on the JFrame.
I'm making a calculator using Swing. So far I have created a GUI that consists of a JFrame with BorderLayout, and in its center I put a JPanel that has a JLabel (representing the screen of the calculator) and some JButtons (representing the keys).
I want the calculator to be able to receive input directly from the keyboard, so I included the method addKeyListener in a class that extends JFrame and I put as an argument a reference to an object of a class that implements KeyListener.
When I run the application it accepts keyboard input until I click on one of the JButtons. After that using the keyboard doesn't work anymore.
I suspect the problem is about focus, but it is not a problem that I could fix by clicking anywhere on the application. I added the following code:
setFocusable(true);
to the JFrame but it did not help. I have read that using Key Bindings maybe a better option than using a KeyListener, but I am not really sure about this approach.
Swing components are lightweight and use KeyBindings, where KeyListeners are for AWT components which are heavyweight. and known to have focus issues when mixed with Swing components. Thus I suggest changing to KeyBindings (but I see you have heard of them). You would use them something like:
final JButton b=..;
//method to add keybindings to a JComponent ie JButton,JPanel,JTextField etc
private void addKeyBindings(JComponent jc) {
//not the getInputMap(..) call it could also be JComponent.WHEN_FOCUSED etc
jc.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("D"), "D pressed");
jc.getActionMap().put("D pressed", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent ae) {
//do something when d is pressed
b.doClick();
}
});
}
For general knowledge a very hacky solution would be calling requestFocusInWindow() on the component to which the listeners are attached whenever focus is lost (like after button click etc)
It's another hacky approach, but you could alter the properties of the JButton when you create it so that it can't take the focus in the first place i.e:
myJbutton.setFocusable(false);
This worked for me.
I have a JPanel inside a JFrame. I have registered a KeyListener, based on which I want to update the JPanel. The problem I am having is that I cannot get the focus on the JPanel and therefore my KeyListener won't work. I already know that the KeyListener is functional because I registered it with the JFrame and it worked fine. My code goes something like this at the moment:
myFrame.setFocusable(false);
myPanel.setFocusable(true);
myPanel.addKeyListener(myKL);
myFrame.add(myPanel);
Has anyone encountered a problem like this before? Is there something I am missing in regards to this?
P.S.: I do not have any components inside the JPanel I just draw an Image on the background, so I need the focus to be on the JPanel itself and not on something inside it.
Although you're indicating that the panel can be focusable, the panel isn't asking for focus. Try using myPanel.requestFocus();.
Use setFocusable(true) and then requestFocusInWindow(). But the latter must be done after the window containing the panel is made visible, for which you will likely need to register a window listener and do the requestFocusInWindow() in the window activated handler code.
Note: Specifically after the window is visible, not just after calling setVisible(true).
I sometimes face a similar problem. I've noticed that in some cases it is better to make or request focus on a specific control within the panel that is within the frame (e.g., the input box to which you want keyboard input to go), rather than request focus for the pane itself.
Try
panel.setFocusable(true);
panel.setRequestFocusEnabled(true);
// some code here
panel.grabFocus();
Try something like this:
myFrame.addFocusListener(new FocusAdapter() {
/**
* {#inheritDoc}
*/
#Override
public void focusGained(FocusEvent aE) {
myPanel.requestFocusInWindow();
}
});