如何在JSplitPane中添加JTable?

时间:2013-02-15 12:32:58

标签: java swing layout jtable jpanel

我正在做一个摇摆的项目。我想从数据库中显示我在JTable中的数据.I
创造了JSplitpane。在面板1中创建了搜索按钮,jLabel和JTextBox用于来自Databse的sect数据...在面板2中我想在JTable.my数据库查询中显示搜索结果并且一切运行良好..但是我无法在panel2中显示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;

       }

2 个答案:

答案 0 :(得分:3)

  

但我无法在panel2中显示JTable ......任何人都可以帮我解决   这个问题。

您的代码不是SSCCE格式...它不能在简单的复制和粘贴上编译,并且由于table2未在给定代码的方法范围内声明,因此存在错误也没有提到panel2,但我认为你的意思是panel1 ......

<强>解决方案:

1)不要使用null / Absolute LayoutManager

请阅读:

2)为什么需要将JTable添加到panel1(或问题panel2中引用)?如果有的话,我们会将JScrollPane作为视口添加到JTable panel1,只需将table2添加到JScrollPane并添加JScrollPaneJSplitPane即:

JScrollPane pane = new JScrollPane(table2);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel, pane);

或者将JTable添加到JScrollPane,然后将JScrollPane添加到panel1 BorderLayout,以便JPanel扩展到容器大小:

JPanel panel1=new JPanel(new BorderLayout());
....
JScrollPane pane = new JScrollPane(table2);
panel1.add(pane);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel, panel1);

3)另外因为我看到你通过最大化JFrame

JFrame#setExtendedState(JFrame.MAXIMIZED_BOTH);

使用LayoutManager明智的做法是在更改JFrame大小时自动调整组件大小。 ,BorderLayoutGridLayoutGridBagLayout是我想到的一些。

4)在添加所有组件之前,也不要将JFrame设置为可见。

5)通过SwingUtilities.invokeLater(Runnable r)块在Event Dispatch Thread上创建Swing组件。

6)不要忘记在JFrame上调用pack(),然后再将其设置为可见,但在添加组件后。

7)此外,我发现此评论可疑://Creating 1st JFrame请阅读The Use of Multiple JFrames, Good/Bad Practice?。而是使用CardLayoutJDialog

8)我无法看到setLocation需要完全展开JFrame

以下是包含修复程序的代码(我刚使用了所有组件默认LayoutManager s(panel1除外,它使用BorderLayout,因此JPanel将扩展到容器大小):< / p>

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class Test {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }

    public Test() {
        createAndShowGui();
    }

    public void createAndShowGui() {

        final JFrame employeeFrame = new JFrame("Report");
        employeeFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        final JPanel panel = new JPanel() {
            @Override
            public Dimension getMinimumSize() {
                return new Dimension(250, 0);
            }
        };
        final JTable table2 = new JTable(new String[][]{
                    {"one", "two", "three"},
                    {"one", "two", "three"},
                    {"one", "two", "three"}
                }, new String[]{"col1", "col2", "col3"});

        JPanel panel1 = new JPanel(new BorderLayout());//panel which holds jtable
        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 createAndShowGui = new DbUtility();
                 createAndShowGui.loadDriver();
                 createAndShowGui.connect();

                 String value = (text1.getText());
                 String value2 = (text2.getText());
                 ResultSet rs = createAndShowGui.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);

                 }
                 } catch (SQLException e1) {
                 e1.printStackTrace();
                 }
                 */
            }
        });

        panel.add(label1);
        label1.setForeground(Color.white);
        panel.add(text1);
        panel.add(label2);
        label2.setForeground(Color.white);
        panel.add(text2);
        panel.add(ADD);

        JScrollPane pane = new JScrollPane(table2);
        panel1.add(pane);
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel, panel1);
        splitPane.setDividerLocation(300);

        employeeFrame.add(splitPane);

        employeeFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
        //employeeFrame.setLocation(100, 50);
        employeeFrame.pack();

        employeeFrame.setVisible(true);
        employeeFrame.setResizable(false);//or else it wont work nicely withn JFrame#setExtendedState
    }
}

答案 1 :(得分:0)

通过查看您的代码,我无法找到您将Table放置在实际上在SplitPane中添加的Panel或Pane中的位置。