In the following code, I’m looking to have the panel that is within the NotesPanel to resize as the JFrame that holds the NotesPanel is resized.
Runnable CSSE:
package com.protocase.notes.views;
import com.protocase.notes.controller.NotesController;
import com.protocase.notes.model.Note;
import com.protocase.notes.model.User;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.Date;
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.border.BevelBorder;
/**
* @author dah01
*/
public class NotesPanel extends JPanel{
public static void main(String[] args) {
JFrame f= new JFrame();
f.setSize(500,500);
Note note = new Note();
User u = new User();
note.setCreator(u);
note.setLastEdited(u);
note.setDateCreated(new Date());
JPanel panel = new JPanel();
panel.add(new NotesPanel(note, null));
panel.add(new NotesPanel(note, null));
panel.setBackground(Color.red);
f.setContentPane(panel);
f.setVisible(true);
}
// <editor-fold defaultstate="collapsed" desc="Attributes">
private Note note;
private NotesController controller;
//</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){
// -- Setup the layout manager.
this.setBackground(new Color(199, 187, 192));
this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
this.setBorder(new BevelBorder(BevelBorder.RAISED));
// -- Setup the creator section.
JLabel creatorLabel = new JLabel("Note by "+note.getCreator()+ " @ "+note.getDateCreated());
creatorLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
// -- 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);
// -- Setup the edited by label.
JLabel editorLabel = new JLabel(" -- Last edited by "+note.getLastEdited() +" at "+note.getDateModified());
editorLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
// -- Add everything to the view.
Dimension wD = new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
scrollPane.setMaximumSize(wD);
this.setBackground(Color.yellow);
this.setMaximumSize(wD);
this.add(creatorLabel);
this.add(scrollPane);
this.add(editorLabel);
}
//</editor-fold>
}
EDIT: It is an issue with BoxLayout, setting the max size doesn’t seem to do anything beneficial.
I changed your code a bit to test it.
I can’t see where your problem is as everything is resizing properly.
Here are some screenshots how it looks for me in different window sizes:



What different do you want to achieve?