Reputation: 43
I'm making a program that uses lots of different compenents, so I've started using a GridBagLayout for it. It's working great, but when I run the program I get this:
It should be putting the right-most list object directly up against the rest of the objects, and filling the entire width of the screen. Not sure why it isn't. Here's a short copy of the relevant code (I put them all into one class for simplicity should you decide to run it.):
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Example extends JFrame{
private Character me;
public static void main(String[] args){
new Example();
}
public Example(){
add(new CharacterDisplay(new Character()));
setSize(600,500);
setVisible(true);
}
private class Character{
public ArrayList<String> notes = new ArrayList<String>();
public Character(){
notes.add("A");
notes.add("few");
notes.add("notes");
}
}
private class CharacterDisplay extends JPanel{
public CharacterDisplay(Character myself){
me = myself;
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JTextArea filler = new JTextArea();
c.gridx = 0;
c.weightx = 1;
c.weighty = 1;
c.gridheight = 11;
c.gridwidth = 6;
c.fill = GridBagConstraints.BOTH;
add(filler,c);
c.gridy = 0;
c.gridx = 6;
c.weightx = 1;
c.weighty = 1;
c.gridheight = 11;
c.gridwidth = 3;
c.fill = GridBagConstraints.BOTH;
add(new NoteBox(), c);
}
}
private class NoteBox extends JPanel{
private int currentNotes = 0;
private JList<String> listContainer;
private JTextField addNoteField = new JTextField();
private JButton removalButton = new JButton("Remove Selected Notes");
private JScrollPane listScroll;
public NoteBox(){
setLayout(new GridBagLayout());
addNoteField.setText("Add a note here");
addNoteField.addActionListener(e -> {
me.notes.add(addNoteField.getText());
repaint();});
String[] model = new String[me.notes.size()];
model = me.notes.toArray(model);
listContainer = new JList<String>(model);
removalButton.addActionListener(e -> remove(listContainer.getSelectedIndices()));
listScroll = new JScrollPane(listContainer);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.gridwidth = 3;
c.weighty = 0;
add(addNoteField, c);
c.weighty = 1;
c.gridy = 1;
c.gridheight = 9;
add(listScroll, c);
c.weighty = 0;
c.gridy = 10;
add(removalButton, c);
}
public void remove(int[] indices){
//Go backward to avoid shifting indices of the next ones to remove;
for(int i = indices.length - 1; i >= 0; i--){
me.notes.remove(indices[i]);
}
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
String[] model = new String[me.notes.size()];
model = me.notes.toArray(model);
listContainer.setListData(model);
}
}
}
I included all of the NoteBox class because it's the one that's malfunctioning and I figured it was the most relevant.
Upvotes: 1
Views: 909
Reputation: 43
I simply had to put weightx = 1 within my NoteBox inner class, as suggested by MadProgrammer. This fixed the issue entirely, I'm just writing this to formally mark the answer as solved.
Upvotes: 1