繁体   English   中英

JScrollPane中的JTable

[英]JTable in JScrollPane

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

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

}
}    

如果您想在表格内滚动,请这样做,

   JScrollPane jpane = new JScrollPane(table);

但是,如果要使表格本身滚动,则将保存表格的面板添加到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);
  }
}

输出:

在此处输入图片说明

为前BorderLayout添加具有正确布局的面板

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM