Results 1 to 4 of 4
  1. #1
    tom2zip is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default adding text to a text area dynamically

    what i'm trying to achieve here is to add a line of text in a text area in another class called PageList
    i'm stuck, because in order for the text area to update, i have to exit the program completely and run it again
    any ideas?

    Java Code:
    import java.awt.BorderLayout;
    
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import java.awt.FlowLayout;
    import javax.swing.JLabel;
    import java.awt.GridLayout;
    import javax.swing.BoxLayout;
    import javax.swing.GroupLayout;
    import javax.swing.GroupLayout.Alignment;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    import javax.swing.LayoutStyle.ComponentPlacement;
    import javax.swing.JTextArea;
    import javax.swing.ScrollPaneConstants;
    import java.awt.Dimension;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.io.*;
    import java.util.*;
    
    public class PageAdd extends JFrame
    {
    
    	private JPanel panel;
    	private JTextField wordField;
    	
    	
    		
    	
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) throws IOException
    	{
    		EventQueue.invokeLater(new Runnable()
    		{
    			public void run()
    			{
    				try
    				{
    					PageAdd frame = new PageAdd();
    					frame.setVisible(false);
    				} catch (Exception e)
    				{
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public PageAdd()
    	{
    		setTitle("Add");
    		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		setBounds(100, 100, 408, 223);
    		panel = new JPanel();
    		panel.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(panel);
    		
    		JLabel lblWord = new JLabel("Word:");
    		lblWord.setPreferredSize(new Dimension(330, 14));
    		panel.add(lblWord);
    		
    		wordField = new JTextField();
    		wordField.setColumns(30);
    		panel.add(wordField);
    		
    		JLabel lblDefinition = new JLabel("Definition:");
    		lblDefinition.setPreferredSize(new Dimension(330, 14));
    		panel.add(lblDefinition);
    		
    		final JTextArea defArea = new JTextArea();
    		panel.add(defArea);
    		defArea.setLineWrap(true);
    		defArea.setRows(4);
    		defArea.setColumns(30);
    		
    		JScrollPane scroll = new JScrollPane(defArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    		panel.add(scroll);
    		
    		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
    		
    		JButton btnAdd = new JButton("Add");
    		btnAdd.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) 
    			{
    				if(wordField.getText().equals("") || defArea.getText().equals(""))
    				{
    					JOptionPane.showMessageDialog(null, "Please enter a word and a definition.");
    				}
    				else
    				{
    					try
    					{
    					
    						FileWriter fw = new FileWriter("G:\\FINAL PROJECT\\program\\list.txt", true);  //your text file
    						PrintWriter output = new PrintWriter(fw);
    						output.println(wordField.getText() + "\t" + defArea.getText());
    						output.close();
    						fw.close();
    						dispose();
    					
    					}
    					catch(IOException e)
    					{
    						e.printStackTrace();
    					}
    				
    				}
    			}
    		});
    		
    		btnAdd.setPreferredSize(new Dimension(80, 20));
    		panel.add(btnAdd);
    				
    				
    	}
    		
    	
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default Re: adding text to a text area dynamically

    Can you post the specific code segment where you added the text.

  3. #3
    tom2zip is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default Re: adding text to a text area dynamically

    this segment is in another class called PageList

    Java Code:
    class CustomListRenderer implements ListCellRenderer 
    		{
    			   public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) 
    			   {
    			        JTextArea renderer = new JTextArea(3,5);
    			        renderer.append(value.toString());
    			        renderer.setCaretPosition(renderer.getText().length()-1);
    			        renderer.setLineWrap(true);
    			        return renderer;
    			   }
    		}
    		
    		DefaultListModel listModel = new DefaultListModel();
    		for(int n=0; n<=pairs.size()-1; n++)
    		{
    			listModel.addElement("Word: " + pairs.get(n).word + "\n"+ "Definition: " + pairs.get(n).definition);
    		}

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  2. Text Area
    By pabloma2002 in forum CLDC and MIDP
    Replies: 1
    Last Post: 02-11-2011, 04:56 PM
  3. Replies: 0
    Last Post: 12-09-2010, 05:56 PM
  4. Replies: 0
    Last Post: 09-28-2010, 09:52 AM
  5. eol in text area....
    By Nicholas Jordan in forum Networking
    Replies: 0
    Last Post: 09-14-2008, 10:59 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
  •