Reputation: 39
I'm having trouble getting my GUI to do what I want it to. This is my first time using Swing, so I apologize in advance if this a dumb question. Much of my code is also copy/pasted from the samples at oracle.com, but I think I have a feel for what it's all doing.
In my Swing GUI, I currently have two elements: a JTable and a a JComboBox (which I believe is encompassed by a JScrollPanel). The way I want it formatted is with the ComboBox above the Table, but I can only seem to get it to go either to the left or right of it. I thought that I would be able to use this line:
add(charList, BorderLayout.LINE_START);
where "charList" is my ComboBox, but it doesn't seem to affect anything. No matter what I set it or the other element to, it still stays side to side.
Here is my complete code, all in one file:
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.AbstractTableModel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
public class MeleeEdit extends JPanel {
private boolean DEBUG = false;
public String[] attributes = {
"Initial Walk Velocity", "Walk Acceleration?", "Walk Maximum Velocity", "Slow Walk Max?"
};
public String[] description = {
"N/A",
"N/A, bruh",
"N/A, BRUH",
"N/A, BRUH!"
};
public MeleeEdit() {
super(new GridLayout(1,0));
String[] characters = { "Captain Falcon", "Young Link", "Donkey Kong", "Doctor Mario", "Falco" };
JComboBox charList = new JComboBox(characters);
charList.setSelectedIndex(0);
//petList.addActionListener(this);
JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 600));
table.setFillsViewportHeight(true);
table.add(Box.createHorizontalStrut(5));
table.add(new JSeparator(SwingConstants.VERTICAL));
table.add(Box.createHorizontalStrut(5));
table.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//scrollPane.setLayout(BorderLayout.CENTER);
//Add the scroll pane to this panel.
add(charList, BorderLayout.LINE_START);
add(scrollPane, BorderLayout.CENTER);
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"Attribute",
"Value",
"Info",
};
private Object[][] data = initGrid();
public Object[][] initGrid(){
Object[][] tmp = new Object[3][3];
for(int i = 0; i < 3; i ++){
tmp[i][0] = attributes[i];
tmp[i][1] = new Float(4.5);
tmp[i][2] = description[i];
}
return tmp;
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if(col==1)
return true;
else
return false;
}
public void setValueAt(Object value, int row, int col) {
if(value==null)
return;
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Melee Character Attribute Editor v0.1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
MeleeEdit newContentPane = new MeleeEdit();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
I can't seem to figure out why it isn't working. Any suggestions?
Upvotes: 3
Views: 147
Reputation: 347194
super(new GridLayout(1,0));
to super(new BorderLayout());
add(charList, BorderLayout.LINE_START);
to add(charList, BorderLayout.PAGE_START);
(personally, I prefer BorderLayout.NORTH
, but I'm old school like that)Take a look at Laying Out Components Within a Container and How to Use Borders for more details
Upvotes: 2