java JScrollPane 中的 JTable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14393112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:07:40  来源:igfitidea点击:

JTable in JScrollPane

javaswingjtablejscrollpanelayout-manager

提问by Waqas

I am adding JTablein a JScrollPanethen adding scrollpane to the panel, then adding panel to the frame but it doesn't work here's the code. I want to have scroll bar on table or frame that will make table scrollable so user can see it. I have tried many ways but non worked for me here is the whole code

我正在添加JTable一个JScrollPane然后将滚动窗格添加到面板,然后将面板添加到框架,但这里的代码不起作用。我想在表格或框架上有滚动条,使表格可滚动,以便用户可以看到它。我尝试了很多方法,但对我不起作用,这里是整个代码

public class View extends JFrame {

private static final long serialVersionUID = 1L;

/**
 * Launch the application.
 */
//here in the main method i it adds in the JFrame everything

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                View frame = new View();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public void showData() throws SQLException, ParseException {

    panel = new JPanel();
    panel_1 = new JPanel();
    model_1 = new DefaultTableModel();
    model_2 = new DefaultTableModel();

    model_1.addColumn("Title");
    model_1.addColumn("Priority ");
    model_1.addColumn("DeadLine");
    model_1.addColumn("Time");
    model_1.addColumn("Progress");

    model_2.addColumn("Task Title");
    model_2.addColumn("Priority ");
    model_2.addColumn("DeadLine");
    model_2.addColumn("Time");
    model_2.addColumn("Done");

    Database obj = new Database();

    ArrayList<Task> list = obj.getTasks();
    for (int i = 0; i < list.size(); i++) {

        Task task = list.get(i);
        Object[] row = { task.title, task.priority, task.deadine,
                task.time, task.progress };

        // Comparing Dates

        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("MM-d-yyyy");
        String dateNow = formatter.format(currentDate.getTime());

        java.util.Date systemDate = new SimpleDateFormat("MM-d-yyyy",
                Locale.ENGLISH).parse(dateNow);

        if (!task.deadine.before(systemDate)) {
            // add row to to do tab
            model_1.addRow(row);
        } else {
            // add row to done tab
            model_2.addRow(row);
        }

        // **********************

    }

    toDoTable = new JTable(model_1);
    doneTable = new JTable(model_2);
    toDoTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    doneTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    toDoTable.setFillsViewportHeight(true);
    doneTable.setFillsViewportHeight(true);

//here i add the JScrollPane  and it doesnt work 

    JScrollPane jpane = new JScrollPane(toDoTable);
    JScrollPane jpane1 = new JScrollPane(doneTable);

    panel.add(jpane);
    panel_1.add(jpane1);
    panel.add(jpane);
    panel_1.add(jpane1);

}
}    

回答by Amarnath

If you want scroll inside a table then do,

如果您想在表格内滚动,请执行以下操作,

   JScrollPane jpane = new JScrollPane(table);

But if you want the table itself to the scrolled then add the panel which is holding the table to the JScrollPaneand add this to your frame.

但是,如果您希望表格本身滚动,则将包含表格的面板添加到JScrollPane并将其添加到您的框架中。

public class JTableExample {
  public static void main(String[] args) {
    Object[] column = {"One", "Two"};
    Object[][] data = {{1, 2}, {3, 4}, {5, 6}};

    JTable toDoTable = new JTable(data, column);
    JScrollPane jpane = new JScrollPane(toDoTable);
    JPanel panel = new JPanel();
    JFrame frame = new JFrame();
    panel.add(jpane);
    frame.add(new JScrollPane(panel));
    frame.setVisible(true);
  }
}

Output:

输出:

enter image description here

在此处输入图片说明

回答by vels4j

Add your panel with proper layout for ex BorderLayout

为 ex BorderLayout 添加具有适当布局的面板

  panel.add(jpane,BorderLayout.NORTH);
  panel_1.add(jpane1,BorderLayout.SOUTH);

or you can use javax.swing.Box

或者你可以使用 javax.swing.Box

 Box box = Box.createVerticalBox();
 box.add(jpane);
 box.add(jpane1);
 frame.getContentPane().add(box);