Horizontal scrollbar is not working with JTable in Java Swing

后端 未结 1 1648
清歌不尽
清歌不尽 2021-02-06 18:39

I have a JTable which I am passing in JScrollPane. The vertical scrollbar is showing up and working good, but the horizontal scrollbar is not working.

相关标签:
1条回答
  • 2021-02-06 19:01

    You need to use:

    table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
    

    Don't use:

    tab.setPreferredScrollableViewportSize(new Dimension(1,1));
    

    That is an unrealistic size. That method is to give a reasonable preferred size to the table so that the frame.pack() method will work.

    js.setPreferredSize(new Dimension(400,400));

    Don't set the preferred size of the scrollpane. The setPreferredScrollableViewportSize() is used to specify a size for the table.

    mainPanel.setPreferredSize(new Dimension(500, 500));
    mainPanel.setSize(500,500);
    

    Don't set a size or a preferred size of a component. Each component is responsible for determining its own preferred size.

    mainPanel=new JPanel();

    By default a JPanel uses a FlowLayout which means any component added to it is displayed at its preferred size. I would probably set the layout to a BorderLayout. Then the component can resize with the space available and the scrollbars will be used as required.

    Edit:

    The scrollbars appear when the preferred size of table is greater than the size of the scrollpane. So you need to set the width of the TableColumn based on the width of the text to be displayed in the column. An easy way to do this is to use the Table Column Adjuster class which will set the width to the largest line of text. YOu would invoke this class after you have added the model (containing the data) to the table:

    Updated code:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import java.io.*;
    
    public class TableCreate extends JFrame
    {
        JPanel mainPanel;
        TableCreate() throws IOException
        {
    
            mainPanel=new JPanel(new BorderLayout());
            String InputFile1 = "TableCreate.java";
            BufferedReader breader1 = new BufferedReader(new FileReader(InputFile1));
            String line1 = "";
            line1 = breader1.readLine();
    
            DefaultTableModel model1 = new DefaultTableModel();
            model1.addColumn("line");
    
            while((line1=breader1.readLine()) != null)
             {
                 System.out.println(line1);
                 model1.addRow(new Object[]{line1});
             }
             breader1.close();
    
            JTable tab=new JTable(model1);
    
            tab.setPreferredScrollableViewportSize(new Dimension(300, 200));
            tab.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            TableColumnAdjuster tca = new TableColumnAdjuster(tab);
            tca.adjustColumns();
    
            JScrollPane js = new JScrollPane(tab);
            add(js);
        }
    
        public static void main(String[] args) throws IOException
        {
            TableCreate tc=new TableCreate();
            tc.pack();
            tc.setVisible(true);
            tc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    
    0 讨论(0)
提交回复
热议问题