Welcome to Dream.In.Code
Become a Java Expert!

Join 150,198 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,010 people online right now. Registration is fast and FREE... Join Now!




JOptionpane.showMessageDialog

 
Reply to this topicStart new topic

JOptionpane.showMessageDialog

mediha
19 Sep, 2008 - 10:15 AM
Post #1

New D.I.C Head
*

Joined: 17 Sep, 2008
Posts: 6

how do you connect if statements with different output using JOptionPane.showMessageDialog..here is my code: can someone help me fix it. I need to prompt user to put different year and month and display should tell user how many days does the certain month has.
PLEASE HELP ASAP:
CODE:
import javax.swing.JOptionPane;

public class mhalil3_h2_p2 {
public static void main (String[] args){


String yearString = JOptionPane.showInputDialog("Enter a year");
/*convert a string year into a int value*/
int year = Integer.parseInt(yearString);

/*declare variable month*/
String monthString = JOptionPane.showInputDialog ("Enter a month");
/*convert a string month into a int value*/
int month = Integer.parseInt (monthString);

/*boolean expression for the leap year and # of days in a month*/
if (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0){
System.out.println ("29 days");
}
else
{
System.out.println("28 days");
}
/*declare variable output*/
String output = (null,year + "is a leap year?" + isLeapYear );
/*Display the result*/
JOptionPane.showMessageDialog (null, output);

}
}
User is offlineProfile CardPM
+Quote Post

pbl
RE: JOptionpane.showMessageDialog
19 Sep, 2008 - 02:05 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
CODE

import javax.swing.JOptionPane;

public class mhalil3_h2_p2 {
   public static void main (String[] args){
      
   int[] daysInMonth = {31,28,31,30,31,30,31,31,30,31,20,31};
    
  String yearString = JOptionPane.showInputDialog("Enter a year");
  /*convert a string year into a int value*/
  int year = Integer.parseInt(yearString);
    
  /*declare variable month*/
  String monthString = JOptionPane.showInputDialog ("Enter a month");
  /*convert a string month into a int value*/
  int month = Integer.parseInt (monthString);
  
  /*boolean expression for the leap year and # of days in a month*/
if (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0){
   daysInMonth[1] = 29;
}
    /*declare variable output*/
  String output = "in " + year + " Month " + month + " has " + daysInMonth[month] + " days";
   /*Display the result*/
  JOptionPane.showMessageDialog (null, output);

          }
}


User is offlineProfile CardPM
+Quote Post

pbl
RE: JOptionpane.showMessageDialog
25 Sep, 2008 - 08:39 PM
Post #3

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Google is your best friend

I Googled for JoptionPane JradioButton

and this is what I got.... can easily be stripped down for onlu display Km to Miles or Miles to KM

[code]
import javax.swing.*;
import javax.swing.JOptionPane; import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;

