その中に JPanel を含むフレームがあり、その中に JScroll があり、フレームのサイズが変更されたときに、JScrollPane の幅がそれに合わせて大きくなればいいと思います。この機能は正常に動作しているように見えますが、サイズを小さくしても JScrollPane は縮小しません。
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>
}