滚动窗格垂直滚动条不显示滚动旋钮

时间:2019-02-01 08:02:06

标签: java swing jtable jscrollpane

我创建了一个JScrollPane并向其中添加了一个JTable。我希望能够使用垂直滚动条滚动到表格的底部。 scollpane与表init一起显示,但是单击顶部或底部箭头不会分别将视图扩展到顶部或底部。垂直滚动轨道中也没有滚动条显示。

我尝试将视口设置为不同的大小,也使用不同的构造函数来创建jscrollpane,即jscrollpane(table_3,JScrollPane.VERTICAL_SCROLLBAR_​​AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_​​AS_NEEDED);但没有一个起作用。

public class test {
JFrame f;
int myMonth, myDay, myYear;
private JTable tblAppts;

private String[] colTimes = { "Time", "Test 1", "Test 2", "Test 3" };
private Object[][] myTimes = { { "Time", "Test 1", "Test 2", "Test 3" }, { 
"00:30", "", "", "" },
        { "01:00", "", "", "" }, { "01:30", "", "", "" }, { "02:00", "", 
 "", "" }, { "02:30", "", "", "" },
        { "03:00", "", "", "" }, { "03:30", "", "", "" }, { "04:00", "", 
 "", "" }, { "04:30", "", "", "" },
        { "05:00", "", "", "" }, { "05:30", "", "", "" }, { "06:00", "", 
"", "" }, { "06:30", "", "", "" },
        { "07:00", "", "", "" }, { "07:30", "", "", "" }, { "08:00", "", 
"", "" }, { "08:30", "", "", "" },
        { "09:00", "", "", "" }, { "09:30", "", "", "" }, { "10:00", "", 
"", "" }, { "10:30", "", "", "" },
        { "11:00", "", "", "" }, { "11:30", "", "", "" }, { "12:00", "", 
"", "" }, { "12:30", "", "", "" },
        { "13:00", "", "", "" }, { "13:30", "", "", "" }, { "14:00", "", 
"", "" }, { "14:30", "", "", "" },
        { "15:00", "", "", "" }, { "15:30", "", "", "" }, { "16:00", "", 
"", "" }, { "16:30", "", "", "" },
        { "17:00", "", "", "" }, { "17:30", "", "", "" }, { "18:00", "", 
"", "" }, { "18:30", "", "", "" },
        { "19:00", "", "", "" }, { "19:30", "", "", "" }, { "20:00", "", 
"", "" }, { "20:30", "", "", "" },
        { "21:00", "", "", "" }, { "21:30", "", "", "" }, { "22:00", "", 
"", "" }, { "22:30", "", "", "" },
        { "23:00", "", "", "" }, { "23:30", "", "", "" } };
private JTable tblTime;
private JTable table_1;
private JTable table_2;
private JTable table_3;

// set the table widths method
public static void setJTableColumnsWidth(JTable table, int 
tablePreferredWidth, double... percentages) {
    double total = 0;
    for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
        total += percentages[i];
    }

    for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
        TableColumn column = table.getColumnModel().getColumn(i);
        column.setPreferredWidth((int) (tablePreferredWidth 
             (percentages[i] / total)));
    }
}

