我对JTable有疑问。这段代码混合了java教程。我只想快速做到。我需要在点击菜单操作后绘制一个样本表。例如,此命令是在命中file->之后的paiting表。加载。问题是:在按下动作并调整窗口大小后,表格正在绘制。它需要工作而不触及窗口的大小。可能我需要对某些组件进行一些刷新/重绘。我尝试了几种方法(在很多摆动对象上调用.repaint()),但我没有找到任何解决方案。谢谢你的帮助。
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);
}
}
}
答案 0 :(得分:0)
添加JTable
组件
container.revalidate();
container.repaint();