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 8560883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:22:43+00:00 2026-06-11T16:22:43+00:00

I have a basic notes panel that I’m looking to shrink the width of

  • 0

I have a basic notes panel that I’m looking to shrink the width of when the parent jframe is resized but it isn’t happening. I’m using nested gridbaglayouts.

package com.protocase.notes.views;

import com.protocase.notes.controller.NotesController;
import com.protocase.notes.model.Subject;
import com.protocase.notes.model.Note;
import com.protocase.notes.model.database.PMSNotesAdapter;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 * @author DavidH
 */
public class NotesViewer extends JPanel {
    // <editor-fold defaultstate="collapsed" desc="Attributes">

    private Subject subject;
    private NotesController controller;

    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Getters N' Setters">
    /**
     * Gets back the current subject.
     * @return 
     */
    public Subject getSubject() {
        return subject;
    }

    public NotesController getController() {
        return controller;
    }

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

    /**
     * Should clear the panel of the current subject and load the details for 
     * the other object.
     * @param subject 
     */
    public void setSubject(Subject subject) {
        this.subject = subject;
    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Constructor">

    /**
     * -- Sets up a note viewer with a subject and a controller. Likely this 
     *    would be the constructor used if you were passing off from another
     *    NoteViewer or something else that used a notes adapter or controller.
     * @param subject
     * @param controller 
     */
    public NotesViewer(Subject subject, NotesController controller) {
        this.subject = subject;
        this.controller = controller;
        initComponents();
    }

    /** 
     * -- Sets up a note view with a subject and creates a new controller. This
     *    would be the constructor typically chosen if choosing notes was 
     *    infrequent and only one or two notes needs to be displayed.
     * @param subject 
     */
    public NotesViewer(Subject subject) {
        this(subject, new NotesController(new PMSNotesAdapter()));
    }

    /** 
     *  -- Sets up a note view without a subject and creates a new controller. 
     *     This would be for a note viewer without any notes, perhaps populating
     *     as you choose values in another form.
     * @param subject 
     */
    public NotesViewer() {
        this(null);
    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="initComponents()">

    /**
     * Sets up the view for the NotesViewer
     */
    private void initComponents() {
        // -- Make a new panel for the header
        JPanel panel = new JPanel();

        panel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();


        c.gridx = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridy = 0;
        c.weightx = .5;
        //c.anchor = GridBagConstraints.NORTHWEST;
        JLabel label = new JLabel("Viewing Notes for [Subject]");
        label.setAlignmentX(JLabel.LEFT_ALIGNMENT);
        label.setBorder(BorderFactory.createLineBorder(Color.YELLOW));

        panel.add(label);

        JButton newNoteButton = new JButton("New");
        c = new GridBagConstraints();
//        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 0;
        c.weightx = .5;
        c.anchor = GridBagConstraints.EAST;
        panel.add(newNoteButton, c);

        // -- NotePanels
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1;
        c.weighty = 1;
        c.gridx = 0;
        c.gridwidth = 2;


        int y = 1;
        for (Note n : subject.getNotes()) {
            c.gridy = y++;
            panel.add(new NotesPanel(n, controller), c);
        }


        this.setLayout(new GridBagLayout());
        GridBagConstraints pc = new GridBagConstraints();
        pc.gridx = 0;
        pc.gridy = 0;
        pc.weightx = 1;
        pc.weighty = 1;
        pc.fill = GridBagConstraints.BOTH;

        panel.setBackground(Color.blue);
        JScrollPane scroll = new JScrollPane();
        scroll.setViewportView(panel);
        //scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        this.add(scroll, pc);
        //this.add(panel, pc);
        // -- Add it all to the layout
    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="private methods">
    //</editor-fold>
}

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.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;

/**
 * @author dah01
 */
public class NotesPanel extends JPanel {
    // <editor-fold defaultstate="collapsed" desc="Attributes">
    private Note note;
    private NotesController controller;
    private CardLayout cardLayout;
    private JTextArea viewTextArea;
    private JTextArea editTextArea;
    //</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", initViewPanel());
        this.add("EditPanel", initEditPanel());

    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="EditPanel">
    private JPanel initEditPanel() {
        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(initCreatorLabel(), c);

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

        c.gridy++;
        c.anchor = GridBagConstraints.WEST;
        c.fill = GridBagConstraints.NONE;
        editPanel.add(initEditorLabel(), c);

        c.gridx++;
        c.anchor = GridBagConstraints.EAST;
        editPanel.add(initSaveButton(), c);




        return editPanel;
    }

    private JScrollPane initEditTextScroll() {
        this.editTextArea = new JTextArea(note.getContents());
        editTextArea.setLineWrap(true);
        editTextArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(editTextArea);
        scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
        Border b = scrollPane.getViewportBorder();

        MatteBorder mb = BorderFactory.createMatteBorder(2, 2, 2, 2, Color.BLUE);

        scrollPane.setBorder(mb);
        return scrollPane;
    }

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

        final NotesController c = this.controller;
        final Note n = this.note;
        final JTextArea noteText = this.viewTextArea;
        final JTextArea textToSubmit = this.editTextArea;
        ActionListener al = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //controller.saveNote(n);
                noteText.setText(textToSubmit.getText());
                l.next(p);
            }
        };

        JButton saveButton = new JButton("Save");
        saveButton.addActionListener(al);
        saveButton.setPreferredSize(new Dimension(62, 26));



        return saveButton;
    }

    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="ViewPanel">
    private JPanel initViewPanel() {
        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(initCreatorLabel(), c);

        c.gridy++;

        viewPanel.add(this.initNoteTextArea(), c);

        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.WEST;
        c.gridy++;
        viewPanel.add(initEditorLabel(), c);

        c.gridx++;
        c.anchor = GridBagConstraints.EAST;
        viewPanel.add(initEditButton(), c);

        return viewPanel;
    }

    private JLabel initCreatorLabel() {
        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 initNoteTextArea() {
        // -- Setup the notes area.
        this.viewTextArea = new JTextArea(note.getContents());
        viewTextArea.setEditable(false);
        viewTextArea.setLineWrap(true);
        viewTextArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(viewTextArea);
        scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);

        return scrollPane;
    }

    private JLabel initEditorLabel() {
        // -- 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 initEditButton() {
        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.setPreferredSize(new Dimension(62,26));
        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-11T16:22:44+00:00Added an answer on June 11, 2026 at 4:22 pm

    For manual control add a component listener to the parent component like this:

    MyParentComponent.addComponentListener(new java.awt.event.ComponentAdapter() {
        public void componentResized(java.awt.event.ComponentEvent evt) {
            ComponentResized(evt);
        }
    });
    

    Create a “ComponentResized” method and change the size to whatever you need like this:

    private void ComponentResized(ComponentEvent evt)
    {
        MyChildComponent.setSize(newWidth, newHeight);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this page that has some basic custom tabs: http://demo.unlockedmanagement.com/users/view/2 * NOTE
I have a basic web service that returns the following object as JSON: public
I have created a custom control (based on a panel) in wxPython that provides
I have two Tables: Table-1: notes Table-2: contacts My basic objective is to perform
I have a basic XML file that I load in my PHP using $xml
I have a small WPF application that writes some basic information to the registry
I have a parent class called Exam and that has many instances of a
I have such a basic problem in Delphi,I can't solve it. My Code: Note:DataR
I have basic Spring MVC 3 setup for i18n where I can show labels
I have basic idea on Kilo Virtual Machine on Mobiles , I have clear

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.