2013-03-22 99 views
0
package com.officelog.manager; 

    import java.awt.*; 
    import javax.swing.*; 
    import java.util.*; 


    public class AdminControl implements Runnable{ 
public static void main(String[] args) { 
    new AdminControl(); 
} 

Thread t = null ; 
int hour ,minute,second ; 
int year,month,day ; 
int Month ; 


private static JFrame frame = new JFrame("Admin_Control"); 
static JPanel panel = new JPanel(); 
JLabel label,name,date,time; 
JTable table ; 
JScrollPane scrollpane ; 
public AdminControl() { 
    frame.setBounds(100, 100, 700, 600); 
    frame.setResizable(false); 
    frame.add(panel); 

    panelSetUp(); 

    frame.setVisible(true); 

} 
private void panelSetUp() { 
    panel.setLayout(null); 
    panel.setBackground(Color.lightGray); 
    panel.setBorder(BorderFactory.createBevelBorder(1,Color.white, Color.white)); 



    time = new JLabel(); 
    panel.add(time); 
    time.setBounds(20, 25, 70, 25); 

    date = new JLabel(); 
    panel.add(date); 
    date.setBounds(20, 75, 70, 25); 

    t = new Thread(this); 
    t.start(); 

    name = new JLabel("Name:"); 
    panel.add(name); 
    name.setBounds(20,50,50,25); 

    label = new JLabel("SuperUser"); 
    panel.add(label); 
    label.setBounds(60, 50, 70, 25); 
    label.setForeground(Color.blue); 


    tableSetUp(); 
    JMenuBar menuBar = new JMenuBar(); 
    frame.add(menuBar,BorderLayout.NORTH); 
    menuBar.setBorder(BorderFactory.createBevelBorder(1, Color.gray, Color.gray)); 

    JMenu file = new JMenu("File"); 
    menuBar.add(file); 

    JMenu source = new JMenu("Source"); 
    menuBar.add(source); 
} 
public void run(){ 
    try{ 
     while(true){ 
      Calendar now = Calendar.getInstance(); 
      hour = now.get(Calendar.HOUR_OF_DAY); 
      minute = now.get(Calendar.MINUTE); 
      second = now.get(Calendar.SECOND); 

      year = now.get(Calendar.YEAR); 

      Month = now.get(Calendar.MONTH); 
      month = Month + 1; 

      day = now.get(Calendar.DATE); 
      displayDateTime(); 
      time.repaint(); 
      date.repaint(); 
      t.sleep(1000); 
     } 
     }catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 

public void displayDateTime(){ 
    time.setText(hour+":"+minute+":"+second); 
    date.setText(day+"/"+month+"/"+ year); 
} 

private void tableSetUp(){ 
    int row ; 

    Object [][] data ={ 
      {new Integer(5),"Deepak","Deepak" 
       ,"2013-21-03" 
      } 
    }; 
    String columnNames []={"EmployeeID" , "EmployeeName", 
      "EmployeePassword","CreatedOn"}; 
    table = new JTable(data , columnNames); 
    scrollpane = new JScrollPane(table); 
    panel.add(scrollpane); 
    scrollpane.setBounds(50, 200, 600, 100); 
    table.setBounds(50, 100, 600, 100); 
    table.setForeground(Color.BLUE); 
    table.setGridColor(Color.pink); 

} 

} 任何機構都可以告訴我如何將數據插入到數據庫的JTable中。這是我的代碼是不完整的。我創建了一個表,但我不知道如何將數據插入到網格格式的數據庫中,以便可以編輯表格中的數據並將其更新到數據庫中。感謝您的時間和考慮Swing,Jtable,JDBC

+0

嗯,我知道如何從數據庫中檢索數據,但我無法弄清楚如何使用TableModel來將數據插入到表中。 – 2013-03-22 04:59:07

+1

你是否在搜索關於你的問題? – Amarnath 2013-03-22 06:09:34

+0

怎麼沒有在這個頁面的最後邊緣的鏈接幫助你至少一個首發?你確實讀過它們(至少那些包含標題中的jdbc/database),不是嗎? – kleopatra 2013-03-22 12:13:07

回答

0

那麼,保存JTable列名稱爲String Array和數據爲two dimensional array of any type。您可以通過從數據庫中檢索數據來填充數據數組。

下面的鏈接有一個例子(沒有數據庫)

http://www.roseindia.net/java/example/java/swing/ScrollableJTable.shtml

以下是甲骨文指導,這是非常好的。

http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

+0

你認爲如果我這樣做,將有可能編輯更新或刪除表或任何特定的值,從而更新數據庫,以及... – 2013-03-22 07:22:17

+0

該鏈接包含的方式編輯/插入JTable的值。其餘部分取決於你的應用邏輯 – 2013-03-22 07:23:35

+0

a)roseindia參考它完全不相關(並且如此微不足道,同時令人費解,因爲它們經常是IMO)b)教程鏈接也是不相關的,雖然總是一個好的首發 – kleopatra 2013-03-22 12:17:15