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 2 of 2

Thread: Need help with putting restraints on my text area.

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need help with putting restraints on my text area.

    my code works its just that my textarea in my message box runs out of my set window.

     
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Email extends JFrame implements ActionListener {
    	private JTextField toField;
    	private JTextField ccField;
    	private JTextField bccField;
    	private JTextField subjectField;
    	private JTextArea message;
    	private JButton send;
    	private JButton clear;
     
     
    	public Email() {
    		send = new JButton("SEND");
    		clear = new JButton("CLEAR");
    		toField = new JTextField(40);
    		ccField = new JTextField(40);
    		bccField = new JTextField(40);
    		subjectField = new JTextField(40);
    		message = new JTextArea(20,30);
    		message.setPreferredSize(new Dimension(10,20));
    		JPanel toPanel = new JPanel(new BorderLayout());
    		JPanel ccPanel = new JPanel(new BorderLayout());
    		JPanel bccPanel = new JPanel(new BorderLayout());
    		JPanel subjectPanel = new JPanel(new BorderLayout());
    		JPanel messagePanel = new JPanel(new BorderLayout());
    		toPanel.add(new JLabel("To:"), BorderLayout.WEST);
    		toPanel.add(toField, BorderLayout.EAST);
    		ccPanel.add(new JLabel("CC:"), BorderLayout.WEST);
    		ccPanel.add(ccField, BorderLayout.EAST);
    		bccPanel.add(new JLabel("BCC:"), BorderLayout.WEST);
    		bccPanel.add(bccField, BorderLayout.EAST);
    		subjectPanel.add(new JLabel("Subject:"), BorderLayout.WEST);
    		subjectPanel.add(subjectField, BorderLayout.EAST);
    		messagePanel.add(new JLabel("Message:"), BorderLayout.WEST);
    		messagePanel.add(message, BorderLayout.SOUTH);
    		JPanel layout = new JPanel(new GridLayout(5,1));
    		layout.add(toPanel);
    		layout.add(ccPanel);
    		layout.add(bccPanel);
    		layout.add(subjectPanel);
     
    		JPanel buttonPanel = new JPanel(new GridLayout(1,0));
    		send.addActionListener(this);
    		clear.addActionListener(this);
    		buttonPanel.add(send);
    		buttonPanel.add(clear);
     
    		add(layout, BorderLayout.NORTH);
    		add(messagePanel, BorderLayout.CENTER);
    		add(buttonPanel, BorderLayout.SOUTH);		
     
    	}
     
    	public void actionPerformed(ActionEvent event) {
     
     
    		try{		
    			if(event.getSource() == send) {
     
    				if(toField.getText().contains("@")) {
     
    					PrintWriter writer = new PrintWriter("email.txt");
    					writer.println("To: " + toField.getText());
    					writer.println("CC: " + ccField.getText());
    					writer.println("BCC: " + bccField.getText());
    					writer.println("Subject: " + subjectField.getText());
    					writer.println("Message: " + message.getText());
    					writer.close();
    					JOptionPane.showMessageDialog(null, "MESSAGE SENT!");
    					toField.setText("");
    					ccField.setText("");
    					bccField.setText("");
    					subjectField.setText("");
    					message.setText("");
    				}
    				else
    					JOptionPane.showMessageDialog(null, "Please enter a receipient!");
    			}
    		}
    		catch(IOException e) {
    		}	
    		if(event.getSource() == clear) {
    			toField.setText("");
    			ccField.setText("");
    			bccField.setText("");
    			subjectField.setText("");
    			message.setText("");
    		}
    	}
     
     
     
    	public static void main(String[] args) {
            Email win = new Email();
            win.setPreferredSize(new Dimension(500,500));
    		win.setResizable(false);
    		win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            win.pack();
            win.setVisible(true);
        }
    }


  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: Need help with putting restraints on my text area.

    Which JTextField is it? What are you adding it to exactly? I see quite a few JTextFields in your code, so I'm not sure which one you're talking about...
    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!

Similar Threads

  1. [SOLVED] [Asking] Listener on Text Area
    By ardisamudra in forum AWT / Java Swing
    Replies: 7
    Last Post: September 30th, 2012, 06:01 AM
  2. Text file to text area and Radiobuttons?
    By donaldmax in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 27th, 2011, 04:45 AM
  3. Display in a text area
    By susieferrari in forum Java Theory & Questions
    Replies: 21
    Last Post: March 22nd, 2011, 09:56 AM
  4. Putting text fields in an array
    By Movies32 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 10th, 2010, 07:46 PM
  5. Problem in implementing mortgage calculator
    By American Raptor in forum AWT / Java Swing
    Replies: 1
    Last Post: April 1st, 2009, 02:09 PM