Question

I'm new to Java and trying to make a simple program that will display the data inside the database. My problem here is I have 20 columns and I can't see the data or even the column title because it is too crowded. I tried combine it with JScrollPane and still doesn't work.

Here is my code

import java.sql.*;
import javax.swing.*;
import net.proteanit.sql.DbUtils;

public class ManageDatabase extends javax.swing.JFrame {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;


/**
 * Creates new form ManageDatabase
 */
public ManageDatabase() {
    initComponents();
    conn=javaconnect.ConnectDb();
    Update_table();        
}
private void Update_table(){

    try {
    String sql = "Select * from table_AOD_Pump_Accessories";
    pst=conn.prepareStatement(sql);
    rs=pst.executeQuery();

    Table_AOD_Pump_Accessories.setModel(DbUtils.resultSetToTableModel(rs));

    }
    catch(Exception e){
    JOptionPane.showMessageDialog(null, e);

    }  
}
Was it helpful?

Solution

Use JScrollPane and add the JTable in it.

JScrollPane scrollPane = new JScrollPane(table);

Try this one also if you want to show the scroll bar as per your requirement.

JScrollPane scrollPaneChatHistory = new JScrollPane(table, 
            JScrollPane.VERTICAL_SCROLLBAR_NEVER,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

--EDIT--

Try

table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

For more info have a look at Auto resizing the JTable column widths.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top