title: Horizontal scrollbar is not working with JTable in Java Swing
body: <p>I have a <code>JTable</code> which I am passing in <code>JScrollPane</code>. The vertical scrollbar is showing up and working good, but the horizontal scrollbar is not working. The code I provided is compilable, just put your path in <code>InputFile1</code> string and create a long file on that location.</p>
  
  <p>I have tried many solutions but nothing is working. I have a table with only one column, that column contains lines from a document. I need both vertical and horizontal scrollbars. Please suggest some solution.</p>
  
  <p>Other attempts:</p>
  
  <p><strong>Case1</strong>:</p>
  
  <pre><code>tab.setPreferredSize(new Dimension(400,400));
  </code></pre>
  
  <p>If I am setting this ,vertical scrollbar doesn't work. </p>
  
  <p><strong>Case2:</strong></p>
  
  <pre><code>tab.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 
  </code></pre>
  
  <p>If I set this, the column width becomes very small. If I try to make it larger then also it does not work and only shows a horizontal scroll bar but without a viewport.</p>
  
  <p>You can uncomment and check them.           </p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>public class tablecreate extends JFrame implements ActionListener 
  {
  
      JPanel mainPanel;
      tablecreate() throws IOException
      {
  
          mainPanel=new JPanel();
          String InputFile1 = "/home/user/Desktop/a.txt";
          BufferedReader breader1 = new BufferedReader(new FileReader(InputFile1));
          String line1 = "";
          line1 = breader1.readLine();
          DefaultTableModel model1 = new DefaultTableModel();
          JTable tab=new JTable(model1);
          model1.addColumn("line"); 
          while((line1=breader1.readLine()) != null)
           {
               System.out.println(line1);
               model1.addRow(new Object[]{line1});
           }
           breader1.close();
           tab.setPreferredScrollableViewportSize(new Dimension(1,1));
           tab.setVisible(true);
          //tab.setPreferredSize(new Dimension(400,400));
          // tab.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 
  
           JScrollPane js = new JScrollPane(tab,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
           js.setPreferredSize(new Dimension(400,400));
           mainPanel.setPreferredSize(new Dimension(500, 500));
           mainPanel.setSize(500,500);
           mainPanel.add(js);
           this.add(mainPanel);
      }
  
      public static void main(String[] args) throws IOException
      {
          tablecreate tc=new tablecreate();
          tc.setSize(500,500);
          tc.setVisible(true);
          tc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
  
      @Override
      public void actionPerformed(ActionEvent arg0) {
          // TODO Auto-generated method stub          
      }       
  }
  </code></pre>