Results 1 to 5 of 5
  1. #1
    Lawllerskates is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default Swing - Changing text in JTextArea from void

    I am having a problem with changing text in a JTextArea from a void..

    My Code:
    Java Code:
    /*
     * Class:  Pie
     */
    
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPopupMenu;
    import javax.swing.JTextArea;
    import javax.swing.UIManager;
    
    public class Pie extends JFrame implements ActionListener
    {
    	String type = "Nothing";
    	public Pie()
    	{
            super("Pie Machine");
            try 
            {       	UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            	JFrame.setDefaultLookAndFeelDecorated(true);
            	JPopupMenu.setDefaultLightWeightPopupEnabled(true);
            	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            	
            	JButton meat = new JButton("Meat");
            	JButton apple = new JButton("Apple");
            	JButton make = new JButton("Make Pie!");
            	JTextArea area = new JTextArea();
                area.setColumns(20);
                area.setLineWrap(true);
                area.setRows(5);
                area.setWrapStyleWord(true);
    
            	make.addActionListener(this);
            	meat.addActionListener(this);
            	apple.addActionListener(this);
    		
            	FlowLayout bord = new FlowLayout();
            	setLayout(bord);
            	//add("North", lbl);
            	add("Center", meat);
            	add("Center", apple);
            	add("Center", area);
            	//add("Center", make);
            	pack();
            	setVisible(true);
            }
            catch (Exception e) 
            {
    			e.printStackTrace();
    		}
    	}
    	public void actionPerformed(ActionEvent evt) 
    	{
    		String cmd = evt.getActionCommand();
    		if (cmd != null) 
    		{
    			if (cmd.equalsIgnoreCase("Apple")) 
    			{
    				isApple();
    			}
    			if (cmd.equalsIgnoreCase("Meat")) 
    			{
    				isMeat();
    			}
    		}
    	}
    	public void isMeat()
    	{
    		
    	}
    	public void isApple()
    	{
    		
    	}
    	public static void main(String[] arguments) 
            {
    		Pie frame = new Pie();
    		frame.setSize(250, 250);
    		frame.setResizable(false);
    		frame.setLocationRelativeTo(null);
            }
    }


    In the isApple() and isMeat() voids, how would I change the text of the JTextArea?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your problem has nothing to do with the methods returning void and everything to do with your JTextArea (area) variable declaration being buried within the Pie constructor. In this location it will be very hard to get a reference to the JTextArea object. Much better is to make area a class variable so that the isApple() and isMeat() methods can access it.
    Much luck!
    Last edited by Fubarable; 02-14-2010 at 02:22 AM.

  3. #3
    Lawllerskates is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    Ahh, Ok.. I'll rewrite it. Could you just give me the code to do it in the way you say and I'll make my way from there. Thanks in advance.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Lawllerskates View Post
    Ahh, Ok.. I'll rewrite it. Could you just give me the code to do it in the way you say and I'll make my way from there.
    Heck, no. How would you benefit in the long run by this?

  5. #5
    Lawllerskates is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    Heh, I already re-wrote it. Was easy, and I only did it for a bit of fun :D

    Java Code:
    import java.awt.FlowLayout;
    
    import java.awt.event.ActionEvent;
    
    import java.awt.event.ActionListener;
    
     
    
    import javax.swing.JButton;
    
    import javax.swing.JFrame;
    
    import javax.swing.JLabel;
    
    import javax.swing.JTextArea;
    
    import javax.swing.WindowConstants;
    
     
    
    public class Pie
    
    {
    
    	static JTextArea area = new JTextArea();
    
    	static JLabel resultLbl = new JLabel("Click the buttons to make a pie!");
    
    	static String pieType = "nothing";
    
    	static String pieType2 = "nothing";
    
        public static void main(String[] args) throws Exception 
    
        {
    
        			JFrame frame = new JFrame("The Pie Factory");
    
            		frame.setLayout(new FlowLayout());
    
                    JLabel firstLbl = new JLabel("First Filling:");
    
                    JLabel secondLbl = new JLabel("Second Filling:  ");
    
                    JLabel spaceLbl = new JLabel("                                              ");
    
                    JButton appleBtn = new JButton("Apple");
    
                    appleBtn.addActionListener(new ActionListener()
    
                    {
    
                        public void actionPerformed(ActionEvent e)
    
                        {
    
                            isApple();
    
                        }
    
                    });
    
                    JButton meatBtn = new JButton("Meat");
    
                    meatBtn.addActionListener(new ActionListener()
    
                    {
    
                        public void actionPerformed(ActionEvent e)
    
                        {
    
                            isMeat();
    
                        }
    
                    });
    
                    JButton octBtn = new JButton("Octopus");
    
                    octBtn.addActionListener(new ActionListener()
    
                    {
    
                        public void actionPerformed(ActionEvent e)
    
                        {
    
                            isOctopus();
    
                        }
    
                    });
    
                    JButton plumBtn = new JButton("Plum");
    
                    plumBtn.addActionListener(new ActionListener()
    
                    {
    
                        public void actionPerformed(ActionEvent e)
    
                        {
    
                            isPlum();
    
                        }
    
                    });
    
                    JButton mintBtn = new JButton("Mint");
    
                    mintBtn.addActionListener(new ActionListener()
    
                    {
    
                        public void actionPerformed(ActionEvent e)
    
                        {
    
                            isMint();
    
                        }
    
                    });
    
                    JButton jamBtn = new JButton(" Jam ");
    
                    jamBtn.addActionListener(new ActionListener()
    
                    {
    
                        public void actionPerformed(ActionEvent e)
    
                        {
    
                            isJam();
    
                        }
    
                    });
    
                    
    
                    area.setColumns(20);
    
                    area.setLineWrap(true);
    
                    area.setRows(2);
    
                    area.setWrapStyleWord(false);
    
                    
    
                    frame.add(firstLbl, "NORTH");
    
                    frame.add(appleBtn, "NORTH");
    
                    frame.add(meatBtn, "NORTH");
    
                    frame.add(octBtn, "NORTH");
    
                    frame.add(secondLbl, "NORTH");
    
                    frame.add(plumBtn, "NORTH");
    
                    frame.add(mintBtn, "NORTH");
    
                    frame.add(jamBtn, "NORTH");
    
                    frame.add(spaceLbl, "SOUTH");
    
                    frame.add(resultLbl, "SOUTH");
    
                    
    
                    frame.pack();
    
                    frame.setVisible(true);
    
                    frame.setSize(400, 200);
    
                    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
            		frame.setResizable(false);
    
            		frame.setLocationRelativeTo(null); 
    
        }
    
    	public static void isApple()
    
    	{
    
    		pieType = "Apple";
    
    		area.setText("");
    
    		displayPie();
    
    	}
    
    	public static void isMeat()
    
    	{
    
    		pieType = "Meat";
    
    		area.setText("");
    
    		displayPie();
    
    	}
    
    	public static void isOctopus()
    
    	{
    
    		pieType = "Octopus";
    
    		area.setText("");
    
    		displayPie();
    
    	}
    
    	public static void isPlum()
    
    	{
    
    		pieType2 = "Plum";
    
    		area.setText("");
    
    		displayPie();
    
    	}
    
    	public static void isMint()
    
    	{
    
    		pieType2 = "Mint";
    
    		area.setText("");
    
    		displayPie();
    
    	}
    
    	public static void isJam()
    
    	{
    
    		pieType2 = "Jam";
    
    		area.setText("");
    
    		displayPie();
    
    	}
    
    	public static void displayPie()
    
    	{
    
    		if(pieType.startsWith("A") || pieType.startsWith("E") || pieType.startsWith("I") || pieType.startsWith("O") || pieType.startsWith("U"))
    
    		{
    
    			resultLbl.setText("I made an " + pieType + " and " + pieType2 + " pie!");
    
    		}
    
    		else
    
    		{
    
    			resultLbl.setText("I made a " + pieType + " and " + pieType2 + " pie!");
    
    		}
    
    	}
    
    }
    And it has been useful, as now I can expand on it..

    Anyways, Thanks for your help :D

Similar Threads

  1. How to add JTextArea in JTable - Swing
    By phungho2000 in forum AWT / Swing
    Replies: 5
    Last Post: 12-17-2008, 09:18 PM
  2. Swing - Changing component default font
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-11-2008, 10:52 PM
  3. Replies: 5
    Last Post: 01-24-2008, 11:53 PM
  4. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 07:02 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
  •