Vertical ScrollBar can view from JTableHeader in JTable

HI,If we add a JTable to JScrollPane, It gives the Header.The Vertical ScrollBar View can we start from JTableHeader(The UpArrow Button in VerticalScrollBar should start from JTableHeader).
[210 byte] By [JAVAMINDSa] at [2007-11-26 13:54:18]
# 1
a) Create a panel with a BorderLayout.b) add the table header to the Northc) add the table to the centerd) add the panel to a scroll pane.
camickra at 2007-7-8 1:32:48 > top of Java-index,Desktop,Core GUI APIs...
# 2

Although I did not start the thread I am interested in the solution...

And I found out that you have to do

> a) Create a panel with a BorderLayout.

> b) add the table header to the North

c) add table to scrollpane

d) add scrollpane to center of panel

import javax.swing.*;

import java.awt.*;

public class TableTest extends JFrame {

public TableTest() {

JPanel p = new JPanel();

p.setLayout(new BorderLayout());

Object[][] rowData = {

{"1", "2", "3", "4"},

{"1", "2", "3", "4"},

{"1", "2", "3", "4"},

{"1", "2", "3", "4"},

{"1", "2", "3", "4"},

{"1", "2", "3", "4"},

{"1", "2", "3", "4"},

{"1", "2", "3", "4"},

{"1", "2", "3", "4"},

{"1", "2", "3", "4"},

{"1", "2", "3", "4"},

{"1", "2", "3", "4"},

{"1", "2", "3", "4"},

{"1", "2", "3", "4"},

};

Object[] columnNames = {

"col 1", "col 2", "col 3", "col 4"

};

JTable table = new JTable(rowData, columnNames);

p.add(table.getTableHeader(), BorderLayout.NORTH);

JScrollPane sp = new JScrollPane(table);

p.add(sp, BorderLayout.CENTER);

this.getContentPane().add(p, BorderLayout.CENTER);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setSize(200,200);

this.setVisible(true);

}

/**

* @param args

*/

public static void main(String[] args) {

new TableTest();

}

}

I would like to know if there is an easy way to do the same with horizontal scrolling? I want the first column to be visible while the other columns can be scrolled. I guess, no, because I cannot find a method in JTable like getTableHeader for columns.

Thanks!

Annette

p.s.: just added your WrapLayout to my ToolBars today and I really like that the buttons do not get lost anymore when resizing the frame! Thank you for sharing it here!

Annettea at 2007-7-8 1:32:48 > top of Java-index,Desktop,Core GUI APIs...
# 3

> I would like to know if there is an easy way to do the same with horizontal scrolling?

Well the JTableHeader is a separate component, so you need to create a separate component for the row header as well. Then you add this component to the scroll pane as its row header. Search the forum for my "FixedColumnScrollPane" and "LineNumberTable" examples which show a couple of different ways to use a row header.

camickra at 2007-7-8 1:32:48 > top of Java-index,Desktop,Core GUI APIs...
# 4

XO, camickr!

Thank you so much! I was first wondering when I was looking at FixedColumnScrollPane how the hell you could do this with a few lines of code. Then I saw

setRowHeaderView( fixed );

of JScrollPane. That's the trick, isn't it? Great! Thank you! I would have produced a lot of nonsense-code while desperately trying to do this........

Annette

Annettea at 2007-7-8 1:32:48 > top of Java-index,Desktop,Core GUI APIs...
# 5
Thanks for tips. But The Header should not be scroll(It should be fix).
JAVAMINDSa at 2007-7-8 1:32:48 > top of Java-index,Desktop,Core GUI APIs...
# 6

Thanks for your solution.

If you run the code will you get the window. in the table above the vertical scrollbar will have a upper_right_corner.

My question is can we remove the upper_right_corner and start the vertical scrollbar button from the top.

Scrollbar should cover the header and header should not move.

Please give me the solution for this

JAVAMINDSa at 2007-7-8 1:32:48 > top of Java-index,Desktop,Core GUI APIs...
# 7

> Scrollbar should cover the header and header should not move.

Doesn't make any sense. The scrollbar covers the vertical area of the panel that is scrollable. It is misleading if the scrollbar covers the entire vertical area, but the top part doesn't scroll (take a look at any other application). But if you really want this then try the following:

a) create a panel with a BorderLayout

b) create a scrollPane and add the table to the scroll pane

c) add the header of the table to the BorderLayout.NORTH

d) add the scroll pane to the BorderLayout.CENTER

e) get the vertical scroll bar from the scroll pane and add it to the BorderLayout.EAST

camickra at 2007-7-8 1:32:48 > top of Java-index,Desktop,Core GUI APIs...
# 8
HI,The Solutions not worked.Can we achive this by JViewPort or can we change the layout of vertical Scrollbar when adding to scrollpane?Thanks & Regards,Harikrishna
JAVAMINDSa at 2007-7-8 1:32:48 > top of Java-index,Desktop,Core GUI APIs...
# 9
HI,Thanks for your solution.It is working.
JAVAMINDSa at 2007-7-8 1:32:48 > top of Java-index,Desktop,Core GUI APIs...