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!
> 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.
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
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
> 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