Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

table display problem in panel

843859Aug 30 2008
summary+_

data of table is accessed with the help of cachedrowset, resultset etc. from the database tables.
condition 1:

when i make a table with the help of a model that extends AbstractDataModel
add table in a scrollpane,
add scollpane to a panel,
add panel to frame.
table width is equal to the width of panel and frame.
it do not depend on number of columns in table,
column width are adjusting in scrollpane.
scrollpane adjusts its display on complete panel.

condition 2:
but

when i make a table with the help of a model that extends AbstractDataModel
add table in a scrollpane,
add scollpane to a panel,
add panel to a tabbed pane,
add tabbed pane to a panel,
add panel to frame.

table display only in center of the panel as a square.
table width is approximately half of the width of panel and frame.
it do not depend on number of columns in table,
column width are adjusting in scrollpane.
*scrollpane do not adjust its display on complete panel,*
although number of columns is more.
what is the problem.

code1: first condition
public class SR
{  
 public static void main(String[] args)
 {  
  JFrame frame = new ResultSetFrame();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
 }
}

class ResultSetFrame extends JFrame
{  
 public ResultSetFrame()
 {  
  setTitle("ResultSet");
  setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

  columnNames = new JComboBox();
  columnTotal = new JTextField(10);

  try
  {
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   con = DriverManager.getConnection("jdbc:odbc:new","sa","");
   stat= con.prepareStatement("select * from timetable");
   rs= stat.executeQuery();
   ResultSetMetaData rsmd=rs.getMetaData();
   columnCount= rsmd.getColumnCount();

   for(int col=1;col<=columnCount;col++)
   {
    columnNames.addItem(rsmd.getColumnName(col));
   }

   button=new JButton("SELECT");

   button.addActionListener(new ActionListener()
   {
    public void actionPerformed(ActionEvent event)
    {
     try
     {
      try
      {
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       con2= DriverManager.getConnection("jdbc:odbc:new","sa","");
       con3= DriverManager.getConnection("jdbc:odbc:new","sa","");
       columnName= (String) columnNames.getSelectedItem();
       stat2= con2.prepareStatement("select * from timetable order by "+columnName+" asc");
       stat3= con3.prepareStatement("select * from timetable order by "+columnName+" asc");
       rs2= stat2.executeQuery();
       rs3= stat3.executeQuery();
       rsmd2=rs2.getMetaData();
      }
      catch(Exception evvvvv)
      {
       System.out.println(evvvvv);
      }

      if (scrollPane != null)
      {
       remove(scrollPane);
      }

      CachedRowSet crs = new CachedRowSetImpl();
      crs.populate(rs2);
      rowsmd=(RowSetMetaData)crs.getMetaData();
      rowsmd.setColumnName(2,"xxx");

      model = new RSTModel(crs);

      int total=model.getRowCount();
      columnTotal.setText(""+total);
      JTable table = new JTable(model);
      table.setSize(10,10);
      table.setBackground(Color.cyan.brighter());
      table.setForeground(Color.blue.darker());
      table.getTableHeader().setBackground(Color.green.brighter());
      table.getTableHeader().setForeground(Color.red.darker());
      table.getTableHeader().setFont(fb);
      scrollPane = new JScrollPane(table);
      add(scrollPane, BorderLayout.CENTER);
      validate();
     }
    
     catch(SQLException e)
     {
      e.printStackTrace();
     }
    }
   });

   addWindowListener(new WindowAdapter()
   {
    public void windowClosing(WindowEvent event)
    {
     try
     {
      if (con!= null)
      {
       con.close();
      }
     }
   
     catch (SQLException e)
     {
      e.printStackTrace();
     }              
    }
   });
  }

  catch(Exception evv)
  {
   System.out.println(evv);
  }

  JPanel p = new JPanel();
  p.add(columnNames);
  p.add(button);
  p.add(columnTotal);
  add(p, BorderLayout.NORTH);
 }

