水平滚动条在Java Swing中不能与JTable一起使用 [英] Horizontal scrollbar is not working with JTable in Java Swing

查看:129
本文介绍了水平滚动条在Java Swing中不能与JTable一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 JTable 我在 JScrollPane 中传递。垂直滚动条显示正常,但水平滚动条不起作用。我提供的代码是可编译的,只需将您的路径放在 InputFile1 字符串中,并在该位置创建一个长文件。

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. The code I provided is compilable, just put your path in InputFile1 string and create a long file on that location.

我尝试了很多解决方案,但没有任何工作。我有一个只有一列的表,该列包含文档中的行。我需要垂直和水平滚动条。请提出一些解决方案。

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.

其他尝试:

案例1

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

如果我设置此项,则垂直滚动条不起作用。

If I am setting this ,vertical scrollbar doesn't work.

案例2:

tab.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 

如果我设置了这个,则列宽变得非常小。如果我尝试将其设置得更大,那么它也不起作用,只显示水平滚动条但没有视口。

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.

您可以取消注释并检查它们。

You can uncomment and check them.

代码:

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          
    }       
}


推荐答案

您需要使用:

table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );

不要使用:

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

这是一个不切实际的尺寸。该方法是为表提供合理的首选大小,以便 frame.pack()方法可以工作。

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));

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

不设置首选大小滚动窗格。 setPreferredScrollableViewportSize()用于指定表的大小。

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();

mainPanel=new JPanel();

默认情况下,JPanel使用FlowLayout,这意味着添加到其中的任何组件都以其首选大小显示。我可能会将布局设置为BorderLayout。然后组件可以使用可用空间调整大小,并根据需要使用滚动条。

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.

编辑:

当表的首选大小大于滚动窗格的大小时,会出现滚动条。因此,您需要根据要在列中显示的文本的宽度来设置 TableColumn 的宽度。一个简单的方法是使用表列调整器 class,将宽度设置为最大的文本行。将模型(包含数据)添加到表后,你会调用这个类:

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:

更新代码:

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);
    }
}

这篇关于水平滚动条在Java Swing中不能与JTable一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