Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    Join Date
    Aug 2008
    Posts
    26
    Rep Power
    0

    Default JScrollPane updation Problem

    I need help with this .
    I'm trying to add component dynamically to a JPanel which is in a JSrollPane
    The new item gets added, but the vertical Scrollbar does not get updated.

    Java Code:
    import java.awt.event.*;
    import java.awt.*;
    
    import javax.swing.*;
    import javax.swing.border.*;
    
    
    public class scrollTest extends JFrame implements ActionListener{
        JPanel jplShow;
        JScrollPane jspShow;
        scrollTest(){
                    JButton jbnadd=new JButton("Add");
                    jbnadd.setBounds(5, 5, 60, 25);
                    jbnadd.addActionListener(this);
                    getContentPane().add(jbnadd);
                    
                    jplShow=new JPanel(); 
                    jplShow.setLayout(null);
                    jspShow=new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                                            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                    jspShow.setViewportView(jplShow);
                    jspShow.setBounds(0,120,670,520);
                    getContentPane().add(jspShow);
                    setVisible(true);
                    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    setMinimumSize(new Dimension(600,500));
        }
        public void actionPerformed(ActionEvent e){
                
                if(e.getActionCommand()=="Add"){
                 
                    int Ycoordinate= jplShow.getComponentCount()*120+20;
                //System.out.println(jplShow.getComponentCount());        
                //    System.out.println(Ycoordinate);                    
                JPanel jplDisplay=new JPanel();
                jplDisplay.setBounds(10, Ycoordinate, 600, 100);
                jplDisplay.setBorder((LineBorder)BorderFactory.createLineBorder(Color.BLACK));            
                jplShow.add(jplDisplay);            
                jplShow.repaint();
                SwingUtilities.updateComponentTreeUI(rootPane);
                }
        }
        public static void main(String args[]){
            new scrollTest();
        }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default

    if(e.getActionCommand()=="Add"){
    Use the equals method here. == compares the contents of variables NOT the objects pointed to if the variables are object references.

  3. #3
    Join Date
    Aug 2008
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Use the equals method here. == compares the contents of variables NOT the objects pointed to if the variables are object references.
    it work the same both way , a JPanel get created on clicking add.

    Problem is :when the jpanels have filled the page(one after another )
    the Scrollbar is to be updated.but here the panels go out of the page

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default

    A bad coding technique. You'll regret using it in the future.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You probably should use a layout manager of some type here for your inner panel, not the null layout. If you do use this, then don't forget to call revalidate on the panel that's receiving the smaller panels after each add.

  6. #6
    Join Date
    Aug 2008
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You probably should use a layout manager of some type here for your inner panel, not the null layout. If you do use this, then don't forget to call revalidate on the panel that's receiving the smaller panels after each add.
    Yes i think the revalidation was important.Null seem less complicated that why i've tried null.

    Here the correct code(including Norms correction):

    Java Code:
    import java.awt.event.*;
    import java.awt.*;
    
    import javax.swing.*;
    import javax.swing.border.*;
    
    
    public class scrollTest extends JFrame implements ActionListener{
        JPanel jplShow;
        JScrollPane jspShow;
        scrollTest(){
                    JButton jbnadd=new JButton("Add");
                    jbnadd.setBounds(5, 5, 60, 25);
                    jbnadd.addActionListener(this);
                    getContentPane().add(jbnadd);
                    
                    jplShow=new JPanel(); 
                    jplShow.setLayout(null);
                    jplShow.setPreferredSize(new Dimension(670,520));
                    jspShow=new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                                            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                    jspShow.setViewportView(jplShow);
                    jspShow.setPreferredSize(new Dimension(670,520));
                    getContentPane().add(jspShow);
                    setVisible(true);
                    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            
                    setPreferredSize(new Dimension(800,700));
                    pack();
        }
        public void actionPerformed(ActionEvent e){
                
                if(e.getActionCommand().equals("Add")){
                 
                    int Ycoordinate= jplShow.getComponentCount()*120+20;    
                    JPanel jplDisplay=new JPanel();
                    jplDisplay.setBounds(10, Ycoordinate, 600, 100);
                    jplDisplay.setBorder((LineBorder)BorderFactory.createLineBorder(Color.BLACK));            
                    jplShow.add(jplDisplay);                  
                    System.out.println(Ycoordinate);    
                    jplShow.setPreferredSize(new Dimension(670,Ycoordinate+100));
                    jplShow.revalidate();
                  
                jplShow.repaint();                        
                }
        }
        public static void main(String args[]){
            new scrollTest();
        }

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    You had this worked out pretty well. I added some suggestions.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    public class ScrollTestRx extends JFrame implements ActionListener{
        JPanel jplShow;
        JScrollPane jspShow;
    
        ScrollTestRx(){
            JButton jbnadd=new JButton("Add");
            // The contentPane layout manager was not set to null
            // so this will not behave correctly, viz, adding the
            // button to the contentPane with setBounds. When you
            // add a component to the conentPane with no constraint
            // it is added to the center section by default (which
            // is okay). Adding another component to the center
            // section will result in the first component being
            // removed and the second component being added. So
            // your button was like an orphan, coming and going.
    //        jbnadd.setBounds(5, 5, 60, 25);
    
            jbnadd.addActionListener(this);
            System.out.println("JFrame default layout = " +
                                getLayout().getClass().getSimpleName());
            // Let the AWT do the work.
            JPanel northPanel = new JPanel();
            System.out.println("JPanel default layout = " +
                                northPanel.getLayout().getClass().getSimpleName());
            northPanel.add(jbnadd);
            getContentPane().add(northPanel, "North");
    
            jplShow=new JPanel();
            jplShow.setLayout(null);
            jplShow.setPreferredSize(new Dimension(670,520));
            jspShow=new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            jspShow.setViewportView(jplShow);
            jspShow.setPreferredSize(new Dimension(670,520));
            getContentPane().add(jspShow);  // default center section
    //        setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setPreferredSize(new Dimension(800,700));
            pack();
            // Always call this last.
            setVisible(true);
        }
    
        public void actionPerformed(ActionEvent e){
            if(e.getActionCommand().equals("Add")){
                // Your layout management section.
                int compCount = jplShow.getComponentCount();
                int panelHeight = 120;
                int margin = 20;
                int yCoordinate= compCount*panelHeight + margin;
                JPanel jplDisplay=new JPanel(new BorderLayout());
                String s = "<html>panel " + (jplShow.getComponentCount()+1) +
                           "<br>yCoordinate = " + yCoordinate;
                jplDisplay.add(new JLabel(s, JLabel.CENTER));
                jplDisplay.setBounds(10, yCoordinate, 600, 100);
                jplDisplay.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                jplShow.add(jplDisplay);
                System.out.println(yCoordinate);
                int maxY = yCoordinate + panelHeight;
                jplShow.setPreferredSize(new Dimension(670,maxY));
                jplShow.revalidate();
    
                jplShow.repaint();
            }
        }
    
        public static void main(String[] args){
            new ScrollTestRx();
        }
    }
    For follow–up information on Norm's exhortation: How should I compare String objects?.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Another way to do this using layout managers:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ScrollTest2 // doesn't subclass JFrame: 
    {                        // favor composition over inheritance
      // avoiding magic numbers, make some dimensions constants
      private static final Dimension SCROLLPANE_SIZE = new Dimension(650, 600);
      public static final Dimension ADDED_PANEL_SIZE = new Dimension(600, 100);
      private static final int GAP = 20;
      private JPanel mainPanel = new JPanel(); // the application's main panel
      
      // panel to hold addedPanels.  Uses GridLayout with 1 column and 
      // "0" rows -- meaning number of rows is undefined.
      private JPanel innerPanel = new JPanel(new GridLayout(0, 1, 0, GAP)); 
      
      public ScrollTest2()
      {
        // Give the inner panel an empty border that pushes 
        // things over to the right a few pixels
        innerPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
    
        // put the innerPanel that uses GridLayout inside of a JPanel using FlowLayout
        // This prevents the first addedPanels added from being too large
        JPanel flowPanel = new JPanel(new FlowLayout());
        flowPanel.add(innerPanel);
        JScrollPane scrollpane = new JScrollPane(flowPanel);
        scrollpane.setPreferredSize(SCROLLPANE_SIZE);
        
        JButton addBtn = new JButton("Add");
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(addBtn);
        addBtn.addActionListener(new AddBtnListener());
        
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(scrollpane, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.NORTH);
      }
      
      public JComponent getComponent()
      {
        return mainPanel;
      }
      
      private class AddBtnListener implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          JPanel addedPanel = new JPanel();
          addedPanel.setPreferredSize(ADDED_PANEL_SIZE);
          addedPanel.setBorder(BorderFactory.createLineBorder(Color.black));
          innerPanel.add(addedPanel);
          innerPanel.revalidate();
        }
      }
      
      private static void createAndShowGUI()
      {
        JFrame frame = new JFrame("ScrollTest2 Application"); // JFrame to hold our mainPanel
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // so app ends when window closes
        
        // add the mainPanel to the JFrame's contentPane
        frame.getContentPane().add(new ScrollTest2().getComponent());
        frame.pack();
        frame.setLocationRelativeTo(null); // center everything
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        // Call Swing App in a thread-safe manner
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowGUI();
          }
        });
      }
    }
    Last edited by Fubarable; 09-23-2008 at 11:24 PM.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Cross-post: Sun Java Forums

    Please do not cross-post questions in multiple forums. This will frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread.
    Last edited by Fubarable; 09-23-2008 at 11:47 PM.

  10. #10
    Join Date
    Aug 2008
    Posts
    26
    Rep Power
    0

    Default

    Thanks all ,for your suggestions.

    Quote Originally Posted by Fubarable View Post

    Please do not cross-post questions in multiple forums. This will frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread.
    Wel i had tried this thing for hours after which it got frustrating for me. I then did a search on the net and found nothing useful. so i posted its here and waited.I needed this thing kind of urgently.So i tried forums.sun.com and i got an little idea from there.
    If its an offense i apologize for it Funurable.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you have to cross-post, the wise thing to do would be to do this as transparently as possible. This means state up front that you are cross-posting with a link to the cross-post. This would help to ensure that no one would be wasting their time answering you -- which would help you as this is not a reputation you want to get.
    Last edited by Fubarable; 09-24-2008 at 04:24 AM.

  12. #12
    Join Date
    Aug 2008
    Posts
    26
    Rep Power
    0

    Default Problem 2

    Well here i tried to use vectors for JTable and added a seperate mouselistener
    (jtable.elementAt(count).addMouseListener(new MouseAdapter()). Tried many ways but it still act as though its the same listener is there for the all tables.

    What am i doing wrong?


    <Harwired's modified code, modified again>

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    import javax.swing.JTable;
    import javax.swing.event.*;
    import javax.swing.table.*;
    
    import java.util.Vector;
    
    public class ScrollTestRx extends JFrame implements ActionListener{
        JPanel jplShow;
        JScrollPane jspShow;
        String []sInitial={" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "};  
        int count;    
         Vector<JTable> jtable;
        ScrollTestRx(){
            
            count=0;
            jtable=new Vector<JTable>(1);
            JButton jbnadd=new JButton("Add");
            
            
            jbnadd.addActionListener(this);
            System.out.println("JFrame default layout = " +
                                getLayout().getClass().getSimpleName());
            // Let the AWT do the work.
            JPanel northPanel = new JPanel();
            System.out.println("JPanel default layout = " +
                                northPanel.getLayout().getClass().getSimpleName());
            northPanel.add(jbnadd);
            getContentPane().add(northPanel, "North");
    
            jplShow=new JPanel();
            jplShow.setLayout(null);
            jplShow.setPreferredSize(new Dimension(670,520));
            jspShow=new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            jspShow.setViewportView(jplShow);
            jspShow.setPreferredSize(new Dimension(670,520));
            getContentPane().add(jspShow);  // default center section
    //        setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setPreferredSize(new Dimension(800,700));
            pack();
            // Always call this last.
            setVisible(true);
        }
    
        public void actionPerformed(ActionEvent e){
            if(e.getActionCommand().equals("Add")){
                // Your layout management section.
                
                int compCount = jplShow.getComponentCount();
                int panelHeight = 120;
                int margin = 20;
                int yCoordinate= compCount*panelHeight + margin;
                JPanel jplDisplay=new JPanel(new BorderLayout());
               // String s = "<html>panel " + (jplShow.getComponentCount()+1) +
               //            "<br>yCoordinate = " + yCoordinate;
              //  jplDisplay.add(new JLabel(s, JLabel.CENTER));
                
                //ADDING A TABLE
                        final String[] names = {"beat1"," "," "," ","Beat 2"," "," "," ",
                                "Beat3"," "," "," ","Beat4"," "," "," "};
                        final Object[][] data = {  sInitial    };
                        TableModel AbModel = new AbstractTableModel() {
                        public int getColumnCount() { return names.length; }
                        public int getRowCount() { return data.length;}
                        public Object getValueAt(int row, int col) { return data[row][col]; }
                        public String getColumnName(int column) {return names[column];}
                        public Class getColumnClass(int col) {return getValueAt(0,col).getClass();}
                        public void setValueAt(Object aValue, int row, int column) {data[row][column] = aValue;}
                        };      
                        jtable.add(count, new JTable(AbModel));
                        String temp=String.valueOf(count);
                        jtable.elementAt(count).setName(temp);
                        System.out.println("Name:"+jtable.elementAt(count).getName());                   
                        jtable.elementAt(count).addMouseListener(new MouseAdapter(){
                public void mouseClicked(MouseEvent e){
                if (e.getClickCount() ==2){          
                    String stemp=e.getComponent().getName();//gettting name of element
                    int itemp=Integer.parseInt(stemp);
                    JTable jtTemp =(JTable)e.getSource();
                    System.out.println(" double clicked Table "+e.getComponent().getName());
                    int selcol=jtable.get(itemp).getSelectedColumn();
                    int selrow=jtable.get(itemp).getSelectedRow();                 
                    System.out.println("Column:"+(selcol+1)+" has been selected");
                    String newval=null;
                    Object oldval=jtable.get(itemp).getValueAt(selrow, selcol);
                    if(oldval==" "){ newval="|S|";}//S=Slow hit H=Hard Hit 
                    else if(oldval=="|S|"){ newval="|H|";}
                    else if(oldval=="|H|"){ newval=" ";}
                    sInitial[selcol]=newval;
                    jtable.get(itemp).setValueAt(newval, selrow, selcol);       
                    
                    jtable.get(itemp).repaint();
                    
                    }           
                }
                } );  
                      
                        JScrollPane jspTable=new JScrollPane(jtable.elementAt(count));
                        jspTable.setPreferredSize(new Dimension(520, 50));
                        
                        jplDisplay.add(jspTable);
                        
                jplDisplay.setBounds(10, yCoordinate, 600, 100);
                jplDisplay.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                jplShow.add(jplDisplay);
                System.out.println(yCoordinate);
                int maxY = yCoordinate + panelHeight;
                jplShow.setPreferredSize(new Dimension(670,maxY));
                jplShow.revalidate();
                count++;
            }
        }
    
        public static void main(String[] args){
            new ScrollTestRx();
        }
    }
    Last edited by goodwillwins; 09-27-2008 at 04:48 PM. Reason: String temp=""+count;\\bad coding technique

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default

    it still act as though its the same listener is there for the all tables.
    How are the listeners different so that you can say if they are the same or different? Add a
    println("ML=" + this + ", ME=" + e)
    to the listener to see where/what is happening.
    Also use @Override before the listener method to be sure that you have coded it correctly. A misspelled method signature will compile but will never be executed.

  14. #14
    Join Date
    Aug 2008
    Posts
    26
    Rep Power
    0

    Default

    [QUOTE=Norm;36095]How are the listeners different so that you can say if they are the same or different? Add a
    println("ML=" + this + ", ME=" + e)
    to the listener to see where/what is happening.
    Also use @Override before the listener method to be sure that you have coded it correctly.


    This is the output:

    ML=ScrollTestRx$2@1bf6770, ME=java.awt.event.MouseEvent[MOUSE_CLICKED,(230,8),absolute(247,113),button=1,m odifiers=Button1,clickCount=2] on 0
    double clicked Table 0
    Column:7 has been selected
    ML=ScrollTestRx$2@94948a, ME=java.awt.event.MouseEvent[MOUSE_CLICKED,(131,9),absolute(148,234),button=1,m odifiers=Button1,clickCount=2] on 1
    double clicked Table 1
    Column:4 has been selected

    i guess they are not the same.
    I didn't get you on the overridde part.How do i use override.
    Last edited by goodwillwins; 09-26-2008 at 06:25 PM.

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default

    jtable.elementAt(count).addMouseListener(new MouseAdapter(){
    @Override
    public void mouseClicked(MouseEvent e){

    Since your code is being executed, this obviously isn't needed.
    The first time you code an Adapter, its a good idea to put it so that the compiler will check that your are really overriding the method. Perhaps you don't need it with an IDE.
    Last edited by Norm; 09-26-2008 at 06:29 PM.

  16. #16
    Join Date
    Aug 2008
    Posts
    26
    Rep Power
    0

    Default

    ok.

    println("ML=" + this + ", ME=" + e);

    this is quite a nice way to check the the event ID. thanks

    I do use an IDE .But will remember that.
    Last edited by goodwillwins; 09-26-2008 at 06:52 PM.

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default

    A lot of objects override the toString() method to give useful debugging info. The output you see when println(e) is from e's toString()

  18. #18
    Join Date
    Aug 2008
    Posts
    26
    Rep Power
    0

    Smile

    Quote Originally Posted by goodwillwins View Post
    Well here i tried to use vectors for JTable and added a seperate mouselistener
    (jtable.elementAt(count).addMouseListener(new MouseAdapter()). Tried many ways but it still act as though its the same listener is there for the all tables.

    What am i doing wrong?

    [/CODE]
    Has anyone come up with any solution???

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default

    Can you restate the problem. I thought you determined that a different listener was being called.

  20. #20
    Join Date
    Aug 2008
    Posts
    26
    Rep Power
    0

    Default

    Considering i have many tables(Vecctor) each having a different mouse listener,when a cell in one of the table is changed the others tables should remain unaffected.
    But this is not the case, the cells of other tables also seem to get updated.
    Last edited by goodwillwins; 09-27-2008 at 05:21 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. JScrollPane And ArrowKeys
    By arpitvavadia in forum AWT / Swing
    Replies: 2
    Last Post: 09-04-2008, 08:22 PM
  2. [SOLVED] JScrollPane - HELP!
    By terox13 in forum AWT / Swing
    Replies: 8
    Last Post: 05-10-2008, 03:58 AM
  3. JScrollPane with HTML
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-14-2008, 11:32 AM
  4. jscrollpane problem
    By monkey04 in forum AWT / Swing
    Replies: 2
    Last Post: 01-19-2008, 05:23 AM
  5. help with JScrollPane
    By tommy in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 07:58 PM

Posting Permissions

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