I was wondering if anybody knew if it was possible to change the background color on the buttons inside a JOptionPane. I know how to change the entire JOptionPane background using a UIManager, but know what I want is to set the individual okButton, cancelButton, and so on within the JOptionPane to separate individual colors. If I can do this, how would I do this?
Thanks for the help.
There is no direct way to do this.
But if you really want to give it a try, then you will need to read the JOptionPane API which gives code that shows you how to manually create and display a JOptionPane without using the showXXX methods.
Using this approach you now have access to the actuall JDialog. Then you can use Darryl's SwingUtils to access the individual buttons and then set the background.
The code would be something like:
JButton ok = SwingUtils.getDescendantOfType(JButton.class, dialog, "Text", "Ok");
ok.setBackground(...);
Simplest would be to just create your own JDialog and set the button characteristics to your heart's content.
You can use your own buttons with yours characteristics in showOptionDialog. I guess it is not the best solution, but it simply works.
JButton button = new JButton("OK");
button.setBackground(Color.BLACK);
button.setForeground(Color.WHITE);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent actionEvent) {
JOptionPane.getRootFrame().dispose();
}
});
JButton[] buttons = { button };
OptionPane.showOptionDialog(null, "Test Message", "Dialog", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, new ImageIcon(), buttons, buttons[0]);
Add the the line of code below before your JOptionPane
UIManager.put("Button.background", Color.white);
JOptionPane.showMessageDialog(null, "Project, Please");
Related
The problem I am running into is this: I have a grid of buttons in a JPanel, these buttons are supposed to change to an image of a queen when I click them. The code looks like this:
private Component createButtonBlack() {
final JButton button = new BoardButton();
final ImageIcon queen = new ImageIcon("/images/queen.png");
button.setBackground(Color.BLACK);
button.setPreferredSize(new Dimension(40, 40));
class QueenClick implements ActionListener {
public void actionPerformed(ActionEvent event) {
button.setIcon(queen);
button.repaint();
}
} // end QueenClick
ActionListener queenClicker = new QueenClick();
button.addActionListener(queenClicker);
return button;
} // end createButtonBlack
The problem (image not appearing) occurs on both the methods for creating black and white buttons but the methods are the same except for the color. Ideally I would like to be able to un-click the buttons and the image disappears but I do not know how to do that.
I am having difficulty with other parts of my 8queens GUI based problem so if you have any suggestions let me know!
Also if you need more code I will certainly supply it. Thank you.
State the exact problem when asking a question.
These buttons are supposed to change to an image of a queen when I click them.
So I'm guessing the icon doesn't change?
Did you:
Verify the ActionListener code is executed?
Verify the Icon was read properly?
You can easily add a System.out.println(...) to verify both of the above.
final ImageIcon queen = new ImageIcon("/images/queen.png");
I'm guessing the problem is the leading "/" in the path. The "/" tells the file system to look at the root of the drive.
if you have any suggestions let me know!
There is no need to create two methods. You can just do:
Component button = createButton();
button.setBackground( Color.BLACK );
There is no need to create individual ActionListeners. You can create a single generic listener with code like:
ActionListener queenClicker = new ActionListener()
{
#Override
public void actionPerformed(Action Event e)
{
JButton button = (JButton)e.getSource();
button.setIcon( queen );
//button.repaint(); // not needed the setIcon method will do the repaint()
}
}
I am developing a Java Swing App, and I want to use JRadioButton objects to show state. I don't want the user to have the ability to select them. If I use the button's .setEnabled(false) method, the radio button is greyed out.
I don't want the Radio Button to grey out. Is there a way to override this?
Well You Could Do Something Like:
boolean rButtonEnabled = false;
JRadioButton rButton = new JRadioButton();
rButton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
rdbtnNewRadioButton.setSelected(rButtonEnabled);
}
});
It's not the prettyist solution, but I hope it helps.
To store the new value perhaps you can create a new class extending JRadioButton.
While this does not use the .setEnabled(), it is effectively the same
I have an application that is developed in Java swing and the NetBens 7 IDE
Steps:
I want to use a Jbutton to perform two different functions depending on the user mode.
for example I want to label a single button with the following text "New Record" and
"Exit New Record"
The default text is the "New Record". This will enable the user enter new record.
Whiles in the new record mode, the text on the jButton changes to "Exit New Record".
To exit the new record mode the user clicks on the same button to exit.
This will then change the text on button to the default enter "Enter New Record"
Is there any suggestion on how to do this with the netbeans IDE or do I manually
override a method?
Implement Action Listener on JButton (code not tested, just for your hint):
public class MyButton extends JButton implements ActionListener{
boolean pressed = false;
public MyButton(String name){
super(name);
this.addActionListener(this);
}
#Override
public void actionPerformed(ActionEvent e){
if(pressed){
pressed = !pressed;
_change_text_on_button_
_do_job_
}
}
Than use customized MyButton.
As shown in examples here and here, a button's text can be changed in its ActionListener. The NetBeans GUI editor generates the code to invoke the ActionListener, but it lets you edit the code in the method that is called. The method name will be something like nameActionPerformed().
See also How to Use Buttons, Check Boxes, and Radio Buttons and this suggestion.
I am trying to create a wizard in java swing programatically.
On the wizard pane I have a next button and it has to perform multiple actions according to which panel is displayed on the wizard.
Is it possible to use java command pattern? may I know how?
thanks in advance.
the code i used for the wizard is
this.mainPanel.add(fileSelectionPane,"SELECT FILE");
this.mainPanel.add(sqlConnectionPane,"SQL CONNECTION");
this.mainPanel.add(devicePane,"PARSER");
this.mainPanel.add(detailsPane,"DISPLAY");
thisLayout.show(this.mainPanel,"SELECT FILE");
this.finishButton.setEnabled(false);
this.backButton.setEnabled(false);
if(newValue==1) {
this.thisLayout.show(this.mainPanel, "SQL CONNECTION");
this.nextButton.setEnabled(true);
this.nextButton.setText("Connect..");
this.cancelButton.setEnabled(true);
this.backButton.setEnabled(true);
}
if(newValue==2) {
this.thisLayout.show(this.mainPanel, "PARSER");
this.nextButton.setEnabled(true);
this.nextButton.setText("Parse..");
this.cancelButton.setEnabled(true);
this.backButton.setEnabled(true);
}
i want the next button to perform specific actions on SELECT FILE and SQL CONNECTION.
is it possible to use command patterns?
Ok, so, You add action listeners to buttons. These action listeners do something when an event occurs.
You want to change the functionality of the button depending on which panel is being displayed? Why not set a instance variable which reflects the state of the Wizard?
For example (roughly),
int state = 0; // home panel
change panel to help page, event listener is fire, set 'state' to 1. You are now tracking which panel is being displayed.
Now, in your original problem, when the button (the one you want multiple functionality with) fires, you can choose the action it will take based on the 'state' var.
have look at CardLayout
those cards put to the JDialog (JDialog has preimplemented by default BorderLayout) to the CENTER area
create a new JPanel and place there JButtons
JPanel with JButtons put to the SOUTH area
search here, on this forum, there are a few excelent examples for wizard or image previue based on CardLayout
try the following code for button:
JButton btn1;
btn1= new javax.swing.JButton();
btn1.setToolTipText("Submit");
btn1.setContentAreaFilled(false);
btn1.setBorderPainted(false);
btn1.setMargin(new java.awt.Insets(2, 2, 2, 2));
btn1.addActionListener(this);
btn1.setIcon(this.getIcons()[21]);
add(btn1); // add to Jpanel
btn1.setBounds(250,10, 12, 12);
public void actionPerformed(java.awt.event.ActionEvent evt) {
Object obj = evt.getSource();
if (obj == btn1) {
// your function on on click of button
return;
}
I have a JTextField inside a JPanel A which is a part of CardLayout. When this A gets shown, I want to set the focus automatically to the JTextField (i.e. the cursor is flashing in the text field so the user doesn't need to click on it to enable the input). I tried calling requestFocusInWindow() on the JTextField object at initialization, but that doesn't seem to work. Do I need to call this method every time when A gets displayed? Thanks.
Maybe you can try requestFocusInWindow() when the panel is shown ?
something like this?
jPanel.addComponentListener(new ComponentAdapter() {
#Override
public void componentShown(java.awt.event.ComponentEvent e)
{
jTextField.requestFocusInWindow();
}
});