EDIT:
As suggested by camickr, I added the JTable to a scrollpane. It seems that he/she was correct that the layout manager was altering the size of the columns.
I have a JTable with 50 rows and 40 columns. Each cell displays either nothing, or a single character.
I am trying to make each cell square by setting the row height and column width to the same value.
public IFace() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 966, 740);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
mntmFile = new JMenuItem("File");
menuBar.add(mntmFile);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new MigLayout("", "[457.00,grow][grow]", "[][grow][center][][grow]"));
JToolBar toolBar = new JToolBar();
contentPane.add(toolBar, "cell 0 0");
lblPalette = new JLabel("Palette");
contentPane.add(lblPalette, "cell 1 0");
AsciiTableModel myTableModel = new AsciiTableModel();
tableASCII = new JTable(myTableModel);
tableASCII.getSelectionModel();
tableASCII.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
tableASCII.setCellSelectionEnabled(true);
tableASCII.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
System.out.println(tableASCII.getColumnModel().getColumn(0).getMaxWidth());
final int cellSize = 20;
for(int i = 0; i < tableASCII.getColumnCount(); i++){
tableASCII.getColumnModel().getColumn(i).setPreferredWidth(cellSize);
System.out.println(tableASCII.getColumnModel().getColumn(i).getWidth());
}
for(int i = 0; i < tableASCII.getRowCount(); i++){
tableASCII.setRowHeight(i, cellSize );
}
contentPane.add(tableASCII, "cell 0 1,span 0 5");
btnFill = new JButton("Fill");
contentPane.add(btnFill, "flowx,cell 1 2");
textFill = new JTextField();
textFill.setHorizontalAlignment(SwingConstants.CENTER);
contentPane.add(textFill, "cell 1 2");
textFill.setColumns(10);
}
After calling setPreferredWidth(cellSize)
, the output of System.out.println(tableASCII.getColumnModel().getColumn(i).getWidth());
is 75
. It should be 20.
This is the code for the tablemodel that tableASCII uses.
public class AsciiTableModel extends AbstractTableModel {
public final int NUM_CHARACTERS_PER_CELL = 1;
private int numColumns = 50;
private int numRows = 40;
private List<ArrayList<Character>> asciiChars = new ArrayList<ArrayList<Character>>();
public AsciiTableModel() {
createListElements();
System.out.println(asciiChars.get(0).size());
asciiChars.get(2).set(0, 'C');
asciiChars.get(1).set(0, 'a');
asciiChars.get(1).set(1, 't');
}
public AsciiTableModel(int rows, int columns) {
numColumns = columns;
numRows = rows;
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return numColumns;
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return numRows;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return asciiChars.get(rowIndex).get(columnIndex);
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if(aValue != null) {
//System.out.println(aValue.toString());
asciiChars.get(rowIndex).set(columnIndex, aValue.toString().charAt(1));
//System.out.println(asciiChars.get(rowIndex).get(columnIndex));
}
}
void createListElements() {
for(int i = 0; i < numRows; i++) {
asciiChars.add(new ArrayList<Character>());
for(int j = 0; j < numColumns; j++) {
asciiChars.get(i).add(' ');
}
}
}