Example usage for javax.swing.table TableColumn TableColumn

List of usage examples for javax.swing.table TableColumn TableColumn

Introduction

In this page you can find the example usage for javax.swing.table TableColumn TableColumn.

Prototype

public TableColumn(int modelIndex, int width, TableCellRenderer cellRenderer, TableCellEditor cellEditor) 

Source Link

Document

Creates and initializes an instance of TableColumn with the specified model index, width, cell renderer, and cell editor; all TableColumn constructors delegate to this one.

Usage

From source file:StocksTable3.java

public StocksTable3() {
    super("Stocks Table");
    setSize(600, 300);/*from w  w  w  .jav  a2 s . com*/

    m_data = new StockTableData();

    m_title = new JLabel(m_data.getTitle(), new ImageIcon("money.gif"), SwingConstants.LEFT);
    m_title.setFont(new Font("TimesRoman", Font.BOLD, 24));
    m_title.setForeground(Color.black);
    getContentPane().add(m_title, BorderLayout.NORTH);

    m_table = new JTable();
    m_table.setAutoCreateColumnsFromModel(false);
    m_table.setModel(m_data);

    for (int k = 0; k < StockTableData.m_columns.length; k++) {
        DefaultTableCellRenderer renderer = new ColoredTableCellRenderer();
        renderer.setHorizontalAlignment(StockTableData.m_columns[k].m_alignment);
        TableColumn column = new TableColumn(k, StockTableData.m_columns[k].m_width, renderer, null);
        m_table.addColumn(column);
    }

    JTableHeader header = m_table.getTableHeader();
    header.setUpdateTableInRealTime(false);

    JScrollPane ps = new JScrollPane();
    ps.getViewport().add(m_table);
    getContentPane().add(ps, BorderLayout.CENTER);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);
    setVisible(true);
}

From source file:StocksTable4.java

public StocksTable4() {
    super("Stocks Table");
    setSize(600, 300);//from   w  w w  .ja v a  2 s . com

    m_data = new StockTableData();

    m_title = new JLabel(m_data.getTitle(), new ImageIcon("money.gif"), SwingConstants.LEFT);
    m_title.setFont(new Font("TimesRoman", Font.BOLD, 24));
    m_title.setForeground(Color.black);
    getContentPane().add(m_title, BorderLayout.NORTH);

    m_table = new JTable();
    m_table.setAutoCreateColumnsFromModel(false);
    m_table.setModel(m_data);

    for (int k = 0; k < StockTableData.m_columns.length; k++) {
        DefaultTableCellRenderer renderer = new ColoredTableCellRenderer();
        renderer.setHorizontalAlignment(StockTableData.m_columns[k].m_alignment);
        TableColumn column = new TableColumn(k, StockTableData.m_columns[k].m_width, renderer, null);
        m_table.addColumn(column);
    }

    JTableHeader header = m_table.getTableHeader();
    header.setUpdateTableInRealTime(true);
    header.addMouseListener(m_data.new ColumnListener(m_table));
    header.setReorderingAllowed(true);

    JScrollPane ps = new JScrollPane();
    ps.getViewport().add(m_table);
    getContentPane().add(ps, BorderLayout.CENTER);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);
    setVisible(true);
}

From source file:StocksTable5.java

