嗨,我正在做一个摇摆项目。我想在数据库中的 JTable 中显示我的数据。我
创建了 JSplitpane。在面板 1 中为来自 Databse 的 sect 数据创建了搜索按钮、jLabel 和 JTextBox ...在面板 2 中,我想在 JTable.my 数据库查询中显示搜索结果,并且一切正常......但我无法在面板 2 中显示 JTable .. .任何人都可以帮我解决这个问题。
public JFrame TimesheetReport(){ //创建第一个 JFrame
final JFrame employeeFrame = new JFrame("Report");
employeeFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
employeeFrame.setResizable(true)
employeeFrame.setLocation(100,50);
employeeFrame.setVisible(true);
employeeFrame.setResizable(false);
panel.setLayout(new FlowLayout());
panel1.setLayout(new BorderLayout());
employeeFrame.pack();
final JLabel label1;
final JLabel label2;
final JTextField text1;
final JTextField text2;
final JButton ADD;
{
label1 = new JLabel("From Date");
text1 = new JTextField(12);
label2 = new JLabel("End Date");
text2 = new JTextField(12);
JButton button2 = new JButton("calendar");
ADD=new JButton("Search");
ADD.setText("Search");
ADD.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
DbUtility TimesheetReport=new DbUtility();
TimesheetReport. loadDriver();
TimesheetReport.connect();
String value=(text1.getText());
String value2=(text2.getText());
ResultSet rs= TimesheetReport.executeSelectQuery14(value,value2);
String Activityid =null, EmployeeName = null,StartDate = null,EndDate=null,Activity=null,Project=null,Day1=null,Day2=null,Day3=null,Day4=null,Day5=null ;
try {
while (rs.next()) {
Activityid = rs.getString("Activityid");
EmployeeName = rs.getString("EmployeeName");
StartDate = rs.getString("StartDate");
EndDate = rs.getString("EndDate");
Activity = rs.getString("Activity");
Project= rs.getString("Project");
Day1 = rs.getString("Day1");
Day2 = rs.getString("Day2");
Day3 = rs.getString("Day3");
Day4 = rs.getString("Day4");
Day5 = rs.getString("Day5");
String[] columnNames = {"Activityid","EmployeeName", "StatrDate","EndDate", "Activity","Project","Day1","Day2","Day3","Day3","Day4","Day5"};
String data[][] = {{Activityid,EmployeeName, StartDate,EndDate,Activity,Project,Day1,Day2,Day3,Day4,Day5}};
System.out.println(Activityid+" "+EmployeeName+" "+StartDate+"" +EndDate+""+Activity+" "+Project+""+Day1+""+Day2+""+Day3+""+Day4+""+Day5);
final DefaultTableModel model = new DefaultTableModel(data, columnNames);
table2= new JTable(model);
table2.setRowHeight( 70 );
table2.setFillsViewportHeight(true);
JTextField field = new JTextField();
field.setForeground(Color.RED);
field.setBackground(Color.RED);
table2.getTableHeader().setPreferredSize(new Dimension(50,50));
table2.getTableHeader().setBackground(Color.blue);
table2.getTableHeader().setForeground(Color.white);
table2.getTableHeader().setFont(new Font("Serif", Font.BOLD, 15));
table2.getTableHeader().setOpaque(true);
table2.getModel();
table2.setForeground(Color.blue);
table2.setBackground(Color.white);
table2.setGridColor(Color.MAGENTA);
table2.setShowGrid(true);
JScrollPane pane = new JScrollPane(table2);
panel1.add(pane);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,panel,panel1 );
splitPane.setDividerLocation(300);
Dimension minimumSize = new Dimension(250, 0);
panel.setMinimumSize(minimumSize);
employeeFrame.add(splitPane);
pane.add(panel1);
panel.add(label1);
label1.setForeground(Color.white);
panel.add(text1);
panel.add(label2);
label2.setForeground(Color.white);
panel.add( text2);
panel.add(ADD);
}
return employeeFrame;
}