0
String[] columnNames = {"Sr. No.", "Subject", "Marks","Total","Grade"};


DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(columnNames);

JTable table= new JTable();
table.setModel(model); 
DefaultTableCellRenderer r = new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) 
{
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
return this;
    }
        };
 TableColumn column = null;

        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
        tcr.setHorizontalAlignment(JLabel.CENTER);

        DefaultTableCellRenderer defaultRenderer = (DefaultTableCellRenderer) table.getDefaultRenderer(Object.class);
        defaultRenderer.setHorizontalAlignment(JLabel.CENTER);
for (int k = 0; k <3; k++)
 {
            column = table.getColumnModel().getColumn(k);
            if (k == 0) 
{
                column.setPreferredWidth(100);
            } 
else {
                if (k == 1)
 {
                    column.setPreferredWidth(100);
                }
 else
 {
                    column.setPreferredWidth(300);
                }
            }
        }
        table.setRowHeight(table.getRowHeight() + 15);
        table.setModel(model);

table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setFillsViewportHeight(true);
JScrollPane scroll = new JScrollPane(table);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
scroll.setBounds(330,330,900,350);
frame.add(scroll);
String newline = "\n";
String asd;
String asd1;
String asd2;
String asd3;
String head1=main_page.heading();
String head2=main_page.heading();
int i=0; int a=0,as;
try
{
 Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/school1", "root","sits");
            Statement s = con.createStatement();
ResultSet rs = s.executeQuery("select * from marksheet where roll=10");     
                     while(rs.next())
 {
 asd=rs.getString("sub");
          asd1=rs.getString("marks");
          asd2=rs.getString("total");
          asd3=rs.getString("grade");
                         a++;
        i++; 
model.addRow(new Object[]{a,asd, asd1,asd2,asd3});
          } 
if(!rs.next())
model.addRow(new Object[] {"Total Marks Obtained"});//here i need to merge the last two cells of column1 and column2 of last row.

How do i merge the two cells of column1 and column2 of last row? As any number of records are displayed on DefaultTableModel automatically from the database, how will i get the current cell to be merged? The index of row and column will be based on the database record's count. Please help me with this.

  • This link will help you: https://stackoverflow.com/questions/21856733/how-to-merge-cell-in-defaulttablemodel-jtable – Aman Sep 21 '17 at 11:47
  • I am sorry but as i am the beginner i am not able to understand the methods to which i should call or the logic for merging cells. May i get help in editing the code by add merging logic for undefined count of records displayed from database? – MaheshwariC Sep 21 '17 at 15:08
  • The mentioned link has perfect example of the functionality that you want. Since there is no predefined function like "table.mergeColumn", you have to create your own. This example mentioned how to create your own view. First run that program in new workspace. Then take the components you required and put them into your code. And yes, to understand which components you need, you have to search and read on google. :-) – Aman Sep 22 '17 at 04:44

0 Answers0