Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Change Button Name in JOptionPane.showMessageDialog

801912Aug 23 2007 — edited Aug 24 2007
Hello,
I've written the following code that invokes a JOPtionPane messageDialog, when the delete button is hit in the following frame and the selectedItem in the JComboBox is Default.
I want to change the JOPtionPane button name from OK to Close. Is there a way to do this with error message dialoges, as their is with confirmDialoges?
Please help out.
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class MyJOptionPaneTest {

	public MyJOptionPaneTest()
	{
		JFrame frame = new JFrame("My Frame");
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT,5,0));
		
		final JComboBox myComboBox = new JComboBox();
		myComboBox.setPreferredSize(new Dimension(100,20));
		myComboBox.getModel().setSelectedItem("Default");
		myComboBox.addItem("Default");
		myComboBox.addItem("Other");
		JButton deleteButton = new JButton("Delete");
		deleteButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if(myComboBox.getSelectedItem().equals("Default"))
				{
					JOptionPane.showMessageDialog(null, "Default cannot be deleted", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		panel.add(myComboBox);
		panel.add(deleteButton);
		
		frame.getContentPane().add(panel);
		frame.setSize(200,100);
		frame.setResizable(false);
		frame.setVisible(true);
	}
	public static void main(String [] args)
	{
		MyJOptionPaneTest test = new MyJOptionPaneTest();
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 21 2007
Added on Aug 23 2007
2 comments
501 views