Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

java - Scrollbar for JTable

so I have actually couple of issues where I could need some help.

1.) Actually I can display the SQL Result into the jList, that works so far. What I am actually missing are the names of the Columns. It just shows me the MySQL Entries.

Database Result in jList

2.) The second issue is, that my Database actually includes more Information. I want to show that in the jList also but to prevent having a huge window I want to add a horizontal and vertical scrollbar.

I tried adding one in but whenever I did that the list suddenly disappeared. how would adding a scrollbar to the list actually look like ?

The next steps of my Java program would be MySQL Operations. I want to change Values of the received dataset and send it back. I think I will do that with some kind of getSelectedRow right ?

Thank you very much for your help

public class aPanel extends JFrame{

    private JTable jt;
    

    
    public aPanel() {
        
        getContentPane().setLayout(null);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(0, 0, 780, 450);
        JPanel contentPane = new JPanel();
        contentPane.setBackground(new Color (51,51,51));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        JPanel leftTitle = new JPanel();
        leftTitle.setBackground(new Color (51,153,255));
        leftTitle.setBounds(0, 0, 230, 450);
        contentPane.add(leftTitle);
        leftTitle.setLayout(null);

        String[] columnNames = {"ID","First_Name","Last_Name"};
        DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);



        try {
            // Establishment of JDBC Connection
            String url = "jdbc:mysql://localhost:3306/eHealthDB?serverTimezone=UTC";
            Connection con = DriverManager.getConnection(url,"root","root");
            
            String sql = "SELECT ID,First_Name,Last_Name FROM patient;";
            
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            
            while(rs.next() ) 
            {
                String pID = rs.getString("ID");    
                String vName = rs.getString("First_Name");
                String nName = rs.getString("Last_Name");   

                
                String[] data = {pID,vName,nName};
                tableModel.addRow(data);
                
            }
            

       
            jt = new JTable();
            jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            jt.setCellSelectionEnabled(false);
            jt.setRowSelectionAllowed(true);
            jt.setBorder(new LineBorder(Color.WHITE));
            jt.setForeground(Color.WHITE);
            jt.setBackground(new Color (59,59,59));
            jt.setBounds(370, 52, 251,167);
            contentPane.add(jt);
            jt.setModel(tableModel);
            
            
        }  catch (Exception e1) {
            System.out.println(e1);
        }
        
    }
}
question from:https://stackoverflow.com/questions/65873032/scrollbar-for-jtable

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. Don't use a null layout (get rid of that code)
  2. Don't use setBounds(...) (get rid of that code)

Swing was designed to be used with layout managers. The layout manager will give the component a size/location based on the rules of the layout manager.

What I am actually missing are the names of the Columns.

In order for the columns names to appear the table must be added to the viewport of a JScrollPane and the scroll pane added to the frame.

So the basic change would be:

//contentPane.setLayout(null);
contentPane.setLayout(new BorderLayout());

and

//contentPane.add(jt);
contentPane.add(new JScrollPane(jt), BorderLayout.CENTER);

This will allow the table to fill the available space in frame. Scrollbars will appear as required. Read the section from the Swing tutorial on Layout Managers for more information and working examples.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...