0

I am building a JTable that displays some little info from my database, about 18000 rows. Everything is displayed well in the table, except for the vertical scrollbar. It just disappears when the table is high populated. I have another JTable with about 500 rows and I the problem gets solved increasing the height of the JTable, but 1) that's not the solution and 2) does not work for the biggest JTable.

My question is: is there any way to force the scrollbar to be there?

I've tried setting the JScrollPane vertical scrollbar policy to ALWAYS but it is not working.

Here is the SSCCE:

import javax.swing.table.DefaultTableModel;

/**
 *
 *
 */
public class NewJFrame extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        initComponents();

        final DefaultTableModel tabla = (DefaultTableModel) jTable1.getModel();

        //Adds 1000 rows for testing the error
        for(int i = 0; i < 1000; i++){



            tabla.addRow(new Object[]{""+i});

        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null}
            },
            new String [] {
                "Title 1"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class
            };

            @Override
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        final javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(13, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (final javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (final ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (final InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (final IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (final javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                    
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration                  
}

Hope you can help!

Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
ri0s425
  • 31
  • 7
  • 2
    Can you show some of the code from what you've tried? – Seda Mar 22 '16 at 10:02
  • 1
    I suspect the difference is not between 500 and 18000 rows, but some other difference between your two JTables. What happens if you put 18000 rows into the one that has 500 and the other way around? – Ole V.V. Mar 22 '16 at 10:14
  • @SKunda I've tried something like 'jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);' I've seen on other similar questions but this does not help – ri0s425 Mar 22 '16 at 10:45
  • @OleV.V. Putting 18000 rows in the other table makes the scroll bar disappear too, it clearly has to do something with the number of rows the JTable has – ri0s425 Mar 22 '16 at 10:46
  • 1
    What platform are you on? Swing doesn’t always behave exactly the same with different L&Fs and OSs. – Ole V.V. Mar 22 '16 at 11:31
  • @OleV.V. I'm using Windows 7 and Netbeans – ri0s425 Mar 22 '16 at 11:39
  • 1
    @ri0s425 Please post a [SSCCE](http://sscce.org/) so we can also reproduce your issue. I think, it could be either an error in your program or in L&F you use. I already had Swing tables with more than 1Mio rows without any painting problems. – Sergiy Medvynskyy Mar 22 '16 at 11:57
  • @SergiyMedvynskyy http://pastebin.com/FhhFbZgN With this simple code I can't see the scroll bar, but when I increase the JTable height, it comes back – ri0s425 Mar 22 '16 at 14:10
  • 1
    @ri0s425, Post the code in the forum, not on pastebin. Code should be posted with every question in the form of a [SSCCE](http://sscce.org/) so we know exactly what you are doing. – camickr Mar 22 '16 at 14:42
  • @ri0s425 Have tried your example but see no problems with scroll bar. – Sergiy Medvynskyy Mar 22 '16 at 14:45
  • @camickr I've added his example to the question – Sergiy Medvynskyy Mar 22 '16 at 14:47
  • 1
    Is it the entire scrollbar that disappears, or do you mean that the scrollbar's thumb disappears - a known problem with Nimbus L&F using Java1.8 – FredK Mar 22 '16 at 15:08
  • This looks like a problem in 8u60 later fixed in 8u66: [Bug ID: JDK-8144243 Scrollbar thumb disappears with Nimbus L&F](http://bugs.java.com/view_bug.do?bug_id=8144243) – aterai Mar 22 '16 at 15:11
  • 1
    Regardless of what Oracle says, neither 8u66 nor 8u73 fixed this problem. – FredK Mar 22 '16 at 15:49
  • The latest version of Java that I have found where this problem does not occur for Nimbus is 1.7u72 – FredK Mar 22 '16 at 15:56
  • @FredK It's only the thumb that disappears. I have updated my JDK to 8u74 from 8u40 and the problem seems solved in my computer, but not on others, that's the main problem because all the other users cant still see the thumb – ri0s425 Mar 23 '16 at 09:04

1 Answers1

0

After deep search I found a question that explained my problem.

Putting this code after setting the Nimbus L&F solved the problem:

LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
UIDefaults defaults = lookAndFeel.getDefaults();
defaults.put("ScrollBar.minimumThumbSize", new Dimension(30, 30));
Community
  • 1
  • 1
ri0s425
  • 31
  • 7