2

i am trying to disable the horizontalScroolbar on Scrollpane.. i have tried using setHorizontalScrollbarVisibility() method but it doesnot work.i want to make the horizontal bar disappear.. i also tried using JScrollPane but it did not display my table.. please help and just what changes should i do..

import java.awt.*;    
import java.awt.event.ActionEvent;     
import java.awt.event.ActionListener;    
import java.util.*;  
import javax.swing.*;   
import javax.swing.table.*;  
class Jtdemo extends    JFrame
{

DefaultTableModel dtm;

    JTextField jf=new JTextField();
    JButton jb=new JButton();
    String data1[]=new String[100];
    //JScrollPane jsp=new JScrollPane();
    JTable jt=new JTable();

    //Vector v=new Vector(10);
int i;
public Jtdemo()
{
        dtm = new DefaultTableModel();
        jt.setModel(dtm);
        jt.setForeground(Color.red);

       pane.setViewportView(jt);
        dtm.addColumn("hello");
        //dtm.addColumn("hi");
        jb.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
        String nn=jf.getText();
        String data[]=nn.split(",");
        for(i=0;i<data.length;i++)
            //v.addElement(data[i]);
                //System.out.println(data[i]);
            dtm.addRow(new Object[]{data[i],/*Comments/Assignment value*/});
            //dtm.addRow(null);
        }
        });
        jt.setBackground(Color.black);
        ScrollPane pane = new ScrollPane();
        //pane.setViewportView(jt);
        setLayout(null);
        pane.setBounds(10, 10, 500,500);
        pane.add(jt);
        jf.setBounds(10, 610, 50,25);
        jb.setBounds(610, 610, 50,25);
        //add(jt);
        add(pane);
        add(jf);
        add(jb);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setExtendedState(MAXIMIZED_BOTH);
        setVisible(true);
    }   
public static void main( String args[] )
{
    // Create an instance of the test application
    new Jtdemo();

}
}

1 Answers1

1

Just use:

JScrollPane pane = new JScrollPane(jt);
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

instead of

pane.setViewportView(jt);
ScrollPane pane = new ScrollPane();
pane.add(jt);

Watch next tutorial

alex2410
  • 10,904
  • 3
  • 25
  • 41
  • thanku @alex2410.. but itried the setViewportView(jt) with JScrollPane too..that also did not display my table.. it was full blank..:( – user2984132 Nov 14 '13 at 10:01
  • If you use 2 lines from my example instead of yours, all will be work. It will show at start only tableHeader, then add rows with help of your `jb` button. – alex2410 Nov 14 '13 at 10:04
  • If you have a JTable inside of a JScrollPane that is wider than the scrollpane, you can drag the mouse inside it in order to 'scroll' the panel, even once you disable the scrollbars using the technique you have outlined here. – otoomey Dec 15 '18 at 19:01