1

I have retrieved data from SQL Database into a JTable. I want to make the table size to be automatically the size of the rows. It would be plus if I can also make the data in the rows centered.

I am fairly new to GUI Java Programming. Can someone please let me understand how it can be done?

private  void DisplayOrder() {
    String qry = "SELECT * FROM SALESORDER"; //Creating Query
    try {
        conn = DriverManager.getConnection(connectionUrl, username, Pass);
        Statement st = conn.prepareStatement(qry);
        ResultSet rs = st.executeQuery(qry);
        
        while (rs.next()){
            String Des   = rs.getString("ProductDescription");
            String qty   = String.valueOf(rs.getInt("Quantity"));
            String price = String.valueOf(rs.getInt("TotalPrice"));
            String tbdata[] = {Des, qty, price};
            DefaultTableModel model = (DefaultTableModel) Ordertable.getModel();
            model.addRow(new Object[]{Des, qty, price});
        }
    } catch (SQLException e){
        
    } finally{
        Ordertable.getTableHeader().setFont(new Font("Segoe UI",Font.BOLD,15));
        Ordertable.getTableHeader().setOpaque(false);
        Ordertable.getTableHeader().setBackground(new Color(32,136,203));
        Ordertable.getTableHeader().setForeground(new Color(255,255,255));
        Ordertable.setRowHeight(25);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    You want the number of rows in `jTable` equal to the number of rows in `SQL Database`? If so then just the `model.addRow(...)` should do the work. So, what exactly is the problem? Can you please explain further? – Mohammed Julfikar Ali Mahbub Nov 17 '20 at 20:44
  • 1
    Hi Ryan, I am not sure what you want. But I am assuming your table is not displayed. One thing I did notice is that you create the table in each iteration. So you call DefaultTableModel model=(DefaultTableModel)Ordertable.getModel(); for every row in the ResultSet. Start by creating the DefaultTable above the while-loop. – kwkw Nov 17 '20 at 20:45
  • 1
    @Wkrem *One thing I did notice is that you create the table in each iteration.* - the table is not being created. The code is just referencing the current model from the existing table so a new row of data can be added. – camickr Nov 18 '20 at 00:30
  • 1
    1) Variable names should NOT start with an upper case character. 2) What is the point of making the table header transparent and then attempting to set the background color? 3) Why do you set properties of the header in a finally block? You should set the properties when you create the table. The properties of the header have nothing to do with reading the data from the database. 4) *i want to make the jtable size to be automatically the size of the rows?* - what size do you see now. Looks to me like the logic is adding all rows of data to the model. – camickr Nov 18 '20 at 00:32
  • I just wanted to make make my table size same as the row size ? i am fairly new here so please excuse my mistakes. for example , if 3 rows then it should be the maximum, how can i obtain that ? – Ryan Anjalo Nov 18 '20 at 09:56
  • @camickr thanks for pointing it out , i should just create a another method for the current table formating , As far i knew variables should be uppercase when starting , i will see into that thanks. – Ryan Anjalo Nov 18 '20 at 09:59
  • *As far i knew variables should be uppercase when starting* 1) Comment doesn't make sense. Look at your code. Majority of the variables are correct, only some are incorrect. Be conssistent!!! 2) Any text book or online tutorial will follow Java conventions, so learn by example. *I just wanted to make make my table size same as the row size ?* - that does not clarify anything. What is the "row size"? What do you see in the table? What do you expect to see? – camickr Nov 18 '20 at 16:25

0 Answers0