'I don't know why the columns swapped in JTable
This question may seem silly to you, but pls help me. My program works, but slighty incorrect. In the image1 you can see the location of the data in the database. The image2 is the output of the table with values from the database. As you can see, during the output, the data of the 4th column moved to 7th, and all subsequent columns after the 4th moved to the left by one position. I've reviewed my code several times, but I can't see any errors. Pls explain the cause of the problem and its solution. Thanks in advance. image1 image2 That's my code Main.java
package com.company;
import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.*;
public class Main extends JFrame {
static Connection connection;
static Statement stat;
Main(String s){
super(s);
setSize(1000,600);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
try
{
stat.close();
connection.close();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
System.exit(0);
}
});
ConnectWithDB();
Data data = new Data();
JTable table = new JTable(data);
JScrollPane bar = new JScrollPane(table);
add(bar);
FillTableDB(data);
data.fireTableDataChanged();
}
private void ConnectWithDB(){
try {
connection = DriverManager.getConnection("jdbc:ucanaccess://D:/Java/Database/OlympiadJournal.accdb");
stat = connection.createStatement();
}
catch (Exception e){
e.printStackTrace();
}
}
private void FillTableDB(Data data){
data.rowData.clear();
try
{
ResultSet resultSet = stat.executeQuery("SELECT * FROM Table1");
while (resultSet.next())
{
String id = resultSet.getString(1);
String firstname = resultSet.getString(2);
String lastname = resultSet.getString(3);
String address = resultSet.getString(4);
String passcode = resultSet.getString(5);
String city = resultSet.getString(6);
String team = resultSet.getString(7);
Row r = new Row(id, firstname, lastname, address, passcode, city, team);
data.rowData.add(r);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
private void AddRecords(){
}
private void DeleteRecords(){
}
public static void main(String[] args) {
JFrame frame = new Main("Журнал олімпіади");
}
}
Row.java
package com.company;
public class Row {
String id;
String firstName;
String lastName;
String address;
String passcode;
String city;
String team;
Row (String Id, String FirstName, String LastName, String Address, String Passcode, String City, String Team){
this.id = Id;
this.firstName = FirstName;
this.lastName = LastName;
this.address = Address;
this.passcode = Passcode;
this.city = City;
this.team = Team;
}
}
Data.java
package com.company;
import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
public class Data extends AbstractTableModel {
ArrayList<Row> rowData = new ArrayList<>();
String[] columnNames= {"id", "first name", "last name", "address", "passcode", "city", "team"};
@Override
public int getRowCount() {
return rowData.size();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
if(rowIndex < 0 || rowIndex > rowData.size()){
return null;
}
Row tempRow = rowData.get(rowIndex);
switch (columnIndex){
case 0 -> {
return tempRow.id;
}
case 1 -> {
return tempRow.firstName;
}
case 2 -> {
return tempRow.lastName;
}
case 3 -> {
return tempRow.address;
}
case 4 -> {
return tempRow.passcode;
}
case 5 -> {
return tempRow.city;
}
case 6 -> {
return tempRow.team;
}
}
return "";
}
public String getColumnName(int columnIndex) {
return columnNames[columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex){
return false;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
