Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How can I get this code to display in my GUI? Specifically my TextArea?

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can I get this code to display in my GUI? Specifically my TextArea?

    import java.util.ArrayList;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
     
     
    public class ThreeArrayLists extends JFrame {
     
     
     
     
    private static final long serialVersionUID = 1L;
     
     
     
    public ThreeArrayLists(){
    	super("ThreeArrayLists");
    	setLayout(new FlowLayout());
    	setSize(350,350);
    	JButton button = new JButton("Press");
    	JLabel label1 = new JLabel("Three Array Lists");
    	JTextArea textArea = new JTextArea(250,250);
    	getContentPane();
    	add(label1);
    	add(button);
    	add(textArea);
     
    	buttonHandler bHandle = new buttonHandler();
    	button.addActionListener(bHandle);
     
    }
    	class buttonHandler implements ActionListener{
    		public void actionPerformed(ActionEvent e)
    		{
    			ArrayList<Double> priceList = new ArrayList<Double>();
    			ArrayList<Double> quantityList = new ArrayList<Double>();
    			ArrayList<Double> amountList = new ArrayList<Double>();
     
    			priceList.add(new Double(10.62));
    			priceList.add(new Double(14.89));
    			priceList.add(new Double(13.21));
    			priceList.add(new Double(16.55));
    			priceList.add(new Double(18.62));
    			priceList.add(new Double(9.47));
    			priceList.add(new Double(6.58));
    			priceList.add(new Double(18.32));
    			priceList.add(new Double(12.15));
    			priceList.add(new Double(3.98));
     
    			quantityList.add(new Double(4.0));
    			quantityList.add(new Double(8.5));
    			quantityList.add(new Double(6.0));
    			quantityList.add(new Double(7.35));
    			quantityList.add(new Double(9.0));
    			quantityList.add(new Double(15.3));
    			quantityList.add(new Double(3));
    			quantityList.add(new Double(5.4));
    			quantityList.add(new Double(2.9));
    			quantityList.add(new Double(4.8));
    			{
    				for (int i = 0; i < 10; i++) {
    					amountList.add((Double.valueOf(priceList.get(i).toString())) * (Double.valueOf(quantityList.get(i).toString())));
     
    					System.out.printf("%d) %.2f * %.2f = %.2f \n",i+1, Double.valueOf(priceList.get(i).toString()), Double.valueOf(quantityList.get(i).toString()), Double.valueOf(amountList.get(i).toString()));
     
    				}
    					}
    			}
    			}
     
     
     
     
     
     
    public static void main(String[] args){
    	ThreeArrayLists newArray = new ThreeArrayLists();
    	newArray.setVisible(true);
     
    }}
    Last edited by jps; August 9th, 2013 at 02:07 PM. Reason: code tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How can I get this code to display in my GUI? Specifically my TextArea?

    When posting code, please use the highlight tag to preserve formatting.

    What does this code do? What do you want it to do instead?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I get this code to display in my GUI? Specifically my TextArea?

    Quote Originally Posted by KevinWorkman View Post
    When posting code, please use the highlight tag to preserve formatting.

    What does this code do? What do you want it to do instead?

    Right now, when the code runs, a button comes up. When you click the button, it displays the following in console:



    1) 10.62 * 4.0 = 42.48

    2) 14.89 * 8.5 = 126.56

    3) 13.21 * 6.0 = 79.26

    4) 16.55 * 7.35 = 121.64

    5) 18.62 * 9.0 = 167.58

    6) 9.47 * 15.3 = 144.89

    7) 6.58 * 3.0 = 19.74

    8) 18.32 * 5.4 = 98.93

    9) 12.15 * 2.9 = 35.24

    10) 3.98 * 4.8 = 19.1

    I need this output, to show up in my JTextArea in my GUI when the button is clicked.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How can I get this code to display in my GUI? Specifically my TextArea?

    Welcome jakhondi21
    Please use code tags when posting code, instructions can be found on the Announcements page
    See the documentation on JTextArea for the existing methods you can use.
    You may or may not want to add the text area to a JScrollPane, and a tutorial on their use can be found here

  5. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I get this code to display in my GUI? Specifically my TextArea?

    My apologies, next time I post code I will definitely use the tags.

Similar Threads

  1. Replies: 3
    Last Post: June 15th, 2013, 09:34 PM
  2. Replies: 0
    Last Post: May 23rd, 2013, 04:35 PM
  3. My GUI programs will not display.
    By BWPop95 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2013, 05:46 AM
  4. Need help with write to .dat file code from textarea
    By miller4103 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 2nd, 2013, 10:29 PM
  5. SWING GUI CONSOLE WINDOW DISPLAY
    By Khadafi in forum Java Theory & Questions
    Replies: 5
    Last Post: January 10th, 2012, 09:15 AM