test() {

    f = new JFrame("WSDP");// creating instance of JFrame#
    // open the frame in a maximized state ie max vert and horizontal
    f.setSize(1000, 1000);
    f.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().setLayout(null);

    // menus
    JPanel panel = new JPanel();
    panel.setBounds(0, 0, 982, 27);

    // create border
    Border blackline = BorderFactory.createLineBorder(Color.black);
    panel.setBorder(blackline);
    f.getContentPane().add(panel);

    // icon buttons panel
    JPanel panel_1 = new JPanel();
    panel_1.setBounds(0, 26, 109, 593);
    panel_1.setBorder(blackline);
    f.getContentPane().add(panel_1);

    JScrollPane scrollPane = new JScrollPane();
    // scrollPane.setBounds(107, 78, 475, 875);
    scrollPane.setBounds(107, 78, 475, 500);
    // scrollPane.setPreferredSize(new Dimension(450, 110));
    scrollPane.setBorder(blackline);
    scrollPane.setViewportBorder(blackline);



    scrollPane.setVerticalScrollBarPolicy
                     (JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

    table_3 = new JTable(myTimes, colTimes);
    scrollPane.setRowHeaderView(table_3);
    table_3.setFillsViewportHeight(true);
    scrollPane.setVerticalScrollBarPolicy
              (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    table_3.setRowHeight(20);
    table_3.setFont(new Font("Courier", Font.BOLD, 14));
    setJTableColumnsWidth(table_3, 480, 10, 30, 30, 30);
    f.getContentPane().add(scrollPane);
    f.setVisible(true);

}

public static void main(String[] args) {

    test t = new test();
}

}

1 个答案:

答案 0 :(得分:1)

由于您对API的工作原理有一些基本的误解,因此您非常需要通读How to Use TablesHow to Use Scroll Panes

您从未设置JScrollPane的视口视图...

我已将您的“示例”简化为一个完整且可验证的示例,该示例演示了您的问题的即时解决方案...

Example

import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    private String[] colTimes = {"Time", "Test 1", "Test 2", "Test 3"};
    private Object[][] myTimes = {{"Time", "Test 1", "Test 2", "Test 3"}, {
        "00:30", "", "", ""},
    {"01:00", "", "", ""}, {"01:30", "", "", ""}, {"02:00", "",
        "", ""}, {"02:30", "", "", ""},
    {"03:00", "", "", ""}, {"03:30", "", "", ""}, {"04:00", "",
        "", ""}, {"04:30", "", "", ""},
    {"05:00", "", "", ""}, {"05:30", "", "", ""}, {"06:00", "",
        "", ""}, {"06:30", "", "", ""},
    {"07:00", "", "", ""}, {"07:30", "", "", ""}, {"08:00", "",
        "", ""}, {"08:30", "", "", ""},
    {"09:00", "", "", ""}, {"09:30", "", "", ""}, {"10:00", "",
        "", ""}, {"10:30", "", "", ""},
    {"11:00", "", "", ""}, {"11:30", "", "", ""}, {"12:00", "",
        "", ""}, {"12:30", "", "", ""},
    {"13:00", "", "", ""}, {"13:30", "", "", ""}, {"14:00", "",
        "", ""}, {"14:30", "", "", ""},
    {"15:00", "", "", ""}, {"15:30", "", "", ""}, {"16:00", "",
        "", ""}, {"16:30", "", "", ""},
    {"17:00", "", "", ""}, {"17:30", "", "", ""}, {"18:00", "",
        "", ""}, {"18:30", "", "", ""},
    {"19:00", "", "", ""}, {"19:30", "", "", ""}, {"20:00", "",
        "", ""}, {"20:30", "", "", ""},
    {"21:00", "", "", ""}, {"21:30", "", "", ""}, {"22:00", "",
        "", ""}, {"22:30", "", "", ""},
    {"23:00", "", "", ""}, {"23:30", "", "", ""}};
    private JTable table_3;

    public Test() {

        JFrame f = new JFrame("WSDP"); // creating instance of JFrame#
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JScrollPane scrollPane = new JScrollPane();

        table_3 = new JTable(myTimes, colTimes);
        table_3.setFillsViewportHeight(true);
        // This is dangerous and is likely to come back and haunt you
        table_3.setRowHeight(20);
        table_3.setFont(new Font("Courier", Font.BOLD, 14));

        // THIS is what's missing
        scrollPane.setViewportView(table_3);

        f.getContentPane().add(scrollPane);

        f.pack();
        f.setVisible(true);

    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
                new Test();
            }
        });
    }

}

我还建议您通读Laying Out Components Within a Container,因为它可以解决您将要面对的许多其他问题