Hello and thanks for taking the time to deal with my problem. First let me introduce you to my dummy-/training-project. The classes listed below should represent a programm after the MVC-model (Model, View, Controller). When running the main class a FileChooser opens up, from which you're able to select a .csv-File that contains information that is saved as a String[][]. This String[][] is then visualized in the view-class as a JTable. This JTable is part of a JPanel inside a JFrame with the BorderLayout.CENTER. Now to the question, why wont my JTable resize properly if i am rezising the JFrame? I searched on the internet and even this.table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
will not effect the resizing at all. I hope you can help me somehow. Thanks!
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();
}
}
public class MVC_Testclass {
public static void main(String[] args) {
Controller controller = new Controller();
}
}
Basically, as you are using JScrollPane container for your JTable, you only need to add the scrollPane
in your centerPanel
, and make sure to add it in the CENTER
. The below resolves the issue in your code.
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);