2013-01-18 88 views
0

我在JScrollPane中添加JTable,然後向面板添加滾動窗格,然後將面板添加到框架,但不起作用,這裏是代碼。我希望在表格或框架上具有滾動條,可以使表格滾動,以便用戶可以看到它。我已經嘗試了許多方法,但不爲我工作 這裏是整個代碼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); 

} 
}  
+2

發佈更多驗證碼。目前發佈似乎沒問題。你如何將面板添加到主容器? SSCCE? – StanislavL

+0

在這裏,而不是這樣做,** panel.add(jpane); **嘗試將此面板添加到一個JScrollPane,使表滾動,**添加(新的JScrollPane(面板))** – Amarnath

回答

3

與適當的佈局添加您的面板前的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); 
+0

我定義了'JPanel'的邊框佈局,它保存了我的'JScrollPane',但它只在我沒有定義方向的情況下添加滾動窗格組件時才起作用。如果我在'.add'函數中通過了一個方向,它就會崩潰。不知道爲什麼。 – brandaemon

5

如果你想滾動表裏面然後做,

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

輸出:

enter image description here

+0

我做到了,但還是它不工作 我使用Windows建設者製作GUI,其主要方法就是這樣 ' 公共靜態無效的主要(字串[] args){ \t \t EventQueue.invokeLater(新的Runnable(){ \t \t \t公共無效的run(){ \t \t \t \t嘗試{ \t \t \t \t \t View frame = new View(); \t \t \t \t \t frame.setVisible(true); \t \t \t \t}趕上(例外五){ \t \t \t \t \t e.printStackTrace(); \t \t \t \t} \t \t \t} \t \t}); \t} – Waqas

+1

@PrinceVakas Firslty如果您是Swing的新手,那麼請自己編寫代碼,而不是使用UI-Builders。用示例代碼更新顯示整個流程的_post_? – Amarnath

+0

我沒有發佈全班 – Waqas