Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By camickr

Thread: Adding dynamic Jpanel

  1. #1
    seth is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default Adding dynamic Jpanel

    Hi All,

    I want to display the one JPanel dynamically when I want to clink on row of a JTable but its not turning out for me.I tried to do it via revalidate() and repaint() method.

    there must be something I am missing but not sure what it is.Any help would be greatly appreciated

    I guess the code under addMouseListener() has the problem

    Below is the code for your reference

    Java Code:
    package test;
    
    import java.awt.*;  
    import java.awt.event.*;  
    import java.sql.*;
    
    import javax.swing.*;  
    import javax.swing.event.*;  
    import javax.swing.table.JTableHeader;
       
    public class TableListener extends JFrame implements Scrollable  
     {  
    	JTable table;
    	JPanel midpanel,pane,pane1,pane2;
    	supportAts.GBHelper pospanel;
    	supportAts.Gap gappanel;
    	JTableHeader header;
    	JScrollPane scroll;
    	JScrollPane scrollmidpane;
    	JButton test;
    	public TableListener()
    	{
    	     int arrrow=0;
    	     JScrollPane mainscroll;
               
            try
            {
            
            Connection con=DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=D:/g/Myprojects/AMPS Incidents-Requests Log July 2011"," ","ATSlog");
            
             PreparedStatement pst=con.prepareStatement("Select * from July where Respondant=?",ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
             pst.setString(1, "Srikanth Bandi");
             ResultSet rs=pst.executeQuery();
            
            
            if(rs.last())
            {
           	 
           	 arrrow=rs.getRow();
           	 rs.beforeFirst();
           
            }
          
            String dynarr[][]=new String[arrrow][39];
            int rowset=1;
            rs.next();
            for(int i=0;i<arrrow;i++)
            {
           	 rowset=1;
           	 
           	 for(int j=0;j<39;j++)
           	 {
           		
           		 dynarr[i][j]=rs.getString(rowset);
           		 System.out.println("Contents of the array is"+dynarr[i][j]);
           	     
           	     rowset++;
           	 }
           	 
           	 rs.next(); 
            }
           
            String[] headers = {"Request Number","Problem Request Description","Request Recieved","Remedy Project Number","Time Reporting Number",
           		 "Severity","Application","Platform","Request Issue Date","Request IssueTime","Date to Complete Signoff",
           		 "Response Date","Response Time","Response SLAMet","Recovery Date","Recovery Time","Recovery SLA Met",
           		 "Resolution Date","Resolution Time","Resolution SLA Met","Actual Time Spent","Respondant","Secondary Respondant",
           		 "Secondary Respondant TimeSpent","Status","Comments Activity","Comments Justification","ModelSol","SignOff",
           		 "Time to Respond","Time to Recover","Time to Resolve","SME TIme","Production Incident","Problem Type",
           		 "Request Issue Date Time","Response Date Time","Recovery Date Time","Resolution Date Time"
            					};  
             
            table = new JTable(dynarr, headers);  
            scroll = new JScrollPane(table);
            table.setPreferredScrollableViewportSize(new Dimension(500, 20));
            table.setFillsViewportHeight(true);
    
    
            header = table.getTableHeader();
            header.setFont(new Font("serif",Font.BOLD,14));
           
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            table.setFillsViewportHeight(false);
            pane1=new JPanel(new GridBagLayout());
            
            pospanel=new supportAts.GBHelper();
            gappanel=new supportAts.Gap(25);
            
             scroll.setMinimumSize(new Dimension(1000,100));
          
            
            table.addMouseListener(new MouseAdapter()
            {
            	public void mouseClicked(MouseEvent e)
            	{
            		if(table.getSelectedRowCount()>1)
                	{
                		JOptionPane.showMessageDialog(null, "Select one row at a time","Error",JOptionPane.OK_OPTION);
                	}
                	else
                	{
                		System.out.println("I am in else part");
                		System.out.println(e.getSource().getClass());  
                        System.out.println("Selected row"+table.getSelectedRow()+"Row Count"+table.getSelectedRowCount());
                        
                        	if(table.getSelectedRowCount()>1)
                        	{
                        		JOptionPane.showMessageDialog(null, "Select one row at a time","Error",JOptionPane.OK_OPTION);
                        	}
                        	else
                        	{
                        		System.out.println("I am in else part");
                        		midpanel=new JPanel();
                        		midpanel.setBorder(BorderFactory.createEmptyBorder(0, 25, 100, 25));
                                
                                test=new JButton("test");
                                midpanel.add(test);
                        		midpanel.setBackground(Color.blue);
                                scrollmidpane=new JScrollPane(midpanel);
                                scrollmidpane.revalidate();
                               
                                JOptionPane.showMessageDialog(null, "Just for test","Error",JOptionPane.OK_OPTION);
                                
                	       }	
            	      }
            	}
    			
            });
            
            midpanel=new SCreateEntry().SCreateEntry();
            scrollmidpane=new JScrollPane(midpanel);
            midpanel.setBorder(BorderFactory.createEmptyBorder(0, 25, 100, 25));
           
            scrollmidpane.setMinimumSize(new Dimension(1000,750));
            midpanel.setPreferredSize(new Dimension(900,750));
            
          
            
            pane1.add(scroll,pospanel);
           
            pane1.add(scrollmidpane,pospanel.nextRow().expandH());
            pane1.add(new supportAts.Gap(),pospanel.width().expandH());
            
            
            
            mainscroll=new JScrollPane();
            mainscroll.add(pane1);
            getContentPane().add(pane1);
            setBounds(0, 0, 1024, 768);
            setVisible(true);
           
            con.close();
            }
            catch(SQLException ae){ae.printStackTrace();}
            
    	
    	}
    public static void main(String[] args) throws Exception  
         {  
    	   
    	try 
    		 {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                new TableListener();
                
             } 
    		catch (Exception unused) {}
              
         }   
    
    public Dimension getPreferredScrollableViewportSize() {return null;}
    public int getScrollableBlockIncrement(Rectangle arg0, int arg1, int arg2) {return 0;}
    public boolean getScrollableTracksViewportHeight() {return false;}
    public boolean getScrollableTracksViewportWidth() {return false;}
    public int getScrollableUnitIncrement(Rectangle arg0, int arg1, int arg2) {return 0;}  
     }
    Last edited by seth; 09-09-2011 at 05:18 PM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default Re: Adding dynamic Jpanel

    1) Don't create a new JScrollPane. All that does is create a component in memory, it does not add the component to the GUI.

    2) Don't create a new midPanel. I would guess you want to invoke removeAll() on the midPanel and then add the new component to the midPanel. Something like:

    Java Code:
    midPanel.removeAll();
    midPanel.add( test );
    midPanel.revalidate();

  3. #3
    seth is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default Re: Adding dynamic Jpanel

    I tried adding the Jpanel,adding the component and the tried revalidation but it also does not work

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default Re: Adding dynamic Jpanel

    Yes it does work.

    You need to create a Short, Self Contained, Correct Example to prove to yourself that it does work. So create a simple frame with a panel in the CENTER. Then add a JButton to the SOUTH of the frame. When you click the button add another component to the panel and revalidate the panel. I'm not going to debug the code you posted because I don't have access to your database.
    Fubarable likes this.

Similar Threads

  1. adding dynamic rows to jtable
    By paul4everyone in forum Advanced Java
    Replies: 5
    Last Post: 05-08-2011, 05:37 AM
  2. Adding a jpanel to a customized Jpanel Class
    By trishtren in forum AWT / Swing
    Replies: 7
    Last Post: 04-05-2011, 06:52 PM
  3. Adding Jpanel ontop of another Jpanel
    By Manfizy in forum AWT / Swing
    Replies: 4
    Last Post: 03-05-2011, 10:34 PM
  4. adding integer to the JPanel
    By navid in forum New To Java
    Replies: 2
    Last Post: 12-18-2010, 04:26 PM
  5. dynamic creation of jpanel
    By masa in forum AWT / Swing
    Replies: 1
    Last Post: 12-18-2008, 11:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •