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

Thread: Problem with placing text with JPanels

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Problem with placing text with JPanels

    Hi! I want to place text-message within 4 JFrames. Running code shows only one message screen-shot is attached.
    Would you please suggest the way how to solve this.

    package test4_windows;
     
    import java.awt.*;
    import javax.swing.*;
     
    public class OutPutWindow {
     
        public OutPutWindow (String msg){
     
            JFrame f = new JFrame("Frame Head");
            JTextArea ta = new JTextArea(msg);
     
            f.setSize(300, 300);
     
            //Creating JPanel
    	JPanel contentPanel1 = new JPanel();
            contentPanel1.setBackground(Color.yellow); //set background color
            contentPanel1.add(ta);                    // add text area with message
     
            JPanel contentPanel2 = new JPanel();
            contentPanel2.setBackground(Color.green);
            contentPanel2.add(ta);
     
            JPanel contentPanel3 = new JPanel();
            contentPanel3.setBackground(Color.red);
            contentPanel3.add(ta);
     
            JPanel contentPanel4 = new JPanel();
            contentPanel4.setBackground(Color.white);
            contentPanel4.add(ta);   
     
            Container pane = f.getContentPane();
     
            pane.setLayout(new GridLayout(2,2));
            pane.add(contentPanel1);
            pane.add(contentPanel2);
            pane.add(contentPanel3);
            pane.add(contentPanel4);
     
            f.setVisible(true);
     
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        }
     
    }
    JFrame_text.jpg


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with placing text with JPanels

    Can you explain what the posted is supposed to do?
    Where are the 4 JFrames.

    Where are you trying to put text in a JPanel?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Problem with placing text with JPanels

    You cannot add a component to multiple containers. When you add the JTextArea to contentPane1, it is added to contentPane1, but when you then add the same JTextArea to contentPane2, it removes it from contentPane1 and then adds it to contentPane2.

    On this tutorial: (Using Top-Level Containers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)), it says:
    Each GUI component can be contained only once. If a component is already in a container and you try to add it to another container, the component will be removed from the first container and then added to the second.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Artem.N (April 25th, 2012)

  5. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Problem with placing text with JPanels

    Thanks for the hint. I've created separate JTextArea for each frame. So it works!

            JTextArea ta1 = new JTextArea(msg);
            JTextArea ta2 = new JTextArea(msg+"2");
            JTextArea ta3 = new JTextArea(msg+"3");
            JTextArea ta4 = new JTextArea(msg+"4");

  6. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Problem with placing text with JPanels

    Sorry, I have forgotten to describe the problem.
    In fact the problem is exactly as it was posted within next reply. I can not place same text (JTextArea) four times within four different frames. The only last assignment of JtextArea is displayed.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with placing text with JPanels

    A component can only be in one container. The last container it is added to will be its location.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Artem.N (April 25th, 2012)

Similar Threads

  1. Placing user input to a array and to then display it as a string
    By LaliB in forum Collections and Generics
    Replies: 5
    Last Post: January 12th, 2012, 11:41 AM
  2. 1 JFrame, more JPanels
    By jknwhz in forum AWT / Java Swing
    Replies: 3
    Last Post: October 24th, 2011, 11:18 PM
  3. Placing files in jre bin...
    By sulaiman2043 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 29th, 2010, 03:55 AM
  4. [SOLVED] placing packages of the same directory into an array
    By javanub:( in forum Java Theory & Questions
    Replies: 16
    Last Post: May 18th, 2010, 07:49 AM
  5. repainting a jframe containing two jpanels
    By musasabi in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 11th, 2010, 10:31 PM