Results 1 to 5 of 5

Thread: who make the JScrollPane work ???

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 1999
    Location
    Belgium
    Posts
    98

    who make the JScrollPane work ???

    Hi,

    In my example I put a JTable on a JFrame. I also put a JScrollPane to the JTable.
    I give the JTable a 100 rows with some values. For some reason I can 't scroll.
    Is it possible to make it work in this example because I really want to keep the line "contentPane.setLayout(null);" and not use another layout.

    Thanks for reading and maybe solving my problem


    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Interface extends JFrame {
    
    
        public Interface() {
        	
            Container contentPane = getContentPane();
            
            contentPane.setLayout(null);
            
            JTable t1 = new JTable(100, 5);
    
            JScrollPane s1 = new JScrollPane(t1);
    
            contentPane.add(t1);
            
            contentPane.add(s1);
    
            // Object.setBounds(left, top, with, height);
            
            t1.setBounds(1, 100, 500, 500);
            
            s1.setBounds(501, 100, 20, 500);
            
            for(int i = 0; i < 100; i++) {
            	
            	t1.setValueAt("Value" + i,i,0);
            	
            }
            
            
            
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    		
    		int w = d.width;
    		
    		int h = d.height;
    		
            this.setSize((int) (((double) w) * 0.9),(int) (((double) h) * 0.9));
            
            this.setLocation((int) (((double) w) * 0.05),(int) (((double) h) * 0.05));
            
            this.setBackground(Color.lightGray );
            
            this.setResizable(false);
    
        	this.setVisible(true);
    
    
           this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
            System.exit(0);
            }
            });
        }
    
        public static void main(String args[]) {
            Interface window = new Interface();
     
        }
    }

  2. #2
    Addicted Member Mrs Kensington's Avatar
    Join Date
    Sep 2001
    Location
    Dorset, UK
    Posts
    144
    You only need to add the Scrollpane to your container and not the table as its contained in the ScrollPane.

    So just remove these lines
    Code:
    contentPane.add(t1);        
    t1.setBounds(1, 100, 500, 500);
    and you'll probably want to set the size of the scrollPane to the size of the table.

    e.g.
    Code:
    s1.setBounds(1, 100, 500, 500);
    That should then work fine.
    Ford? Theres an infinite number of monkeys outside that want to talk to you about a script of hamlet they've produced!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 1999
    Location
    Belgium
    Posts
    98

    Thumbs down

    Sorry, that doesn 't work.
    When I do that, I don 't have a scrollbar at all.
    Thanks for trying.

  4. #4
    Addicted Member Mrs Kensington's Avatar
    Join Date
    Sep 2001
    Location
    Dorset, UK
    Posts
    144
    OK this is your code with my modifications. It works fine when i compile and run it.

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Interface extends JFrame {
    
    
        public Interface() {
        	
            Container contentPane = getContentPane();
            
            contentPane.setLayout(null);
            
            JTable t1 = new JTable(100, 5);
    
            JScrollPane s1 = new JScrollPane(t1);
            
            contentPane.add(s1);
    
            // Object.setBounds(left, top, with, height);
            
            s1.setBounds(1, 100, 500, 500);
            
            for(int i = 0; i < 100; i++) {
            	
            	t1.setValueAt("Value" + i,i,0);
            	
            }
            
            
            
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    		
    		int w = d.width;
    		
    		int h = d.height;
    		
            this.setSize((int) (((double) w) * 0.9),(int) (((double) h) * 0.9));
            
            this.setLocation((int) (((double) w) * 0.05),(int) (((double) h) * 0.05));
            
            this.setBackground(Color.lightGray );
            
            this.setResizable(false);
    
        	this.setVisible(true);
    
    
           this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
            System.exit(0);
            }
            });
        }
    
        public static void main(String args[]) {
            Interface window = new Interface();
     
        }
    }
    Ford? Theres an infinite number of monkeys outside that want to talk to you about a script of hamlet they've produced!

  5. #5
    Member daveyboy's Avatar
    Join Date
    Feb 2002
    Location
    Aberystwyth, UK
    Posts
    33
    I concur. works fine, heres my code, which works, just for reference:

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Interface extends JFrame {
    
    
        public Interface() {
    
            Container contentPane = getContentPane();
    
            JTable t1 = new JTable(100, 5);
    
            JScrollPane s1 = new JScrollPane(t1);
    
            contentPane.add(s1);
    
            // Object.setBounds(left, top, with, height);
    
            t1.setBounds(1, 100, 500, 500);
    
            s1.setBounds(501, 100, 20, 500);
    
            for(int i = 0; i < 100; i++) {
    
            	t1.setValueAt("Value" + i,i,0);
    
            }
    
    
    
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    
    		int w = d.width;
    
    		int h = d.height;
    
            this.setSize((int) (((double) w) * 0.9),(int) (((double) h) * 0.9));
    
            this.setLocation((int) (((double) w) * 0.05),(int) (((double) h) * 0.05));
    
            this.setBackground(Color.lightGray );
    
            //this.setResizable(false);
    
        	this.setVisible(true);
    
    
           this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
            System.exit(0);
            }
            });
        }
    
        public static void main(String args[]) {
            Interface window = new Interface();
    
        }
    }


    Infinity isn't large it's just incomprehensible

Posting Permissions

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



Click Here to Expand Forum to Full Width