Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-08-2008, 10:00 PM
Member
 
Join Date: Apr 2008
Posts: 11
kewlgeye is on a distinguished road
JOptionPane Display Difficulties
Hello to all

I am trying to practice with arrays and GUI's using joptionpanes or boxes as well.

I am having difficulty with viewing the resulting windows. If you enter 5 as the size of the array then the window should print 5 4 3 2 1 in the messagebox but instead it freezes after I enter 5 and nothing happens, also if I just use the CMD to deliver the message "Please enter the size of the array: " then it works but I have to select ok 5 seperate times for each number instead of one box displaying 5 4 3 2 1.

How can I fix this. Here is the code that I made


Code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.JOptionPane; public class array{ static Scanner console = new Scanner(System.in); JFrame frame; public array(){ frame = new JFrame("Array"); JButton button = new JButton("Enter Here"); button.addActionListener(new MyAction()); frame.add(button); frame.setSize(400, 400); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public class MyAction implements ActionListener{ public void actionPerformed(ActionEvent e){ JOptionPane.showInputDialog(null, "" + "Enter the size of the array: "); int arraySize; arraySize = console.nextInt(); int[] list = new int[arraySize]; for (arraySize = 0; arraySize < list.length; arraySize++) if (arraySize == 0) continue; else JOptionPane.showMessageDialog(frame, "" + arraySize + " " + "is your number", "Your Silly Numbers", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(frame, "" + list.length + " " + "is your number", "Your Silly Numbers", JOptionPane.INFORMATION_MESSAGE); } } public static void main(String[] args){ array db = new array(); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-08-2008, 10:31 PM
Senior Member
 
Join Date: Jul 2007
Posts: 910
hardwired is on a distinguished road
This blocks input at the console
Code:
int arraySize = console.nextInt();
Code:
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class AnArrayTest { Scanner console = new Scanner(System.in); JFrame frame; public AnArrayTest() { frame = new JFrame("Array"); JButton button = new JButton("Enter Here"); button.addActionListener(new MyAction()); JPanel panel = new JPanel(); panel.add(button); frame.add(panel); frame.setSize(400, 400); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public class MyAction implements ActionListener{ public void actionPerformed(ActionEvent e){ String input = JOptionPane.showInputDialog(null, "Enter the size of the array: "); int arraySize = //console.nextInt(); Integer.parseInt(input.trim()); int[] list = new int[arraySize]; for (int arrayIndex = 0; arrayIndex < list.length; arrayIndex++) { list[arrayIndex] = arraySize - arrayIndex; } String values = Arrays.toString(list); JOptionPane.showMessageDialog(frame, values + " " + "is your number", "Your Silly Numbers", JOptionPane.INFORMATION_MESSAGE); } } public static void main(String[] args){ new AnArrayTest(); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-09-2008, 04:13 AM
Member
 
Join Date: Apr 2008
Posts: 11
kewlgeye is on a distinguished road
JOptionPane Display Difficulties
Thank you this worked well. i understand everything you did except this statement in the code

String values = Arrays.toString(list);

Also can I get the lines to print after 45 numbers on a new line. I am going to look this up and if I figure it out before someone posts I will post my findings.

Thanks
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-09-2008, 05:08 AM
Member
 
Join Date: Apr 2008
Posts: 11
kewlgeye is on a distinguished road
JOptionPane Display Difficulties
This is what I changed. it compiles, but I am still not able to start a new line after 40 numbers show.

Code:
public class MyAction implements ActionListener{ public void actionPerformed(ActionEvent e){ String input = JOptionPane.showInputDialog(null, "Enter the size of the array: "); int arraySize = //console.nextInt(); Integer.parseInt(input.trim()); int[] list = new int[arraySize]; for (int arrayIndex = 0; arrayIndex < list.length; arrayIndex++){ list[arrayIndex] = arraySize - arrayIndex;} values = Arrays.toString(list); if ((arraySize - 0 + 1) % 40 != 0) JOptionPane.showMessageDialog(frame, values + " " + "are your numbers", "Your Silly Numbers", JOptionPane.INFORMATION_MESSAGE); } }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-09-2008, 05:44 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,136
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by kewlgeye View Post
String values = Arrays.toString(list);
It's the string representation of the array specified.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-09-2008, 06:25 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,136
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Ok, everything is start from the user input size. Say the input is 45. What you want to do is, display 40 in one line and remain 5 is in the next line.

You try it in some way, what you want to do is do the same process for different sizes. Got it?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-09-2008, 08:13 PM
Member
 
Join Date: Apr 2008
Posts: 11
kewlgeye is on a distinguished road
if ((arraySize - 0 + 1) % 40 != 0)


This is the only thing that I know how to do in order to accomplish my goal. I read this off of a site, that is the if statement above.
I don't know how to get it to print to the next line after 40 number. I thought that the statement above did this and my placement of it was ok. This is where I need help. Either showing me, or a link that shows how to accomplish this within a JOptionPaneShowMessage window.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 05-09-2008, 09:09 PM
Senior Member
 
Join Date: Jul 2007
Posts: 910
hardwired is on a distinguished road
"arraySize" is a constant.
Code:
int arraySize = Integer.parseInt(input.trim()); int[] list = new int[arraySize]; for (int i = 0; i < list.length; i++) { list[i] = i+1; } String values = ""; for(int i = 0; i < list.length; i++) { values += list[i]; if(i < list.length-1) values += ", "; if((i + 1) % 40 == 0) values += "\n"; } JOptionPane.showMessageDialog(frame, values + " " +...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JOptionPane (customizing) Java Tip Java Tips 0 03-14-2008 12:39 PM
JOptionPane - showConfirmDialog(...) options Java Tip Java Tips 0 12-17-2007 10:38 AM
About JOptionPane.showMessageDialog jhetfield18 AWT / Swing 2 11-02-2007 11:45 PM
About JOptionPane.showMessageDialog jhetfield18 Advanced Java 0 11-02-2007 11:56 AM
problems with JOptionPane oregon AWT / Swing 2 08-05-2007 06:58 PM


All times are GMT +3. The time now is 06:11 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org