Friday, April 28, 2017

[Solved] Set the row height and column width of JTable

import javax.swing.JTable;

public class TableTest {
      public static void main(String[] args) {
            JTable myTable = new JTable();

           //Set the height of  all rows
            myTable.setRowHeight(50);
           //myTable.setRowHeight(myTable.getRowHeight() + 10);

          //Set the height of one particular row.
          //Works only after calling the setModel method of JTable
          //Need to reset each time table data is modified
          //set the height of row 3 to 50
         MyTableModel model = new MyTableModel();
          myTable.setModel(model);
         myTable.setRowHeight(3, 50);
        //myTable.setRowHeight(3, myTable.getRowHeight(3) + 10);

        model.addTableModelListener(new TableModelListener() { 

             @Override 
            public void   tableChanged(final TableModelEvent e) { 
                  EventQueue.invokeLater(new Runnable() {  
                       @Override 
                       public void run() { 
                              myTable.setRowHeight(3, 50); 
                      } 
                }); 
             } 
         });

         //Set column width         
        TableColumnModel columnModel = myTable.getColumnModel();
         columnModel.getColumn(0).setPreferredWidth(200);
         columnModel.getColumn(1).setPreferredWidth(120);
         columnModel.getColumn(2).setPreferredWidth(150);
        columnModel.getColumn(3).setPreferredWidth(180);
    }
}

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment