Question

I created inventory application, when display transaction list from database I use JTabel inside JScrollPane. Set JScrollPane horizontalScrollBar Policy to AS_NEED and JTable setAutoResize to false and Column width set using TableColumnModel as I need. When Width set manually table width going out of viewport and HorizotalScrollBar activate. But I run program and Use scrollbar to see column outside viewport, text and header text get blur during scroll. Follow given code an screen shoot of project

SetTableModel using DefaultTableModel

DefaultTableModel dm;
dm = new DefaultTableModel(0, 0){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }            
        };
        String titleCol[] = new String[]{"ID","Invoice No","Date","Account Name","Total Amount","Taxeble Amount","Tax %","Tax Amount","Discount Amount","Grand Total"};
        dm.setColumnIdentifiers(titleCol);
        table1.setModel(dm);

       table1.getTableHeader().setFont(new Font("Tahoma", Font.BOLD, 14));

Set Width of Column using TableColumnModel

table1.getTableHeader().setPreferredSize(new Dimension(table1.getTableHeader().getPreferredSize().width, 35));

        TableColumnModel columnModel = table1.getColumnModel();

        columnModel.getColumn(0).setMinWidth(50);
        columnModel.getColumn(0).setMaxWidth(50);
        columnModel.getColumn(1).setMinWidth(85);
        columnModel.getColumn(1).setMaxWidth(85);
        columnModel.getColumn(2).setMinWidth(100);
        columnModel.getColumn(2).setMaxWidth(100);
        columnModel.getColumn(3).setMinWidth(250);
        columnModel.getColumn(4).setMinWidth(100);
        columnModel.getColumn(4).setMaxWidth(100);
        columnModel.getColumn(5).setMinWidth(130);
        columnModel.getColumn(5).setMaxWidth(130);
        columnModel.getColumn(6).setMinWidth(75);
        columnModel.getColumn(6).setMaxWidth(75);
        columnModel.getColumn(7).setMinWidth(130);
        columnModel.getColumn(7).setMaxWidth(130);
        columnModel.getColumn(8).setMinWidth(130);
        columnModel.getColumn(8).setMaxWidth(130);
        columnModel.getColumn(9).setMinWidth(130);
        columnModel.getColumn(9).setMaxWidth(130);

Screenshot of Before using Horizotal ScrollBar enter image description here

Screenshot of After using Horizotal ScrollBar enter image description here

So Is there any solution to avoid blur.

Was it helpful?

Solution

Your problem in next line:

table1.getTableHeader().setPreferredSize(new Dimension(table1.getTableHeader().getPreferredSize().width, 35)); 

Set height of your header with help of TableCellRenderer, for example:

DefaultTableCellRenderer defaultRenderer = (DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer();
defaultRenderer.setPreferredSize(new Dimension(0,35));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top