1

I have JTable inside JScrollPane and I am struggling with the problem, that JTable doesn't fit in JScrollPane when scrolled horizontally. I have 18 rows in JTable but only 16 rows shown when I scroll horizontally. Here is what I mean

http://imgur.com/BfD7sKA (Sorry cannot post images, 10 reputation required :P)

So as you can see from the picture rows kinda run on the columns and last 2 rows are missing.

I haven't coded programatically anything yet, everything has been done using Netbeans IDE Design builder.

P.S. Interesting fact thou, if I expand my program or enter full screen, program shows ALL columns that must be there. But if I shrink program again and try to use horizontal scroll, that's what happens.

If I missed anything (sorry first time posting here) just ask. Thanks for the help!

So here is the code from the Netbeans regarding main UI window

package View;

import static javax.swing.JFrame.EXIT_ON_CLOSE;
import base.*;
import java.awt.Dimension;

/**
 *
 * @author Nikki
 */
public class V_main extends javax.swing.JFrame {

    /**
     * Creates new form V_main
     */
    public V_main() {
        initComponents();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        Dimension d = account_table.getPreferredSize();
            jScrollPane1.setPreferredSize(
        new Dimension(d.width,account_table.getRowHeight()*18+1));
    }

    /**
     * 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() {

        picture_label = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        account_table = new javax.swing.JTable();
        exit_button = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("BASe (Basic Accounting System)");

        picture_label.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/fazer_logo.png"))); // NOI18N

        jButton1.setText("Add New Customer");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Remove Customer");

        jButton3.setText("Edit Customer Information");

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setAutoscrolls(true);
        jScrollPane1.setPreferredSize(new java.awt.Dimension(1500, 1000));

        account_table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Nikki", "Kononov", null, "", "", "none", "nikki@gmail.com", null, "none", null, null, null, null, null, null, null},
                {"Nikki", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {"Nikki", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Company", "Person", "Title ", "Street Address", "Zip Code", "City", "Area", "Country", "Website", "Email", "Telephone", "Fax", "Mobile", "Employees", "Turnover", "Result"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        account_table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        account_table.setCellSelectionEnabled(true);
        account_table.setFillsViewportHeight(true);
        account_table.setPreferredSize(new java.awt.Dimension(1300, 1000));
        jScrollPane1.setViewportView(account_table);
        account_table.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);

        exit_button.setText("Exit");
        exit_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exit_buttonActionPerformed(evt);
            }
        });

        jButton4.setText("Search for Customers");

        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.DEFAULT_SIZE, 938, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(27, 27, 27)
                            .addComponent(picture_label))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton3)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(exit_button, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                            .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(picture_label)
                        .addGap(13, 13, 13)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(exit_button, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE)))
                .addContainerGap())
        );

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

    private void exit_buttonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        super.dispose();
    }                                           

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        BASe.showAdd();
    }                                        

    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify                     
    private javax.swing.JTable account_table;
    private javax.swing.JButton exit_button;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel picture_label;
    // End of variables declaration                   
}
Nikki Kononov
  • 549
  • 1
  • 8
  • 20
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Mar 17 '13 at 10:51
  • __everything has been done using Netbeans IDE Design builder__ IMHO This is not a good way to learn. Please learn to write the code yourself instead of using UI builders. – Amarnath Mar 17 '13 at 10:59
  • don't use setXXSize, ever - for some reasons, [see a recent QA](http://stackoverflow.com/a/7229519/203657). In this particular case, it seems to confuse the scrolling: with different, incompatible sizes on the table and its enclosing scrollPane, the header is scrolled differently from the table itself – kleopatra Mar 17 '13 at 15:08
  • as an aside: please learn java naming conventions and stick to them :-) – kleopatra Mar 17 '13 at 15:13
  • kleopatra, I do follow naming conventions, no worries on this side :D I just always put all the variables first and then name them all at once. Sorry if it confused you. So what should I use instead of setSize? Not quite good at programming, yet :) – Nikki Kononov Mar 17 '13 at 15:54
  • _I do follow naming conventions_ well ... not in the code above (java convention is camel-case, not underscores). Note that setSize != setXXSize - as always, the general answer is a suitable LayoutManager. For a JTable, you might consider subclassing and improve on its Scrollable implementation, in particular the getPrefScrollableViewportSize in terms of how many columns/rows you want to see inside a JScrollPane – kleopatra Mar 17 '13 at 16:22
  • you read half of the message, I wrote "I do follow naming conventions,... I just always put all the variables first and then name them all at once" basically what I meant is when entire layout is ready and all variables have been added then I name them all according to the layout position and naming convention but anyway thanks for the response. – Nikki Kononov Mar 17 '13 at 16:24
  • 1
    well, my emphasis is on _java naming conventions_ - if you want _java developers_ read your code easily ;-) Obviously, you are free to follow (or not) whatever conventions you can think of - if you are the only person reading the code and don't want any external help. – kleopatra Mar 17 '13 at 17:03

2 Answers2

1

You either can scroll or have all columns fit (squeezed if there are many) visible without scrollbar. The behaviour is controlled by the autoResizeMode property. To show the scrollbar:

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

To show all columns, each getting the same amout of excess width:

table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

The latter is the default (modulo netbeans' interference, don't know anything about that part).

Edit

The error in this particular context seems to be calling setPreferredSize on both the table and its enclosing scrollPane with incompatible sizes. The outcome is undetermined behaviour when the actual size is bigger than the table's hard-coded pref.

Bottomline: yet another reason why you shouldn't use setXXSize on a component, ever. Here the way out is to implement the table's getPreferredScrollableViewportSize (which is part of its Scrollable role) to return something reasonable in terms of column/row sizes.

Community
  • 1
  • 1
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • The problem is, I have too many columns and I want them to be readable not squeezed. Thing that I noticed is that, if I extend the width of the table more than 1300 then it starts to act weird as I showed above. But if I make it smaller then it fits the ScrollPane. So could it be that size of the scrollpane is not big enough? – Nikki Kononov Mar 17 '13 at 13:04
  • sound like something wrong with the code you are not showing :-) Please edit your question and post a SSCCE that demonstrates the behaviour you're describing – kleopatra Mar 17 '13 at 13:06
0

Set the header's width more bigger,for example 10000,it will solve the problem! example code below: Tablename.getTableHeader().setPreferredSize(new Dimension(10000,30));