1

Hii i am doing a project in swing . i want to display my data in JTable from database.I
created JSplitpane. In panel 1 created search button,jLabel and JTextBox for sect data from Databse...in Panel 2 i want to show search result in JTable.my database query and all working well..But i couldn't display JTable in panel2...anyone can help me to solve the issue.

public JFrame TimesheetReport(){ //Creating 1st 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;

       }
learner
  • 331
  • 1
  • 9
  • 22

2 Answers2

3

But i couldn't display JTable in panel2...anyone can help me to solve the issue.

Your code is not in SSCCE format... It does not compile on a simple copy and paste and at that there is an error as table2 is not declared within the methods scope of given code also there isnt any reference to panel2 but I think you meant panel1...

Solution:

1) Dont use null/Absolute LayoutManager.

Have a read on:

2) Why do you need to add JTable to panel1 (or as quoted in the question panel2)? If anything we would add the JScrollPane which has the JTable as its viewport topanel1 Simply add table2 to JScrollPane and add JScrollPane to JSplitPane i.e:

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

Alternatively add JTable to JScrollPane and than add JScrollPane to panel1 with BorderLayout so JPanel will stretch to container size:

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

3) Also because I see you maximize the JFrame via:

JFrame#setExtendedState(JFrame.MAXIMIZED_BOTH);

it would be wise to use a LayoutManager which auto resizes components when the JFrame size is changed. i.e BorderLayout,GridLayout and GridBagLayout are a few that come to mind.

4) Also dont set the JFrame visible before all components are added.

5) Create Swing components on Event Dispatch Thread via SwingUtilities.invokeLater(Runnable r) block.

6) Dont forget to call pack() on JFrame before setting it visible but after adding components.

7) Also I find this comment suspicous: //Creating 1st JFrame Have a read on The Use of Multiple JFrames, Good/Bad Practice?. Rather use CardLayout or JDialog.

8) I cant see the need for setLocation with a fully extended JFrame.

Here is your code with fixes (I just used all components default LayoutManagers (except panel1 which uses BorderLayout so JPanel will stretch to container size):

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
    }
}
Community
  • 1
  • 1
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • @radhu well as you can see there are many other points I have listed all of which can cause many problems. But trust me null layout is the biggest – David Kroukamp Feb 15 '13 at 12:56
  • Sir i tried your code..Itz working when we manually adding JTable..But i want to select data from database when i click search button.and display that result in Jtable.. am getting resusult..i think query is working.but still Jtable not displaying..i dnt noe whatz wrong in code.. – learner Feb 15 '13 at 13:40
  • *still Jtable not displaying* I think actually the data is not being displayed on the table. Im not sure thats got to do with this question. But I see you are blocking the [EDT](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html) by doing long runnin task like db query. Rather use [SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html). I see you create a new `JTable` with a new `DefaultTableModel`. You actually want to update the existing *model* of your currently visible table. See [this](http://stackoverflow.com/a/12646338/1133011) example – David Kroukamp Feb 15 '13 at 14:01
0

By looking at your code, I can not find a place where you are placing the Table in either the Panel or the Pane which you actually are adding in SplitPane.

SSC
  • 2,956
  • 3
  • 27
  • 43
  • See here `JScrollPane pane=new JScrollPane(table2); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,panel,pane );` – David Kroukamp Feb 15 '13 at 12:44