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

  • SEARCH
  • Home
  • 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 8516627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:28:53+00:00 2026-06-11T05:28:53+00:00

I have a frame with a JPanel in it, it has a JScroll in

  • 0

I have a frame with a JPanel in it, it has a JScroll in it and what I would like is if when the frame is resized, the JScrollPane’s width grows to fit it. This functionality seems to work fine, however, upon resizing to a smaller dimension my JScrollPane doesn’t shrink.

package com.protocase.notes.views;

import com.protocase.notes.controller.NotesController;
import com.protocase.notes.model.Note;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;

/**
 * @author dah01
 */
public class NotesPanel extends JPanel {
    // <editor-fold defaultstate="collapsed" desc="Attributes">
    private Note note;
    private NotesController controller;
    private CardLayout cardLayout;
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Getters N' Setters">

    public NotesController getController() {
        return controller;
    }

    public void setController(NotesController controller) {
        this.controller = controller;
    }

    public Note getNote() {
        return note;
    }

    public void setNote(Note note) {
        this.note = note;
    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Constructor">

    /**
     * Sets up a note panel that shows everything about the note.
     * @param note 
     */
    public NotesPanel(Note note, NotesController controller) {
        this.note = note;
        cardLayout = new CardLayout();
        this.setLayout(cardLayout);

        // -- Setup the layout manager.
        this.setBackground(new Color(199, 187, 192));
        this.setBorder(new BevelBorder(BevelBorder.RAISED));

        // -- ViewPanel
        this.add("ViewPanel", makeViewPanel());
        this.add("EditPanel", makeEditPanel());
    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="EditPanel">
    private JPanel makeEditPanel() {
        JPanel editPanel = new JPanel();
        editPanel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 0.3;
        editPanel.add(makeCreatorLabel(), c);

        c.gridy++;
        editPanel.add(makeEditTextScroll(), c);

        c.gridy++;
        editPanel.add(makeEditorLabel(), c);

        c.gridy++;
        c.anchor = GridBagConstraints.EAST;
        editPanel.add(makeSaveButton(), c);

        return editPanel;
    }

    private JScrollPane makeEditTextScroll() {
        JTextArea notesContentsArea = new JTextArea(note.getContents());
        notesContentsArea.setLineWrap(true);
        notesContentsArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(notesContentsArea);
        scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
        return scrollPane;
    }

    private JButton makeSaveButton() {
        final CardLayout l = this.cardLayout;
        final JPanel p = this;

        final NotesController c = this.controller;
        final Note n = this.note;
        ActionListener al = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //controller.saveNote(n);
                l.next(p);
            }
        };

        JButton saveButton = new JButton("Save");
        saveButton.addActionListener(al);

        return saveButton;
    }

    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="ViewPanel">
    private JPanel makeViewPanel() {
        JPanel viewPanel = new JPanel();
        viewPanel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        c.fill = GridBagConstraints.HORIZONTAL  ;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 0.3;
        viewPanel.add(makeCreatorLabel(), c);

        c.gridy++;
        viewPanel.add(makeNoteTextArea(), c);

        c.gridy++;
        viewPanel.add(makeEditorLabel(), c);

        c.gridy++;
        c.anchor = GridBagConstraints.EAST;
        viewPanel.add(makeEditButton(), c);

        return viewPanel;
    }

    private JLabel makeCreatorLabel() {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        if (note != null) {
            String noteBy = "Note by " + note.getCreator();

            String noteCreated = formatter.format(note.getDateCreated());
            JLabel creatorLabel = new JLabel(noteBy + " @ " + noteCreated);
            creatorLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
            return creatorLabel;
        } else {
            System.out.println("NOTE IS NULL");
            return null;
        }


    }

    private JScrollPane makeNoteTextArea() {
        // -- Setup the notes area.
        JTextArea notesContentsArea = new JTextArea(note.getContents());
        notesContentsArea.setEditable(false);
        notesContentsArea.setLineWrap(true);
        notesContentsArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(notesContentsArea);
        scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
        return scrollPane;
    }

    private JLabel makeEditorLabel() {
        // -- Setup the edited by label.
        JLabel editorLabel = new JLabel(" -- Last edited by " + note.getLastEdited() + " at " + note.getDateModified());
        editorLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
        return editorLabel;
    }

    private JButton makeEditButton() {
        final CardLayout l = this.cardLayout;
        final JPanel p = this;

        ActionListener ar = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                l.next(p);
            }
        };

        JButton editButton = new JButton("Edit");
        editButton.addActionListener(ar);

        return editButton;
    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Grow Width When Resized">

    @Override
    public Dimension getPreferredSize() {
        int fw  = this.getParent().getSize().width;
        int fh = super.getPreferredSize().height;
        return new Dimension(fw,fh);
    }
    //</editor-fold>
}
  • 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-11T05:28:55+00:00Added an answer on June 11, 2026 at 5:28 am

    The solution is to stop overriding the getPrefferedSize of the outer component….

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a frame with 4 JPanels and 1 JScrollPane, the 4 panels are
I have a JPanel that has several (25) JLabel s on it, using a
I have frame with a button and a JPanel as I named panel, I
i found this link.. LINK what i want is there's a JPanel that has
I have frame buffer, with depth component and 4 color attachments with 4 textures
I have a frame extends from JWindow (because I want to handle my X
I have data frame with some numerical variables and some categorical factor variables. The
I have one frame in which one TestArea is there. When I append some
I have a frame containing a JMenuBar. If the users clicks the Logout MenuItem,
On my website I have a frame containing another page. When I click on

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.