 private JScrollPane scrollPane;
 private RSTModel model;
 private JComboBox columnNames;
 private ResultSet rs,rs2,rs3;
 private ResultSetMetaData rsmd,rsmd2;
 private Connection con,con2,con3;
 private boolean scrolling;
 private static final int DEFAULT_WIDTH = 400;
 private static final int DEFAULT_HEIGHT = 300;
 private PreparedStatement stat,stat2,stat3;
 private String columnName;
 private JTextField columnTotal;
 private JButton button;
 private Font fb= new Font("Times New Roman", Font.BOLD+Font.ITALIC,15);
 private int columnCount;
 private CachedRowSet rows;
 private RowSetMetaData rowsmd;
}


class RSTModel extends AbstractTableModel
{  
 public RSTModel(ResultSet aResultSet)
 {  
  rs = aResultSet;

  try
  {  
   rsmd = rs.getMetaData();
  }

  catch (SQLException e)
  {  
   e.printStackTrace();
  }
 }

 public String getColumnName(int c)
 {  
  try
  {  
   return rsmd.getColumnName(c + 1);
  }

  catch (SQLException e)
  {  
   e.printStackTrace();
   return "";
  }
 }

 public int getColumnCount()
 {  
  try
  {  
   return rsmd.getColumnCount();
  }

  catch (SQLException e)
  {  
   e.printStackTrace();
   return 0;
  }
 }

 public Object getValueAt(int r, int c)
 {  
  try
  {  
   rs.absolute(r + 1);
   return rs.getObject(c + 1);
  }

  catch(SQLException e)
  {  
   e.printStackTrace();
   return null;
  }
 }

 public int getRowCount()
 {  
  try
  {  
   rs.last();
   return rs.getRow();
  }

  catch(SQLException e)
  {  
   e.printStackTrace();
   return 0;
  }
 }

 private ResultSet rs;
 private ResultSetMetaData rsmd;
}
code2: second condition
public class Home2
{
 public static void main(String[] args)
 {  
  Home2Frame home2frame = new Home2Frame();
  home2frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  home2frame.show();
 }
}

class Home2Frame extends JFrame
{  
 public Home2Frame()
 {  
  setTitle("SUNDARAM EDUCORE REPORTS");
  setSize(WIDTH, HEIGHT);
  setResizable(false);

  ImageIcon studicon    = createImageIcon("stud.png");

  JPanel gridPanel = new JPanel();
  gridPanel.setLayout(new GridLayout(1, 1));

  JTabbedPane tabbedPane = new JTabbedPane();

  tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
  gridPanel.add(tabbedPane);

  StudRepPanel studreppanel   =new StudRepPanel();

  tabbedPane.addTab("Student", studicon, studreppanel);

  Container container= getContentPane();
  container.add(gridPanel);
 }

 protected static ImageIcon createImageIcon(String path)
 {
  java.net.URL imgURL = IconDemo.class.getResource(path);
  if (imgURL != null)
  {
   return new ImageIcon(imgURL);
  }
  else
  {
   System.err.println("Couldn't find file: " + path);
   return null;
  }
 }   

 public static final int WIDTH = 1024;
 public static final int HEIGHT = 768;  

}





class StudRepPanel extends JPanel
{
 public StudRepPanel()
 {
  setLayout(new GridLayout(1, 1));

  ImageIcon genicon     = createImageIcon("studgen.png");
   JTabbedPane studreptabbedPane = new JTabbedPane();
  studreptabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
  add(studreptabbedPane);

  GenStudRepPanel genstudreppanel =new GenStudRepPanel();
  studreptabbedPane.addTab("General", genicon, genstudreppanel);
}
 protected static ImageIcon createImageIcon(String path)
 {
  java.net.URL imgURL = IconDemo.class.getResource(path);
  if (imgURL != null)
  {
   return new ImageIcon(imgURL);
  }
  else
  {
   return null;
  }
 }   
}




