Results 1 to 5 of 5
  1. #1
    xhoneyskye is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default adding jtextarea

    So... well I'm new to Java, this is the current code I have. I want to add a textarea at the east of everthing. Could someone help me with this or at least give me an idea on how? Please and thanks.

    Java Code:
    package javaapplication1;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Main extends JFrame implements ActionListener
    {
    
        Container thing = new Container();
        JTextField textfield1= new JTextField("jtextfield");
        JTextField textfield2= new JTextField("jtextfield");
        JTextArea textarea = new JTextArea("textarea");
        JButton button = new JButton("push");
    
        public Main ()
        {
            this.setSize(200, 200);
            
            thing.setLayout(new GridLayout(2,1));
            thing.add(textfield1);
            thing.add(textfield2);
            thing.add(button);
    
    
            button.addActionListener(this);
            
            this.setLayout(new BorderLayout());
            
            this.add(textfield1, BorderLayout.NORTH);
            this.add(textfield2, BorderLayout.CENTER);
            this.add(button, BorderLayout.SOUTH);
            
            this.setVisible(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        }
        
        public static void main(String[] args)
        {
            Main m = new Main();
        }
    
        public void actionPerformed(ActionEvent e) 
        {
            if(e.getSource().equals(button))
            {
                System.out.println("pushed!");
            }
        }
    
    }
    Last edited by Fubarable; 01-26-2010 at 04:10 AM. Reason: code tags added

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

    Default

    Hello, and welcome to the forum. I hope you don't mind that I changed your color tags into code tags since they make your code much easier to read.

    My obvious first question is have you tried to add your textfield to the JFrame (this), well actually it's contentPane, BorderLayout.EAST?

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

    Default

    Ah, I see what you're trying to do. To solve this you need to nest some of your components in another JPanel and add that to the BorderLayout.CENTER position of the JFrame, then add the textarea BorderLayout.EAST. e.g.,
    Java Code:
            this.setLayout(new BorderLayout());
            JPanel centerPanel = new JPanel(new BorderLayout());
            
            centerPanel.add(textfield1, BorderLayout.NORTH);
            centerPanel.add(textfield2, BorderLayout.CENTER);
            centerPanel.add(button, BorderLayout.SOUTH);
            
            add(centerPanel, BorderLayout.CENTER);
            add(textarea, BorderLayout.EAST);

  4. #4
    xhoneyskye is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default

    yessssssssssssssssssss that was exactly what I was looking for. Thank you so much :)

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

    Default

    Quote Originally Posted by xhoneyskye View Post
    yessssssssssssssssssss that was exactly what I was looking for. Thank you so much :)
    Quite welcome! :)

Similar Threads

  1. Tab key in JTextArea
    By KristoZ in forum New To Java
    Replies: 1
    Last Post: 09-25-2009, 08:27 PM
  2. About JTEXTAREA
    By makpandian in forum AWT / Swing
    Replies: 4
    Last Post: 03-19-2009, 07:53 AM
  3. JTextArea setting
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 01-29-2008, 03:57 AM
  4. How to add a shortcut key from JTextArea
    By sukatoa in forum Advanced Java
    Replies: 2
    Last Post: 01-28-2008, 09:39 AM
  5. JTextArea
    By saytri in forum New To Java
    Replies: 0
    Last Post: 01-13-2008, 02:07 AM

Posting Permissions

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