How do I ensure that a JPanel Shrinks when the parent frame is resized?

Posted by dah on Stack Overflow See other posts from Stack Overflow or by dah
Published on 2012-09-12T15:35:54Z Indexed on 2012/09/12 15:38 UTC
Read the original article Hit count: 280

Filed under:
|

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>
}

© Stack Overflow or respective owner

Related posts about java

Related posts about swing