• Post Reply Bookmark Topic Watch Topic
  • New Topic

Freezing first column(JTree) in JTreeTable - Urgent Please

 
Rakesh Gudur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
My requirement is to freeze the first column of a JTreeTable. With a JTable the job is a bit easier and I've seen some examples.
But how do I freeze the first column in a JTreeTable. My requirement is the first column is a JTree and the rest are the table rows and columns. The scrollbar should appear only for the columns greater than 0. There should be a vertical scrollbar which scrolls the JTree and the JTable.
I tried using two different JTreeTables and then add the column of the second treetable to the first. But when I expand and collapse the parent nodes the table rows are not expanding and collapsing.
Pls give me a solution for this. If you can give me some code snippets that would be great. Thanks in advance.
My code is like this, but it doesn't work as I expect:
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
public class Tester extends JPanel{
final static boolean standaloneMode=true;

JMenuBar mb = new JMenuBar();
JMenu file = new JMenu("File");
JMenu reports = new JMenu("Reports");
JMenuItem filePrint = new JMenuItem("Print");
JSeparator separator = new JSeparator();
JMenuItem fileQuit = new JMenuItem("Quit");
JMenuItem itemList = new JMenuItem("List All Items");
JMenuItem browseItems = new JMenuItem("Browse Items");
JFrame frame=new JFrame();
JViewport innerPort, headers;
JScrollBar scrollBar;
JScrollPane scrollPane;
JTreeTable fixedTable;
JTreeTable scrollTable;
TableModel tm;
TableColumnModel fixedColumnModel, scrollColumnModel;
int rowHeight=64;
JTree tree;
DefaultMutableTreeNode[] nodes;
JTextField findField;
JPanel homePanel=null;
JPanel browsePanel=null;
JPanel scrollPanel=null;

public Tester (){
homePanel = new JPanel();
homePanel.setLayout(new CardLayout());
scrollPanel = new JPanel();
scrollPanel.setLayout(new BorderLayout());
this.add(scrollPanel);
scrollTable=new JTreeTable();
// adjustColumnWidth(scrollTable.getColumn("End Date"), 150);
scrollTable.setRowHeight(rowHeight);
fixedTable = new JTreeTable();
fixedTable.setTreeTableModelAdapter(scrollTable.getTreeTableModel(), fixedTable.getTree());
freezeColumn();
fixedTable.setRowHeight(scrollTable.getRowHeight());
headers = new JViewport();
headers.add(scrollTable.getTableHeader());
JPanel topPanel = new JPanel();
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
adjustColumnWidth(fixedColumnModel.getColumn(0), 100);
JTableHeader fixedHeader=fixedTable.getTableHeader();
fixedHeader.setAlignmentY(Component.TOP_ALIGNMENT);
topPanel.add(fixedHeader);
topPanel.add(Box.createRigidArea(new Dimension(2, 0)));
topPanel.setPreferredSize(new Dimension(400, 40));
JPanel headerPanel = new JPanel();
headerPanel.setAlignmentY(Component.TOP_ALIGNMENT);
headerPanel.setLayout(new BorderLayout());
JScrollPane scrollpane = new JScrollPane();
scrollBar = scrollpane.getHorizontalScrollBar();
headerPanel.add(headers, "North");
headerPanel.add(scrollBar, "South");
topPanel.add(headerPanel);
innerPort = new JViewport();
innerPort.setView(scrollTable);
scrollpane.setViewport(innerPort);
scrollBar.getModel().addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e)
{
Point q = headers.getViewPosition();
Point p = innerPort.getViewPosition();
int val = scrollBar.getModel().getValue();
p.x = val;
q.x = val;
headers.setViewPosition(p);
headers.repaint(headers.getViewRect());
innerPort.setViewPosition(p);
innerPort.repaint(innerPort.getViewRect());
}
});

JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));
fixedTable.setAlignmentY(Component.TOP_ALIGNMENT);
bottomPanel.add(fixedTable);
bottomPanel.add(Box.createRigidArea(new Dimension(2, 0)));
innerPort.setAlignmentY(Component.TOP_ALIGNMENT);
bottomPanel.add(innerPort);
bottomPanel.add(Box.createRigidArea(new Dimension(2, 0)));
scrollPane= new JScrollPane(bottomPanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JViewport outerPort = new JViewport();
outerPort.add(bottomPanel);
scrollPane.setColumnHeaderView(topPanel);
scrollPane.setViewport(outerPort);
scrollTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scrollPanel.add(scrollPane);
scrollTable.validate();

}
public void freezeColumn(){
scrollColumnModel = scrollTable.getColumnModel();
fixedColumnModel = new DefaultTableColumnModel();
TableColumn col = scrollColumnModel.getColumn(0);
scrollColumnModel.removeColumn(col);
fixedColumnModel.addColumn(col);
fixedTable.setColumnModel(fixedColumnModel);
}
void adjustColumnWidth(TableColumn c, int size) {
c.setPreferredWidth(size);
c.setMaxWidth(size);
c.setMinWidth(size);
}
public static void main(String [] args){
try {
final String windows =
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
UIManager.setLookAndFeel(windows);
JFrame _frame = new JFrame();
_frame.getContentPane().setLayout(new BorderLayout());
_frame.getContentPane().add(new Tester(), BorderLayout.CENTER);
//((AbstractTableModel)treeTable.getModel()).fireTableStructureChanged();
_frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());
_frame.pack();
_frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
_frame.show();
} catch (Exception x) {
x.printStackTrace();
}
}
}

thanks in adv. I appreciate an urgent response.......
Rakesh.
[ March 23, 2004: Message edited by: Rakesh Gudur ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!