为什么 JTable 不能正确调整大小?
Why isn't the JTable resizing properly?
为什么 JTable 的大小调整不正确?
您好,感谢您花时间解决我的问题。首先让我向您介绍一下我的dummy-/training-project。下面列出的 classes 应该代表 MVC 模型(模型、视图、控制器)之后的程序。当 运行 主 class 文件选择器打开时,您可以从中 select 一个 .csv 文件,其中包含保存为 String[][] 的信息。然后,此 String[][] 在 view-class 中可视化为 JTable。此 JTable 是带有 BorderLayout.CENTER 的 JFrame 内的 JPanel 的一部分。现在问题来了,如果我重新调整 JFrame,为什么我的 JTable 无法正确调整大小?我在互联网上搜索甚至 this.table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
根本不会影响调整大小。我希望你能以某种方式帮助我。谢谢!
MVC-classes + testclass
型号
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
/**
* This class is responsible for the actual data being "processed".
* It saves the Strings out of a File into a String[][] and further more updates its content,
* if changed by the View-Class.
*
* @author CodeScrub
* @version 2022-01-13
*/
public class Model {
private File selectedFile;
private String[][] dataSetTotal;
private ArrayList<String> dataSetList;
private ArrayList<String> tableDatasetList;
public Model() {
dataSetList = new ArrayList<String>();
tableDatasetList = new ArrayList<String>();
}
public void evaluateData() {
try {
Scanner scanner = new Scanner(this.selectedFile);
while(scanner.hasNextLine()) {
String data = scanner.nextLine();
dataSetList.add(data);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
this.dataSetTotal = new String[dataSetList.size()][3];
for(String info : dataSetList) {
String[] dataSetSeperate = info.split(";");
for(int i = 0; i < dataSetSeperate.length; i++) {
dataSetTotal[dataSetList.indexOf(info)][i] = dataSetSeperate[i];
}
}
}
public void setSelectedFile(File selectedFile) {
this.selectedFile = selectedFile;
}
public File getSelectedFile() {
return this.selectedFile;
}
public String[][] getDataSetTotal(){
return this.dataSetTotal;
}
public void updateData(String[][] tableContent) {
for(int i = 0; i < tableContent.length; i++) {
String dataSetSeperate = "";
for(int j = 0; j < tableContent[i].length; j++) {
if(j < 2) {
dataSetSeperate = dataSetSeperate + tableContent[i][j] + ";";
}else {
dataSetSeperate = dataSetSeperate + tableContent[i][j];
}
}
this.tableDatasetList.add(dataSetSeperate);
}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(this.selectedFile));
for(String info : this.tableDatasetList){
writer.write(info + "\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
this.tableDatasetList = new ArrayList<String>();
}
}
查看
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
* This class visualizes the processed data inside a JFrame.
* The data sets are visualized as a JTable.
*
* @author CodeScrub
* @version 2022-01-13
*/
public class View extends JFrame{
private static final long serialVersionUID = 1L;
private JFileChooser chooser;
private FileNameExtensionFilter filter;
private JLabel firstName;
private JLabel lastName;
private JLabel socialSecurityNumber;
private JButton buttonSafeChanges;
private JScrollPane scrollPane;
private JTable table;
private JPanel centerPanel;
private Controller controller;
public View(Controller controller) {
this.controller = controller;
init();
}
private void init() {
this.chooser = new JFileChooser();
this.filter = new FileNameExtensionFilter(
"CSV Files", "csv");
this.chooser.setFileFilter(filter);
this.chooser.setPreferredSize(new Dimension(800,500));
int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
this.controller.updateData(chooser.getSelectedFile());
this.controller.evaluateData();
this.firstName = new JLabel("First Name");
this.lastName = new JLabel("Last Name");
this.socialSecurityNumber = new JLabel("Social Security Number");
this.centerPanel = new JPanel(new BorderLayout());
String[] title = {firstName.getText() , lastName.getText() , socialSecurityNumber.getText()};
this.table = new JTable(this.controller.getDataSetTotal(), title);
this.table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
this.centerPanel.add(table, BorderLayout.CENTER);
this.scrollPane = new JScrollPane(this.table);
this.centerPanel.add(scrollPane, BorderLayout.EAST);
this.buttonSafeChanges = new JButton("Safe Changes");
this.buttonSafeChanges.addActionListener(controller);
this.centerPanel.add(table.getTableHeader(), BorderLayout.NORTH);
this.add(centerPanel,BorderLayout.CENTER);
this.add(buttonSafeChanges, BorderLayout.SOUTH);
this.setTitle("MVC");
this.setSize(475,350);
this.setMinimumSize(new Dimension(475,350));
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
getTableContent();
}else {
System.exit(0);
}
}
public JButton getButtonSafeChanges() {
return this.buttonSafeChanges;
}
public String[][] getTableContent() {
String[][] tableContent;
tableContent = new String[this.table.getRowCount()][this.table.getColumnCount()];
for(int i = 0; i < table.getRowCount(); i++) {
for(int j = 0; j < table.getColumnCount(); j++) {
tableContent[i][j] = table.getValueAt(i,j).toString();
}
}
return tableContent;
}
}
控制器
import java.io.File;
/**
* This class works as an interface between the view- and model-class
* and also handles the action performed after the button press "Safe Changes".
*
* @author CodeScrub
* @version 2022-01-13
*/
public class Controller implements ActionListener{
private View view;
private Model model;
public Controller() {
this.model = new Model();
this.view = new View(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == this.view.getButtonSafeChanges()) {
this.model.updateData(this.view.getTableContent());
}
}
public void updateData(File selectedFile) {
this.model.setSelectedFile(selectedFile);
}
public File getSelectedFile() {
return this.model.getSelectedFile();
}
public void evaluateData() {
this.model.evaluateData();
}
public String[][] getDataSetTotal(){
return this.model.getDataSetTotal();
}
}
测试-class:
public class MVC_Testclass {
public static void main(String[] args) {
Controller controller = new Controller();
}
}
基本上,当您为 JTable 使用 JScrollPane 容器时,您只需在 centerPanel
中添加 scrollPane
,并确保在 CENTER
中添加它。下面解决了您的代码中的问题。
this.table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
//this.centerPanel.add(table, BorderLayout.CENTER);
this.scrollPane = new JScrollPane(this.table);
this.centerPanel.add(scrollPane, BorderLayout.CENTER);
为什么 JTable 的大小调整不正确?
您好,感谢您花时间解决我的问题。首先让我向您介绍一下我的dummy-/training-project。下面列出的 classes 应该代表 MVC 模型(模型、视图、控制器)之后的程序。当 运行 主 class 文件选择器打开时,您可以从中 select 一个 .csv 文件,其中包含保存为 String[][] 的信息。然后,此 String[][] 在 view-class 中可视化为 JTable。此 JTable 是带有 BorderLayout.CENTER 的 JFrame 内的 JPanel 的一部分。现在问题来了,如果我重新调整 JFrame,为什么我的 JTable 无法正确调整大小?我在互联网上搜索甚至 this.table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
根本不会影响调整大小。我希望你能以某种方式帮助我。谢谢!
MVC-classes + testclass
型号
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
/**
* This class is responsible for the actual data being "processed".
* It saves the Strings out of a File into a String[][] and further more updates its content,
* if changed by the View-Class.
*
* @author CodeScrub
* @version 2022-01-13
*/
public class Model {
private File selectedFile;
private String[][] dataSetTotal;
private ArrayList<String> dataSetList;
private ArrayList<String> tableDatasetList;
public Model() {
dataSetList = new ArrayList<String>();
tableDatasetList = new ArrayList<String>();
}
public void evaluateData() {
try {
Scanner scanner = new Scanner(this.selectedFile);
while(scanner.hasNextLine()) {
String data = scanner.nextLine();
dataSetList.add(data);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
this.dataSetTotal = new String[dataSetList.size()][3];
for(String info : dataSetList) {
String[] dataSetSeperate = info.split(";");
for(int i = 0; i < dataSetSeperate.length; i++) {
dataSetTotal[dataSetList.indexOf(info)][i] = dataSetSeperate[i];
}
}
}
public void setSelectedFile(File selectedFile) {
this.selectedFile = selectedFile;
}
public File getSelectedFile() {
return this.selectedFile;
}
public String[][] getDataSetTotal(){
return this.dataSetTotal;
}
public void updateData(String[][] tableContent) {
for(int i = 0; i < tableContent.length; i++) {
String dataSetSeperate = "";
for(int j = 0; j < tableContent[i].length; j++) {
if(j < 2) {
dataSetSeperate = dataSetSeperate + tableContent[i][j] + ";";
}else {
dataSetSeperate = dataSetSeperate + tableContent[i][j];
}
}
this.tableDatasetList.add(dataSetSeperate);
}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(this.selectedFile));
for(String info : this.tableDatasetList){
writer.write(info + "\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
this.tableDatasetList = new ArrayList<String>();
}
}
查看
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
* This class visualizes the processed data inside a JFrame.
* The data sets are visualized as a JTable.
*
* @author CodeScrub
* @version 2022-01-13
*/
public class View extends JFrame{
private static final long serialVersionUID = 1L;
private JFileChooser chooser;
private FileNameExtensionFilter filter;
private JLabel firstName;
private JLabel lastName;
private JLabel socialSecurityNumber;
private JButton buttonSafeChanges;
private JScrollPane scrollPane;
private JTable table;
private JPanel centerPanel;
private Controller controller;
public View(Controller controller) {
this.controller = controller;
init();
}
private void init() {
this.chooser = new JFileChooser();
this.filter = new FileNameExtensionFilter(
"CSV Files", "csv");
this.chooser.setFileFilter(filter);
this.chooser.setPreferredSize(new Dimension(800,500));
int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
this.controller.updateData(chooser.getSelectedFile());
this.controller.evaluateData();
this.firstName = new JLabel("First Name");
this.lastName = new JLabel("Last Name");
this.socialSecurityNumber = new JLabel("Social Security Number");
this.centerPanel = new JPanel(new BorderLayout());
String[] title = {firstName.getText() , lastName.getText() , socialSecurityNumber.getText()};
this.table = new JTable(this.controller.getDataSetTotal(), title);
this.table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
this.centerPanel.add(table, BorderLayout.CENTER);
this.scrollPane = new JScrollPane(this.table);
this.centerPanel.add(scrollPane, BorderLayout.EAST);
this.buttonSafeChanges = new JButton("Safe Changes");
this.buttonSafeChanges.addActionListener(controller);
this.centerPanel.add(table.getTableHeader(), BorderLayout.NORTH);
this.add(centerPanel,BorderLayout.CENTER);
this.add(buttonSafeChanges, BorderLayout.SOUTH);
this.setTitle("MVC");
this.setSize(475,350);
this.setMinimumSize(new Dimension(475,350));
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
getTableContent();
}else {
System.exit(0);
}
}
public JButton getButtonSafeChanges() {
return this.buttonSafeChanges;
}
public String[][] getTableContent() {
String[][] tableContent;
tableContent = new String[this.table.getRowCount()][this.table.getColumnCount()];
for(int i = 0; i < table.getRowCount(); i++) {
for(int j = 0; j < table.getColumnCount(); j++) {
tableContent[i][j] = table.getValueAt(i,j).toString();
}
}
return tableContent;
}
}
控制器
import java.io.File;
/**
* This class works as an interface between the view- and model-class
* and also handles the action performed after the button press "Safe Changes".
*
* @author CodeScrub
* @version 2022-01-13
*/
public class Controller implements ActionListener{
private View view;
private Model model;
public Controller() {
this.model = new Model();
this.view = new View(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == this.view.getButtonSafeChanges()) {
this.model.updateData(this.view.getTableContent());
}
}
public void updateData(File selectedFile) {
this.model.setSelectedFile(selectedFile);
}
public File getSelectedFile() {
return this.model.getSelectedFile();
}
public void evaluateData() {
this.model.evaluateData();
}
public String[][] getDataSetTotal(){
return this.model.getDataSetTotal();
}
}
测试-class:
public class MVC_Testclass {
public static void main(String[] args) {
Controller controller = new Controller();
}
}
基本上,当您为 JTable 使用 JScrollPane 容器时,您只需在 centerPanel
中添加 scrollPane
,并确保在 CENTER
中添加它。下面解决了您的代码中的问题。
this.table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
//this.centerPanel.add(table, BorderLayout.CENTER);
this.scrollPane = new JScrollPane(this.table);
this.centerPanel.add(scrollPane, BorderLayout.CENTER);