public class JOptionPaneDemo extends JFrame implements ActionListener {

Container contentPane = null;
private JButton jbnDialog;
String ButtonLabels;
private JRadioButton[] dialogTypeButtons;
private JRadioButton[] messageTypeButtons;
private int[] messageTypes = { JOptionPane.PLAIN_MESSAGE,
JOptionPane.INFORMATION_MESSAGE, JOptionPane.QUESTION_MESSAGE,
JOptionPane.WARNING_MESSAGE, JOptionPane.ERROR_MESSAGE };
private ButtonGroup messageTypeButtonGroup, buttonTypeButtonGroup,
dialogTypeButtonGroup;
private JRadioButton[] optionTypeButtons;
private int[] OptionTypes = { JOptionPane.DEFAULT_OPTION,
JOptionPane.YES_NO_OPTION, JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.OK_CANCEL_OPTION };
public static void main(String[] args) {
new JOptionPaneDemo();
}
public JOptionPaneDemo() {
super("JOptionPane Source Demo");
addWindowListener(new WindowListener());
contentPane = getContentPane();
contentPane.setLayout(new GridLayout(0, 1));
JPanel jplButton = new JPanel();
jbnDialog = new JButton("Show an Option Pane");
jbnDialog.addActionListener(this);
jplButton.add(jbnDialog);
contentPane.add(jplButton);
createRadioButtonGroupings();
ButtonLabels = "Button1 Button2 Button3";
pack();
setVisible(true);
}
public void createRadioButtonGroupings() {
JPanel jplDialogType = new JPanel();
dialogTypeButtonGroup = new ButtonGroup();
dialogTypeButtons = new JRadioButton[] {
new JRadioButton("Show Message", true),
new JRadioButton("Get Confirmation"),
new JRadioButton("Collect Input"),
new JRadioButton("Present Options") };
for (int i = 0; i < dialogTypeButtons.length; i++) {
dialogTypeButtonGroup.add(dialogTypeButtons[i]);
jplDialogType.add(dialogTypeButtons[i]);
}
contentPane.add(jplDialogType);
JPanel jplMessageType = new JPanel();
messageTypeButtonGroup = new ButtonGroup();
messageTypeButtons = new JRadioButton[] {
new JRadioButton("Plain"),
new JRadioButton("Information", true),
new JRadioButton("Question"), new JRadioButton("Warning"),
new JRadioButton("Error") };
for (int i = 0; i < messageTypeButtons.length; i++) {
messageTypeButtonGroup.add(messageTypeButtons[i]);
jplMessageType.add(messageTypeButtons[i]);
}
contentPane.add(jplMessageType);
JPanel jplButtonType = new JPanel();
buttonTypeButtonGroup = new ButtonGroup();
optionTypeButtons = new JRadioButton[] {
new JRadioButton("Default", true),
new JRadioButton("Yes/No"),
new JRadioButton("Yes/No/Cancel"),
new JRadioButton("OK/Cancel") };
for (int i = 0; i < optionTypeButtons.length; i++) {
buttonTypeButtonGroup.add(optionTypeButtons[i]);
jplButtonType.add(optionTypeButtons[i]);
}
contentPane.add(jplButtonType);
}
// Windows Listener for Window Closing
public class WindowListener extends WindowAdapter {

public void windowClosing(WindowEvent event) {
System.exit(0);
}
}
public void actionPerformed(ActionEvent event) {
/*
* dialogTypeButtons =
*
* new JRadioButton[] { new JRadioButton("Show Message", true),
*
* new JRadioButton("Get Confirmation"),
*
* new JRadioButton("Collect Input"),
*
* new JRadioButton("Present Options") };
*/
if (dialogTypeButtons[0].isSelected()) {
JOptionPane.showMessageDialog(this, "Show Message",
"Simple Dialog", getMessageType());
} else if (dialogTypeButtons[1].isSelected()) {
JOptionPane.showConfirmDialog(this, "Get Confirmation",
"Simple Dialog", getButtonType(), getMessageType());
} else if (dialogTypeButtons[2].isSelected()) {
JOptionPane.showInputDialog(this, "Collect Input",
"Simple Dialog", getMessageType(), null, null, null);
} else if (dialogTypeButtons[3].isSelected()) {
JOptionPane.showOptionDialog(this, "Present Options",
"Simple Dialog", getButtonType(), getMessageType(),
null, substrings(ButtonLabels), null);
}
}
private int getAssociatedType(AbstractButton[] buttons, int[] types) {
for (int i = 0; i < buttons.length; i++) {
if (buttons[i].isSelected()) {
return (types[i]);
}
}
return (types[0]);
}
private int getMessageType() {
return (getAssociatedType(messageTypeButtons, messageTypes));
}
private int getButtonType() {
return (getAssociatedType(optionTypeButtons, OptionTypes));
}
private String[] substrings(String string) {
StringTokenizer tok = new StringTokenizer(string);
String[] substrings = new String[tok.countTokens()];
for (int i = 0; i < substrings.length; i++)
substrings[i] = tok.nextToken();
return (substrings);
}
}
/code]
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:51AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month