The container needs to be revalidated and repainted after adding the JTable
component
container.revalidate();
container.repaint();
Question
I have a question regarding JTable. This code was mixed of java tutorials. I just wanted to make it quick. I need to draw a sample table after hitting menu action. For example this code is paiting table after hitting file-> load. Problem is: table is drawing after hitting action and resizing window. It needs to work without touching size of window. Probably I would need some refresh/repaint on some components. I tried few ways (invoke .repaint() on many swing objects) but I didn't find any solution. Thanks for any help.
package pl.edu.pb.szkszym.view;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.AbstractTableModel;
import javax.swing.JFileChooser;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
public class Start extends JFrame {
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Start frame = new Start();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Start() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenuItem mntmLoad = new JMenuItem("Load");
mntmLoad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser fileopen = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("c files", "c");
fileopen.addChoosableFileFilter(filter);
int ret = fileopen.showDialog(null, "Open file");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fileopen.getSelectedFile();
System.out.println(file);
table = new JTable(new MyTableModel(new String[] { "col1",
"col2" }, new Object[][] {
{ "Kathy", new Integer(5) },
{ "John", "Doe" },
{ new Integer(2),
new Boolean(false) } }));
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(table.getTableHeader(), BorderLayout.PAGE_START);
container.add(table, BorderLayout.CENTER);
}
}
});
mnFile.add(mntmLoad);
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames;
private Object[][] data;
public MyTableModel(String[] columns, Object[][] data) {
this.columnNames = columns;
this.data = data;
}
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();
}
/*
* Don't need to implement this method unless your table's editable.
*/
public boolean isCellEditable(int row, int col) {
// Note that the data/cell address is constant,
// no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}
/*
* Don't need to implement this method unless your table's data can
* change.
*/
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}
}
Solution
The container needs to be revalidated and repainted after adding the JTable
component
container.revalidate();
container.repaint();