PDA

Click to See Complete Forum and Search --> : who make the JScrollPane work ???


R@emdonck
Apr 17th, 2002, 10:57 AM
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




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();

}
}

Mrs Kensington
Apr 17th, 2002, 11:55 AM
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

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.

s1.setBounds(1, 100, 500, 500);


That should then work fine.

R@emdonck
Apr 17th, 2002, 12:30 PM
Sorry, that doesn 't work.
When I do that, I don 't have a scrollbar at all.
Thanks for trying.

Mrs Kensington
Apr 17th, 2002, 04:55 PM
OK this is your code with my modifications. It works fine when i compile and run it.


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();

}
}

daveyboy
Apr 18th, 2002, 05:14 AM
I concur. works fine, heres my code, which works, just for reference:


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();

}
}