Here's the code I have for my GUI
public class gui extends JFrame{
JFrame mainF;
JPanel listPanel;
JTextArea jtArea;
ButtonGroup buttons;
ButtonGroup restoreButtons;
JRadioButton createButton;
JRadioButton saveButton;
JRadioButton deleteButton;
JRadioButton redo;
JRadioButton undo;
gui() {
this.mainF = new JFrame();
mainF.getContentPane().setBackground(Color.gray);
mainF.setLayout(null);
mainF.setSize(800, 800);
createButton = new JRadioButton("Create note");
createButton.setBounds(10, 150, 150, 50);
saveButton = new JRadioButton("Save note");
saveButton.setBounds(10, 250, 150, 50);
saveButton.setEnabled(false);
redo = new JRadioButton("Redo");
redo.setBounds(10, 400, 150, 50);
redo.setEnabled(false);
undo = new JRadioButton("Undo");
undo.setBounds(10, 350, 150, 50);
undo.setEnabled(false);
deleteButton = new JRadioButton("Delete note");
deleteButton.setBounds(10, 500, 150, 50);
deleteButton.setEnabled(false);
this.jtArea = new JTextArea();
int centerX = (mainF.getWidth() - 200) / 2;
int centerY = (mainF.getHeight() - 500) / 2;
jtArea.setBounds(centerX, centerY, 200, 500);
jtArea.setBackground(Color.white);
jtArea.setEnabled(false);
JScrollPane scrollPane = new JScrollPane(jtArea);
scrollPane.setBounds(centerX, centerY, 200, 500);
jtArea.setLineWrap(true);
jtArea.setWrapStyleWord(true);
jtArea.setEnabled(false);
this.listPanel = new JPanel(new GridLayout(0, 1));
JScrollPane listScrollPane = new JScrollPane(listPanel);
listScrollPane.setBounds(centerX + 285, centerY, 200, 500);
listScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
buttons = new ButtonGroup();
buttons.add(createButton);
buttons.add(saveButton);
buttons.add(deleteButton);
buttons.add(undo);
buttons.add(redo);
mainF.add(jtArea);
mainF.add(createButton);
mainF.add(saveButton);
mainF.add(deleteButton);
mainF.add(redo);
mainF.add(undo);
mainF.add(listScrollPane);
mainF.setVisible(true);
mainF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
I'm still unfamiliar with GUI components so this is very new to me. It shows a scroll bar on the right side, but I can't scroll through it when it's full. My entire code is supposed to add buttons to that listPanel and if it gets full then I can scroll through them but that doesn't seem to be the case.
mainF.setLayout(null);is your first mistake.is your second. Which
JFrameare you actually using?! As a general rule avoid extending from top level containers likeJFrame, you're not adding any additional functionally to the class andJFrameis a complex compound component, best to just avoid it.is your third mistake (credit to camickr) - you're applying the
JTextAreato theJScrollPane, which is correct, but then adding theJTextAreato theJFrame, which will first remove it from theJScrollPane... and now theJScrollPaneisn't even been displayed. You should be adding thescrollPaneto the frame, not theJTextArea.Remember, you're not stuck to using a single layout manager, you can make use of multiple layout managers through a process of "compound layouts", for example...
Please note - the above screen shot shows preloaded text, which is not present in the example code
I would recommend taking a look at: