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

Thread: JPanel and JText area keep resizing even though I'm telling it not to.

  1. #1
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default JPanel and JText area keep resizing even though I'm telling it not to.

    I have a JPanel that keeps resizing when the font in my JTextArea, which is in the JPanel, resizes. I don't like that. I want it to remain the same size. The JTextArea already scroll bars. I have a JFrame class that has a JPanel content pane that holds the JPanel with the JTextArea and I don't want that to show scrolls, unless, for instance, the user makes the screen less than full size. When full sized, I never want it to have to need scrolls. (It's a pain for users to navigate if it does that.)


    Also, in addition to growing when the font gets set to say, size 70, when I set the font small, say size 8, it shrinks and my JPanel shrinks to half its size, and there's a bunch of blank area showing and it looks goofy.

    I've tried adding a ComponentListener and checking for resizes and trying to set it back to how it was. I've tried setting the size of the JPanel and I've tried setting the size of the JTextArea. Nothing works.

    Also, I'm using BorderLayout throughout.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JPanel and JText area keep resizing even though I'm telling it not to.

    Post an example that demonstrates the problem. There are a number of possible causes and cures.

  3. #3
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: JPanel and JText area keep resizing even though I'm telling it not to.

    Here's one that does it. Note, I found that what causes it to happen is if the font is resized and then the JFrame window is iconified or changed in size. (You have to do something like mnimize the window or do something like that, in addition to changing the font.)

    import javax.swing.JFrame;
    import java.awt.Font;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import java.awt.BorderLayout;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JScrollPane;
     
     
    public class ResizingTextArea extends JFrame implements ActionListener
    {
     
     
       private JPanel contentPane;
       private JPanel top;
       private JPanel bottom;
       private JTextArea area;
       private JButton font1, font2, font3, font4;
       private JScrollPane spane;
     
       public ResizingTextArea()
       {
     
          super("Oh no!");
          setVisible(true);
          contentPane = new JPanel();
          setContentPane(new JScrollPane(contentPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
     
          top = new JPanel();
          contentPane.setLayout(new BorderLayout());
     
          contentPane.add(top, BorderLayout.NORTH);
          bottom = new JPanel();
     
          contentPane.add(bottom, BorderLayout.CENTER);
     
          area = new JTextArea(30, 30);
     
          spane = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
     
          top.add(spane);
     
          bottom.setLayout(new java.awt.FlowLayout());
     
          font1 = new JButton("Font 1");
          font2 = new JButton("Font 2");
          font3 = new JButton("Font 3");
          font4 = new JButton("Font 4");
     
     
          bottom.add(font1);
          bottom.add(font2);
          bottom.add(font3);
          bottom.add(font4);
     
          font1.addActionListener(this);
          font2.addActionListener(this);
          font3.addActionListener(this);
          font4.addActionListener(this);
     
          setDefaultCloseOperation(EXIT_ON_CLOSE);
     
     
     
       }
     
     
       public void actionPerformed(ActionEvent e)
       {
     
          if (e.getSource().equals(font1))
             area.setFont(new Font("Times New Roman", Font.PLAIN, 7));
     
          else if (e.getSource().equals(font2))
             area.setFont(new Font("Verdana", Font.PLAIN, 70));
     
          else if (e.getSource().equals(font3))
             area.setFont(new Font("Script", Font.BOLD, 30));
     
          else
             area.setFont(new Font("Tahoma", Font.ITALIC, 12));
     
     
     
     
       }
     
     
       public static void main(String[] args)
       {
          new ResizingTextArea();
     
     
     
       }
     
     
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JPanel and JText area keep resizing even though I'm telling it not to.

    You were doing some weird things, your variables had confusing names, and I was getting confused. I think you're creating JPanels you don't need, putting them on top of each other (which really replaces the previous with the latest), or inside each other, and I don't know what all. Like I said, I got confused. I can't tell you exactly what was causing the behavior you observed, except to say that you were doing it wrong. Maybe you can compare what I think are the useful parts posted below and figure out which of your parts are redundant and/or causing the problem.

    When building a GUI interface, have a plan in mind. Draw your layout, labeling each part and noting in which container each part is placed and where each part is placed in the container. If you don't need or want to draw it out, then comment it in your code. For this project, I'd have commented my code something like:

    In the constructor:
    create the JFrame named "Oh no!"

    create a JPanel to hold the text area (writingPanel)
    create a JTextArea
    create a JScrollPane that contains the JTextArea
    add the JScrollPane to the writingPanel

    add the JPanel to the CENTER of the JFrame

    create a JPanel to hold the font buttons (buttonPanel)

    create 4 font buttons

    add the 4 font buttons to the buttonPanel

    add the buttonPanel to the JFrame's SOUTH area

    Here's my code that does roughly what I've described above with the necessary adjustments and frills added:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
     
    public class ResizingTextArea extends JFrame implements ActionListener
    {
        private JPanel writingPanel;
        private JPanel buttonPanel;
        private JTextArea area;
        private JButton font1Button, font2Button, font3Button, font4Button;
        private JScrollPane spane;
     
        public ResizingTextArea()
        {
            super("Oh no!");
     
            // GB: moved one and added the second for convenience
            setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            setLocationRelativeTo( null );
     
            writingPanel = new JPanel();
            writingPanel.setPreferredSize( new Dimension( 400, 500 ) );
     
            area = new JTextArea(30, 30);
     
            spane = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            // GB: added this to be the same preferred size as its container
            spane.setPreferredSize( new Dimension( 400, 500 ) );
     
            writingPanel.add( spane );
     
            // GB: adds to the CENTER by default
            add( new JScrollPane( writingPanel ) );
     
            // a panel for the buttons that will be added at the south position
            buttonPanel = new JPanel();
     
            // create the font buttons
            font1Button = new JButton("Font 1");
            font2Button = new JButton("Font 2");
            font3Button = new JButton("Font 3");
            font4Button = new JButton("Font 4");
     
     
            // add the font buttons to the button panel
            buttonPanel.add(font1Button);
            buttonPanel.add(font2Button);
            buttonPanel.add(font3Button);
            buttonPanel.add(font4Button);
     
            // add the button panel to the bottom of the JFrame
            add( buttonPanel, BorderLayout.SOUTH );
     
            font1Button.addActionListener(this);
            font2Button.addActionListener(this);
            font3Button.addActionListener(this);
            font4Button.addActionListener(this);
     
            // GB: added the first line and moved the second one from the top
            // read what pack() does
            pack();
            setVisible(true);
     
        }
     
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource().equals(font1Button))
                area.setFont(new Font("Times New Roman", Font.PLAIN, 7));
     
            else if (e.getSource().equals(font2Button))
                area.setFont(new Font("Verdana", Font.PLAIN, 70));
     
            else if (e.getSource().equals(font3Button))
                area.setFont(new Font("Script", Font.BOLD, 30));
     
            else
                area.setFont(new Font("Tahoma", Font.ITALIC, 12));
     
        }
     
        // starts the swing app on the EDT
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater( new Runnable()
            {
                public void run()
                {
                    new ResizingTextArea();
                }
     
            } );
        }
     
    }

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    GoodbyeWorld (October 23rd, 2013)

  6. #5
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: JPanel and JText area keep resizing even though I'm telling it not to.

    I've never got why you call call SwingUtilities invokeLater. They do that a lot in the java examples.

    As for pack, why does it have to come before setVisible() ?

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JPanel and JText area keep resizing even though I'm telling it not to.

    Not everything I did must be done in the exact order I did it. I normally do it in that order, because it makes sense to me. The pack() does need to be called after the components have been added to the container, and it makes sense to me to make the container visible after it has been built. On a complicated interface, I imagine the graphics might be seen jumping around as the interface is built, sized, located, etc., but I don't know if that could actually happen.

    The SwingUtilities.invokeLater() statement is a way to start the Swing App on the Event Dispatch Thread, or EDT. There are other ways, most end up doing the same thing, but this is the way I learned and most commonly use. It's important. Read more about why it's important, how Swing was designed and why that design is not thread safe, how multi-threading or concurrency work in Java, etc. It's not a topic you'll absorb and understand in one sitting, but it's a topic you should continue to study until you do mostly understand it.

    I'm glad you're seeing the practice a lot in the Swing examples you've found, because it was very rare in the Swing tutorials I started with, even in the official ones, and those that didn't do it that way were all wrong.

    One of the things i really didn't like about your original code was the use of the variable name contentPane. It's a great name, so great in fact, that it is already in common use in Swing and means something very specific in containers. You also used the statement setContentPane() which I think caused most of your problems and my confusion. I'm not going to go back and unconfuse myself, but you can play around with your original code if you like to try and eliminate conflicts or code that cancelled out other code.

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

    GoodbyeWorld (October 26th, 2013)

  9. #7
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: JPanel and JText area keep resizing even though I'm telling it not to.

    I was always taught to do setContentPane() with Frames and InternalFrames.

    As for threading, I had a tough time understanding how to get that to work properly and usually ended up using Swing Timer instead (and it worked).

  10. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JPanel and JText area keep resizing even though I'm telling it not to.

    Once upon a time, . . . prior to 1.5, using JFrame's contentPane was the recommended approach. Read the notes for JFrame 1.4 and compare them to the notes for JFrame 1.5. Even after 1.5, obtaining the contentPane and adding to it is okay. It's just not necessary. Since 1.5, the add() method was constructed to do the same thing.

    So there was nothing wrong with what you were "always taught," however, your implementation was confusing at best.

    Swing Timers are important tools to be able to use, so it's great that those are working for you, and there's nothing wrong with that. Swing Timers fire an event that causes an actionListener to run, typically on the same thread as the Swing app, the EDT (ideally). Employing other threads to support a Swing app is a fine thing to do when needed, and those should normally be a SwingWorker subclass.

  11. The Following User Says Thank You to GregBrannon For This Useful Post:

    GoodbyeWorld (October 26th, 2013)

Similar Threads

  1. Replies: 5
    Last Post: August 10th, 2013, 03:21 PM
  2. Replies: 3
    Last Post: June 27th, 2013, 07:27 AM
  3. Create a JText field upon button click
    By jo15765 in forum AWT / Java Swing
    Replies: 3
    Last Post: May 28th, 2012, 07:06 PM
  4. Help, I have no idea what my console is telling me. :(
    By Zack22 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 18th, 2011, 08:48 AM
  5. Problem with JText Fields and using action listener
    By toble in forum AWT / Java Swing
    Replies: 2
    Last Post: October 27th, 2010, 04:44 PM