Results 1 to 16 of 16
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [SOLVED] JOptionPane.ShowConfirmDialog() problem

    I've come across this code which I'm trying to understand and presumably it should show a series of popups (i.e. JOptionPane.ShowConfirmDialog(), JOptionPane.ShowInputDialog() etc), however nothing happens when I tinker with the radio buttons.

    Java Code:
        
    [b]OptionDialogFrame.java[/b]
    package homenetwork.bkr.training;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Date;
    import javax.swing.*;
    
    
    @SuppressWarnings("serial")
    public class OptionDialogFrame extends JFrame {
    
    	public OptionDialogFrame()
    	{
    		setTitle("Option Dialog Test");
    		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    		
    		JPanel gridPanel = new JPanel();
    		gridPanel.setLayout(new GridLayout(2,3));
    		
    		typePanel = new ButtonPanel("Type", "Message", "Confirm", "Option", "Input");
    		messageTypePanel = new ButtonPanel("Message Type", "ERROR_MESSAGE", "INFORMATION_MESSAGE", "WARNING_MESSAGE", "QUESTION_MESSAGE", "PLAIN_MESSAGE");
    		messagePanel = new ButtonPanel("Message", "String", "Icon", "Component", "Other", "Object[]");
    		optionTypePanel = new ButtonPanel("Confirm", "DEFAULT_OPTION", "YES_NO_OPTION", "YES_NO_CANCEL_OPTION", "OK_CANCEL_OPTION");
    		optionsPanel = new ButtonPanel("Option", "String[]", "Icon[]", "Object[]");
    		inputPanel = new ButtonPanel("Input", "Text field", "Combo box");
    		
    		gridPanel.add(typePanel);
    		gridPanel.add(messageTypePanel);
    		gridPanel.add(messagePanel);
    		gridPanel.add(optionTypePanel);
    		gridPanel.add(optionsPanel);
    		gridPanel.add(inputPanel);
    		
    		//add a panel with a Show button
    		JPanel showPanel = new JPanel();
    		JButton showButton = new JButton("Show");
    		[b]showButton.addActionListener(new ShowAction());[/b] //adding a listener, right?
    		showPanel.add(showButton);
    		add(gridPanel, BorderLayout.CENTER);
    		add(gridPanel,BorderLayout.SOUTH);
    		
    	}
    	
    	/** Gets the currently selected message.
    	 * @return a string, icon, component or object array, depending on the message panel selection.
    	 */
    	public Object getMessage()
    	{
    		String s = messagePanel.getSelection();
    		if (s.equals("String")) return messageString;
    		else if (s.equals("Icon")) return messageIcon;
    		else if (s.equals("Component")) return messageComponent;
    		else if (s.equals("Object[]")) return new Object[] {messageString, messageIcon, messageComponent, messageObject};
    		else if (s.equals("Other")) return messageObject;
    		else return null;
    	}
    	
    	/**
    	 * Gets the currently selected options.
    	 * @return: an array of strings, icons of objects, depending on the Option panel selection.
    	 */
    	public Object[] getOptions(){
    		String s = optionsPanel.getSelection();
    		if (s.equals("String[]")) return new String[] {"Yellow", "Blue", "Red"};
    		else if (s.equals("Icon[]")) return new 
    								ImageIcon[] {new ImageIcon("C:\\icons\\yellow-ball.png"),
    											new ImageIcon("C:\\icons\\blue-ball.png"), new ImageIcon("C:\\icons\\red-ball.png")};
    		else if (s.equals("Object[]")) return new Object[] {messageString, messageIcon, messageComponent, messageObject};
    		else return null;
    		}
    		
    	/**
    	 * Gets the selected message or option type
    	 * @param panel: the Message Type or Confirm panel
    	 * @return: the selected XXX_MESSAGE or XXX_OPTION constant from the JOptionPane class.
    	 */
    	public int getType(ButtonPanel panel)
    	{
    		String s = panel.getSelection();
    		try 
    		{
    			return JOptionPane.class.getField(s).getInt(null);
    		}
    		catch (Exception e)
    		{
    			return -1;
    		}
    	}
    	
    	/** The action listener for the Show button shows a Confirm, Input, Message, 
    	 * or Option dialog depending on the Type panel selection.
    	 */
    	[b]private class ShowAction implements ActionListener[/b]
    	{
    		[b]public void actionPerformed (ActionEvent event)
    		{[/b]
    			if (typePanel.getSelection().equals("Confirm")) 
    				JOptionPane.showConfirmDialog(OptionDialogFrame.this, getMessage(), "Title", getType(optionTypePanel), getType(messageTypePanel));
    			else if (typePanel.getSelection().equals("Input"))
    			{
    				if (inputPanel.getSelection().equals("Text Field")) 
    					JOptionPane.showInputDialog(OptionDialogFrame.this, getMessage(), "Title", getType(messageTypePanel));
    				else JOptionPane.showInputDialog(OptionDialogFrame.this, getMessage(), "Title", getType(messageTypePanel), null, new String[] {"Yellow", "Blue", "Red"}, "Blue");
    			}
    			else if  (typePanel.getSelection().equals("Message")) 
    				JOptionPane.showMessageDialog(OptionDialogFrame.this, getMessage(), "Title", getType(messageTypePanel));
    			else if (typePanel.getSelection().equals("Option"))
    				JOptionPane.showOptionDialog(OptionDialogFrame.this, getMessage(), "Title", getType(optionTypePanel), getType(optionTypePanel), null, getOptions(), getOptions()[0]);
    		}
    	}
    	
    	public static final int DEFAULT_WIDTH = 600;
    	public static final int DEFAULT_HEIGHT = 400;
    	
    	private ButtonPanel typePanel;
    	private ButtonPanel messagePanel;
    	private ButtonPanel messageTypePanel;
    	private ButtonPanel optionTypePanel;
    	private ButtonPanel optionsPanel;
    	private ButtonPanel inputPanel;
    	
    	private String messageString = "Message";
    	private ImageIcon messageIcon = new ImageIcon("C:\\icons\\blue-ball.png");
    	private Object messageObject = new Date();
    	private Component messageComponent = new SampleComponent();
    	
    }
    
    [b]ButtonPanel.java[/b]
    package homenetwork.bkr.training;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class ButtonPanel extends JPanel {
    
    	/** Constructs a button panel.
    	 * @param title: the title shown in the border.
    	 * @param options: an array of radio button labels
    	 */
    	public ButtonPanel(String title, String... options)
    	{
    		setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder()));
    		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    		group = new ButtonGroup();
    		
    		//make one radio button for each option
    		for (String option: options)
    		{
    			JRadioButton b = new JRadioButton(option);
    			b.setActionCommand(option);
    			add(b);
    			group.add(b);
    			b.setSelected(option == options[0]);
    		}
    		
    	}
    	
    	/**
    	 * Get the currently selected option.
    	 * Return the label of the currently selected radio button.
    	 */
    	public String getSelection()
    	{
    		return group.getSelection().getActionCommand();
    	}
    	
    	private ButtonGroup group;
    }
    
    [b]OptionDialogTest.java[/b]
    package homenetwork.bkr.training;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.util.*;
    import javax.swing.*;
    
    public class OptionDialogTest {
    	
    	public static void main (String[] args)
    	{
    		EventQueue.invokeLater(new Runnable()
    		{
    			public void run()
    			{
    				OptionDialogFrame frame = new OptionDialogFrame();
    				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				frame.setVisible(true);
    			}
    		});
    	}
    	
    	
    }
    
    [b]SampleComponent.java[/b]
    package homenetwork.bkr.training;
    
    import java.awt.*;
    import java.awt.geom.Rectangle2D;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class SampleComponent extends JComponent {
    	public void paintComponent(Graphics g)
    	{
    		Graphics2D g2 = (Graphics2D) g;
    		Rectangle2D rect = new Rectangle2D.Double(0,0, getWidth() - 1, getHeight() - 1);
    		g2.setPaint(Color.YELLOW);
    		g2.fill(rect);
    		g2.setPaint(Color.blue);
    		g2.draw(rect);
    	}
    	
    	public Dimension getPreferredSize()
    	{
    		return new Dimension(10,10);
    	}
    
    }
    Output here.

    NOTE: Are the URL tags (e.g. CODE) case-sensitive?
    :confused:

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    jon, it's much much better ask your question more specifically. You've just post a lengthy code, and it can be a real mess to read.

    So where you stuck with JOptionPane? If you want to study more about those things from the beginning, better to start with smaller examples. Not an example like this.


    Quote Originally Posted by jon80 View Post
    NOTE: Are the URL tags (e.g. CODE) case-sensitive?
    :confused:
    Are you talking about the forum tags? Forum tags are not case sensitive. Just keep in mind to add relevant close tag for each open tag.

  3. #3
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Umm...I pointed out in bold in the code where I think that the listener is being called in the code above.

    Sorry, but according to some post I've recently read the code I post is expected to compile or reproduce the actual problem.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    i found that you do not add showPanel to the main frame
    you only add action listener to listen click action of "show" button inside showPanel
    not action of radio button
    so you wont able to see any popup

  6. #6
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Would you kindly show me where?

  7. #7
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    Yes, that's one of the way you can call the action listener. So...
    :confused:

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by jon80 View Post
    Would you kindly show me where?
    That's why I told you to start with simple examples, not like this.

    As mtyoung says check that, where's showPanel define and how to added it into the main frame. So first of all you have to identify where the main frame is. Just have a try.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  10. #10
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    It's an open-ended question, sorry about that.

    I have a problem with the above code, and, unfortunately, haven't found a solution as yet.

    There were recommendations by 'mtYoung', however, I could not clearly understand the portions of the code s/he was referring to.

    The problem, from what I can see is methods called within the private class ShowAction are not displaying any dialogs whatsover, and, it seems that either myself or the original author of the source code has incorrectly designed the code.

    Would you kindly guide me into troubleshooting, and hopefully resolving the problem in order to have "working code"?

  11. #11
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    have you try to find "showPanel" in the code?

    Java Code:
    add(gridPanel, BorderLayout.CENTER);
    add(gridPanel,BorderLayout.SOUTH);
    you add gridPanel twice, and showPanel not added
    you dont know what Ctrl-F is?

  12. #12
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    lol, sorry bout that.

  13. #13
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Btw, any idea whether it is possible to wrap components within a grid so that they fit the size of the ButtonPanel?

    GridBagLayout

  14. #14
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    in ButtonPanel, the layout set to use BoxLayout with X_AXIS

    you may try Y_AXIS or PAGE_AXIS

    please read BoxLayout (Java Platform SE 6)

  15. #15
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    "The components will not wrap so, for example, a vertical arrangement of components will stay vertically arranged when the frame is resized"

    BoxLayout (Java Platform SE 6)

  16. #16
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

Similar Threads

  1. JOptionPane
    By Mir in forum New To Java
    Replies: 5
    Last Post: 11-29-2008, 03:04 AM
  2. JOptionPane problem
    By MomenT in forum New To Java
    Replies: 22
    Last Post: 10-02-2008, 06:56 PM
  3. [SOLVED] On Exit JoptionPane problem
    By Mir in forum New To Java
    Replies: 24
    Last Post: 07-15-2008, 08:39 AM
  4. JOptionPane - showConfirmDialog(...) options
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 10:38 AM
  5. Problem with JOptionPane.showInputDialog()
    By romina in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 06:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •