Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8330107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:01:03+00:00 2026-06-09T02:01:03+00:00

I am using a JPanel with some JLabels and JTextFields as editor and renderer

  • 0

I am using a JPanel with some JLabels and JTextFields as editor and renderer in a JTree.

The user can click on any JTextField and modify the text. All works fine.

All I am missing is how I can dynamically resize (grow) the JTextField as the user is typing.

Below a small sample program that illustrates the problem. Click on i.e. “colors” and enter some additional characters. The JTextField keeps its size, which is expected, but I would like it to grow and shrink to accommodate the text.

I tried different layout managers, calculate the preferredSize of the JTextField and the JPanel, call invalidate() and the JPanel and the Tree and so on, without success.

Any ideas?

import java.awt.Component;
import java.util.EventObject;

import javax.swing.AbstractCellEditor;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;

public class Tree {

    public static void main(final String[] args) {
        new Tree().grow();
    }

    private void grow() {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JTree tree = new JTree();
        tree.setEditable(true);
        tree.setCellRenderer(new SomeTreeCellRenderer());
        tree.setCellEditor(new SomeTreeCellRenderer());

        frame.add(new JScrollPane(tree));

        frame.pack();
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private class SomeTreeCellRenderer extends AbstractCellEditor implements 
            TreeCellRenderer, TreeCellEditor {

        @Override
        public Object getCellEditorValue() {
            return null;
        }

        @Override
        public boolean isCellEditable(final EventObject event) {
            return true;
        }

        @Override
        public Component getTreeCellRendererComponent(
                final JTree tree, final Object value, final boolean selected, final boolean expanded, 
                final boolean leaf, final int row, final boolean hasFocus) {

            final JPanel panel = new JPanel();
            final BoxLayout layout = new BoxLayout(panel, BoxLayout.X_AXIS);
            panel.setLayout(layout);

            panel.add(new JLabel("Some text: "));
            panel.add(new JTextArea(String.valueOf(value)));

            return panel;
        }

        @Override
        public Component getTreeCellEditorComponent(
                final JTree tree, final Object value, final boolean isSelected,
                final boolean expanded, final boolean leaf, final int row) {

            return getTreeCellRendererComponent(tree, value, isSelected, expanded, leaf, row, false);
        }

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:01:05+00:00Added an answer on June 9, 2026 at 2:01 am

    What’s happening is that the container owning your panel doesn’t have a layout manager assigned to it – so the panel is not resizing when the text area is resizing. A simple fix to this would be to set the size of the panel whenever your text area resizes. So instead of directly doing this:

            panel.add(new JTextArea(String.valueOf(value)));
    

    You can do this:

            JTextArea ta = new JTextArea(String.valueOf(value));
            ta.addComponentListener(new ComponentListener() {
    
                @Override
                public void componentResized(ComponentEvent e) {
                    panel.setSize(panel.getPreferredSize());
                }
    
                @Override
                public void componentShown(ComponentEvent e) {}
    
                @Override
                public void componentMoved(ComponentEvent e) {}
    
                @Override
                public void componentHidden(ComponentEvent e) {}
            });
    
            panel.add(ta);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JTable that holds several JPanels using a custom renderer/editor. The JPanel
in Netbeans, I am using Jlabels to display images and text within a JFrame.
Im working on a Marquee JPanel ,and im using a JLabel to scroll Text
I can't get graphics/text to draw on my panel/canvas/window in my Java program (using
I have created some custom JPanel classes using the NetBeans GUI Builder. Next, I
I have a JPanel on which some drawing is performed using paintComponent method and
Without using super.paintComponent(g); can i still clear my Jpanel or Jframe Screen? I have
I am using a Java Project that gives an user interface for doing some
I’m using a JPanel to add JComponents on and I tried to set the
I'm writing a game which uses a border layout with a JPanel using BorderLayout.CENTER.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.