class GenStudRepPanel extends JPanel
{
 public GenStudRepPanel()
 {
  columnNames = new JComboBox();
  columnNames2 = new JComboBox();
  columnTotal = new JTextField(10);
  button=new JButton("SELECT");

  try
  {
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   con = DriverManager.getConnection("jdbc:odbc:new","sa","");

   stat= con.prepareStatement("select * from timetable");

   rs= stat.executeQuery();

   ResultSetMetaData rsmd=rs.getMetaData();

   columnCount= rsmd.getColumnCount();

   for(int col=1;col<=columnCount;col++)
   {
    columnNames.addItem(rsmd.getColumnName(col));
   }


   button.addActionListener(new ActionListener()
   {
    public void actionPerformed(ActionEvent event)
    {
     try
     {
      try
      {
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       con2= DriverManager.getConnection("jdbc:odbc:new","sa","");
       con3= DriverManager.getConnection("jdbc:odbc:new","sa","");
       columnName= (String) columnNames.getSelectedItem();
       stat2= con2.prepareStatement("select * from timetable order by "+columnName+" asc");
       stat3= con3.prepareStatement("select * from timetable order by "+columnName+" asc");
       rs2= stat2.executeQuery();
       rs3= stat3.executeQuery();
       rsmd2=rs2.getMetaData();
      }
      catch(Exception evvvvv)
      {
       System.out.println(evvvvv);
      }

      if (scrollPane != null)
      {
       remove(scrollPane);
      }

      CachedRowSet crs = new CachedRowSetImpl();
      crs.populate(rs2);
      rowsmd=(RowSetMetaData)crs.getMetaData();
      rowsmd.setColumnName(2,"xxx");

      model = new RSTModel(crs);

      int total=model.getRowCount();
      columnTotal.setText(""+total);
      JTable table = new JTable(model);
      table.setSize(600,600);
      table.setBackground(Color.cyan.brighter());
      table.setForeground(Color.blue.darker());
      table.getTableHeader().setBackground(Color.green.brighter());
      table.getTableHeader().setForeground(Color.red.darker());
      table.getTableHeader().setFont(fb);
      table.setRowHeight(20);


      scrollPane = new JScrollPane(table);
      scrollPane.setSize(600,600);
      add(scrollPane);
//      add(scrollPane, BorderLayout.CENTER);

      validate();
     }
    
     catch(SQLException e)
     {
      e.printStackTrace();
     }
    }
   });
  }

  catch(Exception evv)
  {
   System.out.println(evv);
  }
 }

 private JScrollPane scrollPane;
 private RSTModel model;
 private JComboBox columnNames,columnNames2;
 private ResultSet rs,rs2,rs3;
 private ResultSetMetaData rsmd,rsmd2;
 private Connection con,con2,con3;
 private boolean scrolling;
 private static final int DEFAULT_WIDTH = 400;
 private static final int DEFAULT_HEIGHT = 300;
 private PreparedStatement stat,stat2,stat3;
 private String columnName;
 private JTextField columnTotal;
 private JButton button;
 private Font fb= new Font("Times New Roman", Font.BOLD+Font.ITALIC,15);
 private int columnCount;
 private CachedRowSet rows;
 private RowSetMetaData rowsmd;
}
class RSTModel extends AbstractTableModel
{  
 public RSTModel(ResultSet aResultSet)
 {  
  rs = aResultSet;

  try
  {  
   rsmd = rs.getMetaData();
  }

  catch (SQLException e)
  {  
   e.printStackTrace();
  }
 }

 public String getColumnName(int c)
 {  
  try
  {  
   return rsmd.getColumnName(c + 1);
  }

  catch (SQLException e)
  {  
   e.printStackTrace();
   return "";
  }
 }

 public int getColumnCount()
 {  
  try
  {  
   return rsmd.getColumnCount();
  }

  catch (SQLException e)
  {  
   e.printStackTrace();
   return 0;
  }
 }

 public Object getValueAt(int r, int c)
 {  
  try
  {  
   rs.absolute(r + 1);
   return rs.getObject(c + 1);
  }

  catch(SQLException e)
  {  
   e.printStackTrace();
   return null;
  }
 }

 public int getRowCount()
 {  
  try
  {  
   rs.last();
   return rs.getRow();
  }

  catch(SQLException e)
  {  
   e.printStackTrace();
   return 0;
  }
 }

 private ResultSet rs;
 private ResultSetMetaData rsmd;
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 27 2008
Added on Aug 30 2008
0 comments
156 views