The Artima Developer Community
Sponsored Link

Java Answers Forum
javaxswing message dialog TABLE please help

3 replies on 1 page. Most recent reply: Sep 19, 2005 10:32 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Jocke

Posts: 2
Nickname: j0k3
Registered: Sep, 2005

javaxswing message dialog TABLE please help Posted: Sep 14, 2005 6:32 AM
Reply to this message Reply
Advertisement
Hello everyone!
I'm still new to Java and im experimenting some to gain knowledge, but now I'm stucked.
I have made a program that is supposed to show a list of celcius degrees that tells how much fahrenheit that is.
However it doesnt do that, It shows fx:

"xx Celcius = xx Fahrenheit" in a popupwindow, but then I have to press OK to get the next value.

Can someone please help me? I want everything in the same first window, just a newline and then print the next celcius/fahrenheit.

Thank you, and sorry about my english.


import javax.swing.*; 
 
public class ovning_2_6 
{ 
    
   public static void main(String[] arg) 
   { 
      
      String c; 
      int fahrenheit; 
      int celcius; 
          
             
      for(int i=-20; i<21; i++) 
      {    
            
        celcius=i; 
         fahrenheit=celcius*9/5+32; 
          
         JOptionPane.showMessageDialog(null, "Celcius: " + celcius + ". Fahrenheit: " + fahrenheit + "\n"); 
 
                
                
      } 
      
      System.exit(0); 
      
  
   } 
    
    
}


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: javaxswing message dialog TABLE please help Posted: Sep 14, 2005 10:40 PM
Reply to this message Reply
I'm wondering why you don't understand that the showMessageDialog method opens a message dialog ...


1. You need to create a JFrame.
2. In this JFrame place a JScrollPane
3. Create Your list of degrees either in a 2dimensional array or a Vector of Vectors (meaning a Vector who's elements(lines) contain another vector with the line's elements).
4. create a JTable using the list of values and a list with the column names.
5. use the scrollpanes's setViewportView to put the table in the scrollpane.
6. set the JFrame to visible. Maybe you first want to set it's size on the screen.


How all of this is done?
Well, there exists something called Java API wich explains pretty much everything.



If you don't want to use a table: Create a JList instead.
Create Strings with the same content as in your message box and add them as Items.

Jocke

Posts: 2
Nickname: j0k3
Registered: Sep, 2005

Re: javaxswing message dialog TABLE please help Posted: Sep 19, 2005 4:05 AM
Reply to this message Reply
Hi , thank you!

I managed to fix this, I did like this:


import javax.swing.*;

public class ovning_2_6
{

public static void main(String[] arg)
{

String utskrift="0";
int fahrenheit;
int celcius;


for(int i=-20; i<21; i++)
{

celcius=i;
fahrenheit=celcius*9/5+32;

utskrift = utskrift + "Celcius: " + celcius + ". Fahrenheit: " + fahrenheit + "\n";



}

JOptionPane.showMessageDialog(null, utskrift);

System.exit(0);


}


}

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: javaxswing message dialog TABLE please help Posted: Sep 19, 2005 10:32 PM
Reply to this message Reply
Well, if the checkbox is enough for you ;)


Just one hint: I don't think you will get the correct results, since you only use Integer operations.
This way a Celsius degree of 1 would be 36 Fahrenheit, but it should be 36.8

The best thing would be to make celsius and fahrenheit double values. Since celsius is the first value used in the formula, every following operation would be a floating point operation then and you woudl get the correct results.

Flat View: This topic has 3 replies on 1 page
Topic: Save text file with images Previous Topic   Next Topic Topic: when to use ejbs

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use