public StocksTable5() {
    super("Stocks Table");
    setSize(600, 300);/*from  w  w  w  . ja  va2s  . c  o  m*/

    m_data = new StockTableData();

    m_title = new JLabel(m_data.getTitle(), new ImageIcon("money.gif"), SwingConstants.LEFT);
    m_title.setFont(new Font("TimesRoman", Font.BOLD, 24));
    m_title.setForeground(Color.black);
    getContentPane().add(m_title, BorderLayout.NORTH);

    m_table = new JTable();
    m_table.setAutoCreateColumnsFromModel(false);
    m_table.setModel(m_data);

    for (int k = 0; k < StockTableData.m_columns.length; k++) {
        DefaultTableCellRenderer renderer = new ColoredTableCellRenderer();
        renderer.setHorizontalAlignment(StockTableData.m_columns[k].m_alignment);
        TableColumn column = new TableColumn(k, StockTableData.m_columns[k].m_width, renderer, null);
        m_table.addColumn(column);
    }

    JTableHeader header = m_table.getTableHeader();
    header.setUpdateTableInRealTime(true);
    header.addMouseListener(m_data.new ColumnListener(m_table));
    header.setReorderingAllowed(true);

    JScrollPane ps = new JScrollPane();
    ps.getViewport().add(m_table);
    getContentPane().add(ps, BorderLayout.CENTER);

    JMenuBar menuBar = createMenuBar();
    setJMenuBar(menuBar);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);
    setVisible(true);
}

From source file:StocksTable.java

public StocksTable() {
    super("Stocks Table");
    setSize(600, 300);//from w  w  w.  ja  v a 2s.c o m

    m_data = new StockTableData();

    m_title = new JLabel(m_data.getTitle(), new ImageIcon("money.gif"), SwingConstants.LEFT);
    m_title.setFont(new Font("TimesRoman", Font.BOLD, 24));
    m_title.setForeground(Color.black);
    getContentPane().add(m_title, BorderLayout.NORTH);

    m_table = new JTable();
    m_table.setAutoCreateColumnsFromModel(false);
    m_table.setModel(m_data);

    for (int k = 0; k < StockTableData.m_columns.length; k++) {
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(StockTableData.m_columns[k].m_alignment);
        TableColumn column = new TableColumn(k, StockTableData.m_columns[k].m_width, renderer, null);
        m_table.addColumn(column);
    }

    JTableHeader header = m_table.getTableHeader();
    header.setUpdateTableInRealTime(false);

    JScrollPane ps = new JScrollPane();
    ps.getViewport().add(m_table);
    getContentPane().add(ps, BorderLayout.CENTER);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);
    setVisible(true);
}

From source file:StocksTable2.java

public StocksTable2() {
    super("Stocks Table");
    setSize(600, 300);//from  www  .  j  a  v  a2s.c o  m

    m_data = new StockTableData();

    m_title = new JLabel(m_data.getTitle(), new ImageIcon("money.gif"), SwingConstants.LEFT);
    m_title.setFont(new Font("TimesRoman", Font.BOLD, 24));
    m_title.setForeground(Color.black);
    getContentPane().add(m_title, BorderLayout.NORTH);

    m_table = new JTable();
    m_table.setAutoCreateColumnsFromModel(false);
    m_table.setModel(m_data);

    for (int k = 0; k < StockTableData.m_columns.length; k++) {
        DefaultTableCellRenderer renderer = new ColoredTableCellRenderer();
        renderer.setHorizontalAlignment(StockTableData.m_columns[k].m_alignment);
        TableColumn column = new TableColumn(k, StockTableData.m_columns[k].m_width, renderer, null);
        m_table.addColumn(column);
    }

    JTableHeader header = m_table.getTableHeader();
    header.setUpdateTableInRealTime(false);

    JScrollPane ps = new JScrollPane();
    ps.getViewport().add(m_table);
    getContentPane().add(ps, BorderLayout.CENTER);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);
    setVisible(true);
}

From source file:ExpenseReport.java

public ExpenseReport() {
    super("Expense Report");
    setSize(570, 200);/* ww w  . jav a 2s .  com*/

    m_data = new ExpenseReportData(this);

    m_table = new JTable();
    m_table.setAutoCreateColumnsFromModel(false);
    m_table.setModel(m_data);
    m_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    for (int k = 0; k < ExpenseReportData.m_columns.length; k++) {
        TableCellRenderer renderer;
        if (k == ExpenseReportData.COL_APPROVED)
            renderer = new CheckCellRenderer();
        else {
            DefaultTableCellRenderer textRenderer = new DefaultTableCellRenderer();
            textRenderer.setHorizontalAlignment(ExpenseReportData.m_columns[k].m_alignment);
            renderer = textRenderer;
        }

        TableCellEditor editor;

        if (k == ExpenseReportData.COL_CATEGORY)
            editor = new DefaultCellEditor(new JComboBox(ExpenseReportData.CATEGORIES));
        else if (k == ExpenseReportData.COL_APPROVED)
            editor = new DefaultCellEditor(new JCheckBox());
        else
            editor = new DefaultCellEditor(new JTextField());

        TableColumn column = new TableColumn(k, ExpenseReportData.m_columns[k].m_width, renderer, editor);
        m_table.addColumn(column);
    }

    JTableHeader header = m_table.getTableHeader();
    header.setUpdateTableInRealTime(false);

    JScrollPane ps = new JScrollPane();
    ps.setSize(550, 150);
    ps.getViewport().add(m_table);
    getContentPane().add(ps, BorderLayout.CENTER);

    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));

    ImageIcon penny = new ImageIcon("penny.gif");
    m_title = new JLabel("Total: $", penny, JButton.LEFT);
    m_title.setForeground(Color.black);
    m_title.setAlignmentY(0.5f);
    p.add(m_title);
    p.add(Box.createHorizontalGlue());

    JButton bt = new JButton("Insert before");
    bt.setMnemonic('b');
    bt.setAlignmentY(0.5f);
    ActionListener lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = m_table.getSelectedRow();
            m_data.insert(row);
            m_table.tableChanged(
                    new TableModelEvent(m_data, row, row, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
            m_table.repaint();
        }
    };
    bt.addActionListener(lst);
    p.add(bt);

    bt = new JButton("Insert after");
    bt.setMnemonic('a');
    bt.setAlignmentY(0.5f);
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = m_table.getSelectedRow();
            m_data.insert(row + 1);
            m_table.tableChanged(new TableModelEvent(m_data, row + 1, row + 1, TableModelEvent.ALL_COLUMNS,
                    TableModelEvent.INSERT));
            m_table.repaint();
        }
    };
    bt.addActionListener(lst);
    p.add(bt);

    bt = new JButton("Delete row");
    bt.setMnemonic('d');
    bt.setAlignmentY(0.5f);
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = m_table.getSelectedRow();
            if (m_data.delete(row)) {
                m_table.tableChanged(new TableModelEvent(m_data, row, row, TableModelEvent.ALL_COLUMNS,
                        TableModelEvent.INSERT));
                m_table.repaint();
                calcTotal();
            }
        }
    };
    bt.addActionListener(lst);
    p.add(bt);

    getContentPane().add(p, BorderLayout.SOUTH);

    calcTotal();

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);

    setVisible(true);
}

From source file:ColorTableColumnRenderer.java

public ColorTableColumnRenderer() {
    super("Stocks Table");
    setSize(300, 300);/*from ww w  . ja v a2s.c o m*/

    tableModel = new MyTableModel();

    table = new JTable();
    table.setModel(tableModel);

    DefaultTableCellRenderer renderer = new ColoredTableCellRenderer();
    TableColumn column = new TableColumn(0, 190, renderer, null);
    table.addColumn(column);

    JScrollPane ps = new JScrollPane();
    ps.getViewport().add(table);
    getContentPane().add(ps, BorderLayout.CENTER);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);
    setVisible(true);
}

From source file:net.sourceforge.happybank.main.BankMain.java

private void initComponents() {
    frame = new javax.swing.JFrame();
    menuBar = new javax.swing.JMenuBar();
    menuFile = new javax.swing.JMenu();
    menuItemExit = new javax.swing.JMenuItem();
    menuActions = new javax.swing.JMenu();
    menuItemView = new javax.swing.JMenuItem();
    separator2 = new javax.swing.JSeparator();
    menuHelp = new javax.swing.JMenu();
    separator4 = new javax.swing.JSeparator();
    menuItemAbout = new javax.swing.JMenuItem();
    accountEntries = new javax.swing.JTable();
    accountModel = new AccountTableModel();

    /*/*from   ww  w.ja  v a2 s.co  m*/
     * File menu
     */
    menuFile.setMnemonic('F');
    menuFile.setText("File");
    // - Exit option
    menuItemExit.setMnemonic('X');
    menuItemExit.setText("Exit");
    menuItemExit.setActionCommand("Exit");
    menuItemExit.addActionListener(new ActionHandler());
    menuFile.add(menuItemExit);
    menuBar.add(menuFile);

    // Actions menu
    menuActions.setMnemonic('A');
    menuActions.setText("Actions");
    // - View option
    menuItemView.setMnemonic('V');
    menuItemView
            .setAccelerator(KeyStroke.getKeyStroke('V', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    menuItemView.setText("View");
    menuItemView.setActionCommand("View");
    menuItemView.addActionListener(new ActionHandler());
    menuActions.add(menuItemView);
    menuActions.add(separator2);

    // Help menu
    menuHelp.setMnemonic('H');
    menuHelp.setText("Help");
    // - About option
    menuHelp.add(separator4);
    menuItemAbout.setMnemonic('A');
    menuItemAbout.setText("About");
    menuItemAbout.setActionCommand("About");
    menuItemAbout.addActionListener(new ActionHandler());
    menuHelp.add(menuItemAbout);
    menuBar.add(menuHelp);

    /*
     * configure the TabListCellRenderer
     */
    accountEntries.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    accountEntries.setAutoCreateColumnsFromModel(false);
    accountEntries.setModel(accountModel);
    accountEntries.getTableHeader().setReorderingAllowed(false);
    accountEntries.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    accountEntries.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent evt) {
            // capture single click
            if (evt.getClickCount() == 1 && SwingUtilities.isLeftMouseButton(evt)) {
                // ignore
            }
            // capture double click
            if (evt.getClickCount() == 2) {
                // edit the account
                int row = accountEntries.rowAtPoint(evt.getPoint());
                accountEntries.setRowSelectionInterval(row, row);
                onViewAccount();
            }
        } // mouseClicked
    }); // MouseAdapter

    // set the column widths and alignment
    for (int k = 0; k < AccountTableModel.COLUMNS.length; k++) {
        TableCellEditor zipper = new DefaultCellEditor(new JTextField());
        DefaultTableCellRenderer textRenderer = new DefaultTableCellRenderer();
        textRenderer.setHorizontalAlignment(AccountTableModel.COLUMNS[k].cAlignment);
        TableColumn column = new TableColumn(k, AccountTableModel.COLUMNS[k].cWidth, textRenderer, zipper);
        accountEntries.addColumn(column);
    }

    // set the table header
    JTableHeader header = accountEntries.getTableHeader();
    header.setUpdateTableInRealTime(false);

    /*
     * create the selection area
     */
    accountPanel = new JPanel();
    accountPanel.setLayout(new BorderLayout());
    scrollPane = new JScrollPane(accountEntries);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrollPane.setPreferredSize(new java.awt.Dimension(750, 300));
    accountPanel.add(scrollPane);
    frame.getContentPane().add(accountPanel, java.awt.BorderLayout.CENTER);

    /*
     * layout the frame
     */
    frame.setJMenuBar(menuBar);
    frame.setTitle(APP_NAME);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation(screenSize.width / 2 - 300, screenSize.height / 2 - 200);
    // add a listener for the close event
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            onExit();
        }
    });

    loadAccounts();
}

From source file:org.kuali.test.ui.base.BaseTable.java

/**
 *
 * @param col/*w  ww.j  a  v a2  s.  com*/
 * @return
 */
protected TableColumn getTableColumn(int col) {
    int colwidth = getColumnWidth(col);

    return new TableColumn(col, colwidth, getTableCellRenderer(getConfig(), col),
            getTableCellEditor(getConfig(), col));
}

From source file:org.wandora.application.gui.OccurrenceTableSingleType.java

/** Creates a new instance of OccurrenceTableSingleType */
public OccurrenceTableSingleType(Topic topic, Topic type, Options options, Wandora wandora)
        throws TopicMapException {
    this.wandora = wandora;
    this.topic = topic;
    TopicMap tm = wandora.getTopicMap();

    try {//from   ww  w  .  java  2  s .  c o  m
        Options opts = options;
        if (opts == null)
            opts = wandora.getOptions();
        if (opts != null) {
            tableType = opts.get(VIEW_OPTIONS_KEY);
            if (tableType == null || tableType.length() == 0)
                tableType = VIEW_SCHEMA;

            defaultRowHeight = opts.getInt(ROW_HEIGHT_OPTIONS_KEY);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    this.type = type;

    HashSet<Topic> langSet = new LinkedHashSet();
    if (VIEW_USED.equalsIgnoreCase(tableType) || VIEW_USED_AND_SCHEMA.equalsIgnoreCase(tableType)) {
        Hashtable<Topic, String> occs = null;
        Topic langTopic = null;
        occs = topic.getData(type);
        for (Enumeration<Topic> keys = occs.keys(); keys.hasMoreElements();) {
            langTopic = keys.nextElement();
            langSet.add(langTopic);
        }
    }
    if (VIEW_SCHEMA.equalsIgnoreCase(tableType) || VIEW_USED_AND_SCHEMA.equalsIgnoreCase(tableType)) {
        Collection<Topic> langTopics = tm.getTopicsOfType(LANGUAGE_SI);
        langSet.addAll(langTopics);
    }

    langs = langSet.toArray(new Topic[langSet.size()]);
    data = new String[langs.length];
    originalData = new String[langs.length];
    colors = new Color[langs.length];

    for (int j = 0; j < langs.length; j++) {
        if (langs[j] != null) {
            data[j] = topic.getData(type, langs[j]);
            if (data[j] == null)
                data[j] = "";
            originalData[j] = data[j];
            colors[j] = wandora.topicHilights.getOccurrenceColor(topic, type, langs[j]);
        }
    }

    dataModel = new DataTableModel();
    sorter = new TableRowSorter(dataModel);

    final TableCellRenderer oldRenderer = this.getTableHeader().getDefaultRenderer();

    this.getTableHeader().setPreferredSize(new Dimension(100, 23));
    this.getTableHeader().setDefaultRenderer(new TableCellRenderer() {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int column) {
            Component c = oldRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
                    column);
            return c;
        }
    });

    this.setAutoCreateColumnsFromModel(false);

    TableColumn column = new TableColumn(0, 40, new TopicCellRenderer(), new TopicCellEditor());
    this.addColumn(column);
    column = new TableColumn(1, 400, new DataCellRenderer(), new DataCellEditor());
    this.addColumn(column);
    this.setTableHeader(this.getTableHeader());

    this.setModel(dataModel);
    this.setRowSorter(sorter);
    sorter.setSortsOnUpdates(true);

    updateRowHeights();

    popupStruct = WandoraMenuManager.getOccurrenceTableMenu(this, options);
    JPopupMenu popup = UIBox.makePopupMenu(popupStruct, wandora);
    this.setComponentPopupMenu(popup);
    this.addMouseListener(this);

    this.setColumnSelectionAllowed(false);
    this.setRowSelectionAllowed(false);
    this.setDragEnabled(true);
    this.setTransferHandler(new OccurrencesTableTransferHandler());
    this.setDropMode(DropMode.ON);
    this.createDefaultTableSelectionModel();
}