Fixed Table Row Example : Grid Table « Swing Components « Java






Fixed Table Row Example

Fixed Table Row Example
 
// Example from http://www.crionics.com/products/opensource/faq/swing_ex/SwingExamples.html

/* (swing1.1.1beta1) */

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.table.AbstractTableModel;

/**
 * @version 1.0 03/05/99
 */
public class FixedRowExample extends JFrame {
  Object[][] data;

  Object[] column;

  JTable fixedTable, table;

  private int FIXED_NUM = 2;

  public FixedRowExample() {
    super("Fixed Row Example");

    data = new Object[][] { { "a", "", "", "", "", "" },
        { "", "b", "", "", "", "" }, { "", "", "c", "", "", "" },
        { "", "", "", "d", "", "" }, { "", "", "", "", "e", "" },
        { "", "", "", "", "", "f" },
        { "fixed1", "", "", "", "", "", "", "" },
        { "fixed2", "", "", "", "", "", "", "" } };
    column = new Object[] { "A", "B", "C", "D", "E", "F" };

    AbstractTableModel model = new AbstractTableModel() {
      public int getColumnCount() {
        return column.length;
      }

      public int getRowCount() {
        return data.length - FIXED_NUM;
      }

      public String getColumnName(int col) {
        return (String) column[col];
      }

      public Object getValueAt(int row, int col) {
        return data[row][col];
      }

      public void setValueAt(Object obj, int row, int col) {
        data[row][col] = obj;
      }

      public boolean CellEditable(int row, int col) {
        return true;
      }
    };

    AbstractTableModel fixedModel = new AbstractTableModel() {
      public int getColumnCount() {
        return column.length;
      }

      public int getRowCount() {
        return FIXED_NUM;
      }

      public Object getValueAt(int row, int col) {
        return data[row + (data.length - FIXED_NUM)][col];
      }
    };

    table = new JTable(model);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    fixedTable = new JTable(fixedModel);
    fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    fixedTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    JScrollPane scroll = new JScrollPane(table);
    scroll
        .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

    JScrollPane fixedScroll = new JScrollPane(fixedTable) {
      public void setColumnHeaderView(Component view) {
      } // work around
    }; //
    // fixedScroll.setColumnHeader(null);

    fixedScroll
        .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    JScrollBar bar = fixedScroll.getVerticalScrollBar();
    JScrollBar dummyBar = new JScrollBar() {
      public void paint(Graphics g) {
      }
    };
    dummyBar.setPreferredSize(bar.getPreferredSize());
    fixedScroll.setVerticalScrollBar(dummyBar);

    final JScrollBar bar1 = scroll.getHorizontalScrollBar();
    JScrollBar bar2 = fixedScroll.getHorizontalScrollBar();
    bar2.addAdjustmentListener(new AdjustmentListener() {
      public void adjustmentValueChanged(AdjustmentEvent e) {
        bar1.setValue(e.getValue());
      }
    });

    scroll.setPreferredSize(new Dimension(400, 100));
    fixedScroll.setPreferredSize(new Dimension(400, 52)); // Hmm...
    getContentPane().add(scroll, BorderLayout.CENTER);
    getContentPane().add(fixedScroll, BorderLayout.SOUTH);
  }

  public static void main(String[] args) {
    FixedRowExample frame = new FixedRowExample();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    frame.pack();
    frame.setVisible(true);
  }
}



           
         
  








Related examples in the same category

1.HyperLink in TableHyperLink in Table
2.Column popup menuColumn popup menu
3.Tree TableTree Table
4.Swing Table in ComboBoxSwing Table in ComboBox
5.Tabbable Currency TableTabbable Currency Table
6.Icon Currency TableIcon Currency Table
7.MultiLine Header TableMultiLine Header Table
8.ToolTip TableToolTip Table
9.Striped Currency TableStriped Currency Table
10.CurrencyTableCurrencyTable
11.Calculated Column TableCalculated Column Table
12.Table Utilities
13.Fraction Currency TableFraction Currency Table
14.Highlight Currency TableHighlight Currency Table
15.Highlight Currency Table 2Highlight Currency Table 2
16.MultiLine TableMultiLine Table
17.Updatable Highlight Currency TableUpdatable Highlight Currency Table
18.Editable Highlight Currency TableEditable Highlight Currency Table
19.ComboBox TableComboBox Table
20.Groupable(Group) Header ExampleGroupable(Group) Header Example
21.MultiWidth Header ExampleMultiWidth Header Example
22.MultiLine Header ExampleMultiLine Header Example
23.Table Row Header ExampleTable Row Header Example
24.Fixed Table Column ExampleFixed Table Column Example
25.Button Table ExampleButton Table Example
26.Radio Button Table ExampleRadio Button Table Example
27.RadioButton Table Example 2RadioButton Table Example 2
28.MultiLine Cell ExampleMultiLine Cell Example
29.Each Row with different Editor ExampleEach Row with different Editor Example
30.Multiple Component Table: Checkbox and ComboboxMultiple Component Table: Checkbox and Combobox
31.multiple Component Table 2: checkboxmultiple Component Table 2: checkbox
32.Union Data Table ExampleUnion Data Table Example
33.Total(Calculate) Row ExampleTotal(Calculate) Row Example
34.Colored Cell Table Example
35.multiple Font Cell Table Example
36.Multi Span Cell Table Example
37.Mixed Table Example
38.Pushable Table Header ExamplePushable Table Header Example
39.Sortable Table ExampleSortable Table Example
40.ToolTip Header Table ExampleToolTip Header Table Example
41.Indicator Table ExampleIndicator Table Example
42.multiple Row Header Example
43.Column Border Table ExampleColumn Border Table Example
44.Cell Border Table ExampleCell Border Table Example
45.Hide Column Table ExampleHide Column Table Example
46.Animated Icon Table ExampleAnimated Icon Table Example
47.Animated Icon Header Example
48.Editable Header Table ExampleEditable Header Table Example
49.Editable Header Table Example 2Editable Header Table Example 2
50.JSortTable