Board index » jbuilder » JScrollPane for JTable in a GridBagLayout

JScrollPane for JTable in a GridBagLayout


2005-04-01 10:26:20 PM
jbuilder20
Hello,
i am having some trouble in implementing a JScrollPane for a JTable (im showing database records there via SQL).
The JScrollPane is placed on a JPanel which in turn resides on my main frame. My main Frame has a GridBagLayout.The JPanel has also a GridBagLayout.
I am using table.setSize(...) to make the JTable bigger than the screen. This works ok. Now im using
table.setPreferredScrollableViewportSize(t);
where t is a region inside the screen (smaller than the JTable).
Now: how big do i have to make the JScrollPane?
1) if i make the JScrollPane as big as the JTable, then there appears a horizontal ScrollBar (scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
but it has no knob for scrolling (i assume because they have the same size, the system thinks its not necessary)
2) if i make the JScrollPane as big as t (inside the screen), the JTable becomes also smaller (which i do not want - i want the JTable to be bigger than the screen).
Does somebody have a hint how i can get a horizontal scrollbar which works for a JTable which is bigger than the screen?
thanks
Oliver
 
 

Re:JScrollPane for JTable in a GridBagLayout

Hello,
now i have tried the following approach:
table.setAutoResizeMode (JTable.AUTO_RESIZE_OFF);
scroller.setHorizontalScrollBar(new JScrollBar());
table.setAutoResizeMode (JTable.AUTO_RESIZE_OFF);
this is almost ok (at least i can scroll the table now)
- but it looks somewhat odd (i can scroll the table horizontally, but i have to pull the scrollbar up and down to do this).
regards
Oliver
 

Re:JScrollPane for JTable in a GridBagLayout

On 4/1/2005 at 9:26:20 AM, Oliver wrote:
Quote
1) if i make the JScrollPane as big as the JTable, then there appears a
horizontal ScrollBar
(scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_A
LWAYS); but it has no knob for scrolling (i assume because they have the
same size, the system thinks its not necessary)

2) if i make the JScrollPane as big as t (inside the screen), the JTable
becomes also smaller (which i do not want - i want the JTable to be
bigger than the screen).
Can you post a runnable example of the problem?
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

{smallsort}

Re:JScrollPane for JTable in a GridBagLayout

Hello
its a bit difficult for me to extract the relevant parts from
my code, but i am using
protected ColorTable table = new ColorTable();
protected JScrollPane scroller = new JScrollPane(table);
private void createGUI() {
GridBagLayout layout = new GridBagLayout();
controlPanel.setLayout(layout);
controlPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEtchedBorder(),
BorderFactory.createEmptyBorder(10, 10, 10, 10)));
// controlPanel.setForeground();
setLayout(layout);
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.anchor = GridBagConstraints.NORTH;
constraints.weightx = 1;
constraints.weighty = 0;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
add(toolBar, constraints);
toolBar.addSeparator(new Dimension(10, -1));
toolBar.setFloatable(false);
toolBar.setBackground(new Color(133,100,183));
constraints.gridy = 1;
add(controlPanel, constraints);
constraints.fill = GridBagConstraints.BOTH;
constraints.anchor = GridBagConstraints.CENTER;
constraints.weightx = 1;
constraints.weighty = 1;
constraints.gridy = 2;
scroller.setBorder(BorderFactory.createLoweredBevelBorder());
add(scroller, constraints);
setPreferredSize(new Dimension(800, 800));
}
private void SetScrollbar() {
table.setAutoResizeMode (JTable.AUTO_RESIZE_OFF);
scroller.setHorizontalScrollBar(new JScrollBar());
table.setAutoResizeMode (JTable.AUTO_RESIZE_OFF);
}
where ColorTable is a descendant of JTable (with a special Renderer), and scroller is a JScrollPane.
Im not so familiar with the GridBagLayout, maybe this plays a role here too?
kind regards
Oliver