Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By markious

Thread: GUI, printing available Locales

  1. #1
    markious's Avatar
    markious is offline Member
    Join Date
    Oct 2008
    Posts
    27
    Rep Power
    0

    Default GUI, printing available Locales

    Ok so Im trying to print out the available Locales on a machine in a text area


    The lie of code output.setText(availableLocales); is giving the trouble.

    Eclipse is giving out about it but Im not to sure on the problem why.

    Can anyone shed some light please



    Java Code:
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Calendar;
    import java.util.Locale;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    
    
    
    /**
     * @author B00022881 Mark Walsh 
     *
     */
    public class localesTest extends JFrame implements ActionListener {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        
        
        JTextArea output = new JTextArea();
        Locale[]  availableLocales = Calendar.getAvailableLocales( );
        
        public localesTest(){
            
            super("Testing Available Locales");
            
            Container c = getContentPane();
            
            JPanel panel = new JPanel();
            
            output.setText("Available Locales will Appear here..");
            output.setEditable(false);
            
            
            JButton listLocals = new JButton();
            listLocals.setText("List All Locales");
            listLocals.addActionListener(this);
            
            
            JScrollPane outputPane = new JScrollPane(output);
            panel.add(listLocals);
            panel.add(outputPane);
            
            c.add(panel);
            
            setSize(250, 300);
            setVisible(true);
            setResizable(false);
        }
        
        
        public void actionPerformed(ActionEvent e) {
            
            if(e.getActionCommand().equals("List All Locales")){
                
    
                /**
                * This Line is giving the Problems
                *
                */
                output.setText(availableLocales);
                
            }
            
        }
    
        
        public static void main(String[] args) {
            localesTest myLocalest = new localesTest();
            myLocalest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        }
    
    }


    Thanks in advance

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    This Line is giving the Problems
    What problems?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    markious's Avatar
    markious is offline Member
    Join Date
    Oct 2008
    Posts
    27
    Rep Power
    0

    Default

    Emm here's the exact definition


    The method setText(String) in the type JTextComponent is not applicable for the arguments (Locale[]) localesTest.java

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    There you go. What output do you expect?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    markious's Avatar
    markious is offline Member
    Join Date
    Oct 2008
    Posts
    27
    Rep Power
    0

    Default

    Im trying to output the Locales available on the current machine.


    Just looking at it some more I should probably have a for loop to go through the array

    so I now have the line

    Java Code:
    for(int i=0; i<availableLocales.length; i++){
                
                   output.setText(availableLocales);
            }
    Same problem

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    Yes, even if you had the output.setText(availableLocales[i]); part correct, because you'd
    a) overwrite the text in every iteration and
    b) a Locale is not a String

    You can either loop and use Locale's toString() implementation, but remember to append. Or you set the text using Arrays.toString(availableLocales).
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    markious's Avatar
    markious is offline Member
    Join Date
    Oct 2008
    Posts
    27
    Rep Power
    0

    Default

    Thanks man :D, ill work on that now.

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    Have fun:)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    markious's Avatar
    markious is offline Member
    Join Date
    Oct 2008
    Posts
    27
    Rep Power
    0

    Default

    Got it,

    Thanks for the help..

    I just used output.append to print out the contents of the array.


    Replaced what was in my actionPerformed with


    Java Code:
    if(e.getActionCommand().equals("List All Locales")){
                
                
                for(int i=0; i<availableLocales.length; i++){
                
                    
                    output.append(availableLocales[i].getDisplayName()+"\n");
            }
                }

    So full code reads

    Java Code:
    //imports
    
    public class localesTest extends JFrame implements ActionListener {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        
        
        JTextArea output = new JTextArea(20, 22);
        Locale[]  availableLocales = Calendar.getAvailableLocales( );
        
        public localesTest(){
            
            super("Testing Available Locales");
            
            Container c = getContentPane();
            
            JPanel panel = new JPanel();
            
            output.setText("Available Locales will Appear here..");
            output.setEditable(false);
            
            
            JButton listLocals = new JButton();
            listLocals.setText("List All Locales");
            listLocals.addActionListener(this);
            
            
            JScrollPane outputPane = new JScrollPane(output);
            panel.add(listLocals);
            panel.add(outputPane);
            
            c.add(panel);
            
            setSize(250, 300);
            setVisible(true);
            setResizable(false);
        }
        
        
        public void actionPerformed(ActionEvent e) {
            
            if(e.getActionCommand().equals("List All Locales")){
                
                
                for(int i=0; i<availableLocales.length; i++){
                
                    
                    output.append(availableLocales[i].getDisplayName()+"\n");
            }
                }
            
        }
    
        
        public static void main(String[] args) {
            localesTest myLocalest = new localesTest();
            myLocalest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        }
    
    }
    GerB68 likes this.

  10. #10
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    Good. Glad you solved it.

    EDIT: while we're at it, class names start with a capital letter, so it should be LocalesTest and there is no need to extend JFrame, as you don't alter JFrame's behaviour. Rather have a JFrame instance in your class. And last but not least start your window from the EDT.
    Last edited by PhHein; 03-04-2010 at 04:39 PM. Reason: More complaining about style ;-)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  11. #11
    markious's Avatar
    markious is offline Member
    Join Date
    Oct 2008
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by PhHein View Post

    EDIT: while we're at it, class names start with a capital letter, so it should be LocalesTest
    Cheers Ill remember that from now on.

    Quote Originally Posted by PhHein View Post
    and there is no need to extend JFrame, as you don't alter JFrame's behaviour. Rather have a JFrame instance in your class.
    Usually I would actually, don't know what made me extend this time around

    Quote Originally Posted by PhHein View Post
    And last but not least start your window from the EDT.
    Ok excuse the newwb question but by EDT you mean?

  12. #12
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    The EDT is the event-dispatching thread. See this example from the tutorials:
    http://java.sun.com/docs/books/tutor...FrameDemo.java
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  13. #13
    markious's Avatar
    markious is offline Member
    Join Date
    Oct 2008
    Posts
    27
    Rep Power
    0

Similar Threads

  1. Help in Printing
    By kirly in forum Advanced Java
    Replies: 3
    Last Post: 10-03-2011, 03:40 PM
  2. Printing
    By zzpprk in forum AWT / Swing
    Replies: 0
    Last Post: 01-20-2010, 11:25 AM
  3. Printing Help...
    By chiragkini in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2009, 06:07 AM
  4. Printing Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:41 PM
  5. Listing all available Locales
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-29-2007, 04:54 PM

Posting Permissions

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