Example usage for javax.swing JTable JTable

List of usage examples for javax.swing JTable JTable

Introduction

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

Prototype

public JTable(TableModel dm) 

Source Link

Document

Constructs a JTable that is initialized with dm as the data model, a default column model, and a default selection model.

Usage

From source file:DefaultsDisplay.java

protected JTable createDefaultsTable() {
    JTable table = new JTable(new UIDefaultsTableModel());
    table.setRowHeight(rowHeight);/* w  ww.jav  a2s. c  om*/
    table.setShowHorizontalLines(false);
    table.setShowVerticalLines(false);
    table.setIntercellSpacing(new Dimension(0, 0));
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    initFilters(table);

    DefaultTableColumnModel columnModel = new DefaultTableColumnModel();

    Color rowColors[] = new Color[2];
    rowColors[0] = UIManager.getColor("Table.background");
    rowColors[1] = new Color((int) (rowColors[0].getRed() * .90), (int) (rowColors[0].getGreen() * .95),
            (int) (rowColors[0].getBlue() * .95));

    int width = 0;

    TableColumn column = new TableColumn();
    column.setCellRenderer(new KeyRenderer(rowColors));
    column.setModelIndex(UIDefaultsTableModel.KEY_COLUMN);
    column.setHeaderValue("Key");
    column.setPreferredWidth(250);
    columnModel.addColumn(column);
    width += column.getPreferredWidth();

    column = new TableColumn();
    column.setCellRenderer(new RowRenderer(rowColors));
    column.setModelIndex(UIDefaultsTableModel.TYPE_COLUMN);
    column.setHeaderValue("Type");
    column.setPreferredWidth(250);
    columnModel.addColumn(column);
    width += column.getPreferredWidth();

    column = new TableColumn();
    column.setCellRenderer(new ValueRenderer(rowColors));
    column.setModelIndex(UIDefaultsTableModel.VALUE_COLUMN);
    column.setHeaderValue("Value");
    column.setPreferredWidth(300);
    columnModel.addColumn(column);
    width += column.getPreferredWidth();

    table.setColumnModel(columnModel);

    table.setPreferredScrollableViewportSize(new Dimension(width, 12 * rowHeight));

    return table;

}

From source file:edu.ku.brc.specify.utilapps.sp5utils.Sp5Forms.java

/**
 * /*  www  . ja  va 2s  .c o m*/
 */
protected void createUI() {
    CellConstraints cc = new CellConstraints();
    PanelBuilder pb = new PanelBuilder(new FormLayout("f:p:g,p,p:g", "p,2px,f:p:g,4px,p,2px,p,10px,p"));

    missingFieldsList = new JList(new DefaultListModel());

    formsTable = new JTable(new FormCellModel(forms));
    formsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (formsTable.getSelectedRow() > -1) {
                formSelected();
            }
        }
    });

    fieldsTable = new JTable(fieldsTableModel = new FieldCellModel());

    TableColumn col = fieldsTable.getColumnModel().getColumn(1);
    col.setCellEditor(sp6FieldEditor = new Sp6FieldComboBoxEditor());

    // If the cell should appear like a combobox in its
    // non-editing state, also set the combobox renderer
    //col.setCellRenderer(sp6FieldRenderer = new Sp6FieldComboBoxRenderer());

    calcColumnWidths(formsTable);

    PanelBuilder pbBtn = new PanelBuilder(new FormLayout("f:p:g,p,f:p:g,p,f:p:g,p,f:p:g,p,f:p:g", "p"));
    reportBtn = createButton("Create Form Images");
    showBtn = createButton("Show");
    schemaBtn = createButton("Schema");
    dataCheckerBtn = createButton("Check For Nulls");
    pbBtn.add(reportBtn, cc.xy(2, 1));
    pbBtn.add(schemaBtn, cc.xy(4, 1));
    pbBtn.add(showBtn, cc.xy(6, 1));
    pbBtn.add(dataCheckerBtn, cc.xy(8, 1));

    pb.add(createLabel("Forms", SwingConstants.CENTER), cc.xy(1, 1));
    pb.add(createLabel("Missing Fields", SwingConstants.CENTER), cc.xy(3, 1));

    pb.add(createScrollPane(formsTable), cc.xy(1, 3));
    pb.add(createScrollPane(missingFieldsList), cc.xy(3, 3));

    pb.add(createLabel("Form Fields", SwingConstants.CENTER), cc.xyw(1, 5, 3));
    pb.add(createScrollPane(fieldsTable), cc.xyw(1, 7, 3));

    pb.add(pbBtn.getPanel(), cc.xyw(1, 9, 3));
    pb.setDefaultDialogBorder();

    setContentPane(new JScrollPane(pb.getPanel()));

    showBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            showForm();
        }
    });
    reportBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            doCreateFormImages();
        }
    });
    schemaBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            doSchema();
        }
    });

    dataCheckerBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            TableDataChecker tdc = new TableDataChecker(DBConnection.getInstance().createConnection());
            tdc.doCheckDB();
        }
    });

}

From source file:edu.ku.brc.af.ui.weblink.WebLinkEditorDlg.java

@Override
public void createUI() {
    super.createUI();

    nameTF = UIHelper.createTextField();
    baseUrlTF = UIHelper.createTextField(25);
    descTA = UIHelper.createTextArea();//from   w ww . j  a  va 2s. c  o m

    descTA.setLineWrap(true);
    descTA.setWrapStyleWord(true);

    txtFGColor = nameTF.getForeground();
    txtBGColor = nameTF.getBackground();

    if (isEdit) {
        ViewFactory.changeTextFieldUIForDisplay(nameTF, true);
    }

    descTA.setRows(5);

    model = new WebLinkArgsTableModel();
    table = new JTable(model);
    table.setDefaultRenderer(String.class, new BiColorTableCellRenderer(true));

    CellConstraints cc = new CellConstraints();
    PanelBuilder rightPB = new PanelBuilder(new FormLayout("p,2px,f:p:g", //$NON-NLS-1$
            "p,2px,p, 4px,p,2px,200px" + (tableInfo != null ? ",2px,200px" : ""))); //$NON-NLS-1$

    rightPB.add(createI18NFormLabel("WebLinkArgDlg.NAME"), cc.xy(1, 1)); //$NON-NLS-1$ //$NON-NLS-2$
    rightPB.add(nameTF, cc.xy(3, 1));

    rightPB.add(createI18NFormLabel("WebLinkArgDlg.URL"), cc.xy(1, 3)); //$NON-NLS-1$ //$NON-NLS-2$
    rightPB.add(baseUrlTF, cc.xy(3, 3));

    rightPB.add(createI18NFormLabel("WebLinkArgDlg.DESC"), cc.xy(1, 5)); //$NON-NLS-1$ //$NON-NLS-2$
    rightPB.add(UIHelper.createScrollPane(descTA), cc.xy(3, 5));

    rightPB.add(createI18NFormLabel("WebLinkArgDlg.FIELDS"), cc.xy(1, 7)); //$NON-NLS-1$ //$NON-NLS-2$
    rightPB.add(UIHelper.createScrollPane(table), cc.xy(3, 7));

    if (tableInfo != null) {
        rightPB.add(UIHelper.createScrollPane(availableFields), cc.xy(3, 9));
    }

    rightPB.setDefaultDialogBorder();

    TableColumn promptCol = table.getColumnModel().getColumn(2);
    promptCol.setCellEditor(new DefaultCellEditor(new JCheckBox()));
    //promptCol.setCellRenderer(cellRenderer)
    UIHelper.makeTableHeadersCentered(table, false);

    contentPanel = rightPB.getPanel();
    mainPanel.add(contentPanel, BorderLayout.CENTER);
    pack();

    setDataIntoUI();

    DocumentListener docLis = new DocumentListener() {
        protected void changed() {
            hasChanged = true;
            parseForFields();
            enableUI();
        }

        public void changedUpdate(DocumentEvent e) {
            changed();
        }

        public void insertUpdate(DocumentEvent e) {
            changed();
        }

        public void removeUpdate(DocumentEvent e) {
            changed();
        }
    };

    nameTF.getDocument().addDocumentListener(docLis);
    baseUrlTF.getDocument().addDocumentListener(docLis);

    adjustAvailableJList();

    enableUI();

    pack();
}

From source file:cloud.gui.CloudGUI.java

private void createSnapshotTable() {
    snapshotModel = new DefaultTableModel(new String[][] {}, snapshotTableColumns);
    snapshotTable = new JTable(snapshotModel) {
        private static final long serialVersionUID = 6454534842446167244L;

        public boolean isCellEditable(int rowIndex, int colIndex) {
            return false;
        }/*  w w w . ja  v a 2s.  c o  m*/
    };

    snapshotTable.addMouseListener(snapshotPopupListener);
    snapshotTable.getColumnModel().getColumn(0).setPreferredWidth(20);
    JScrollPane pane = new JScrollPane(snapshotTable);
    snapshotPanel.add(pane);

}

From source file:com.github.lindenb.jvarkit.tools.bamviewgui.BamFileRef.java

private static JTable createTable(TableModel m) {
    JTable t = new JTable(m) {
        @Override/*from   w ww .  java2s  . c o  m*/
        public String getToolTipText(MouseEvent event) {
            JTable t = (JTable) event.getSource();
            int x = t.rowAtPoint(event.getPoint());
            if (x == -1)
                return null;
            int y = t.columnAtPoint(event.getPoint());
            if (y == -1)
                return null;
            Object o = t.getValueAt(x, y);
            if (o == null)
                return null;
            return String.valueOf(o);
        }

    };
    t.setToolTipText("");
    t.setShowVerticalLines(false);
    DefaultTableCellRenderer render = new DefaultTableCellRenderer() {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int column) {
            Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if (!isSelected && !hasFocus)
                this.setBackground(row % 2 == 0 ? color1 : color2);
            if (value != null && value instanceof Boolean) {
                if (Boolean.TRUE.equals(value))
                    this.setText("\u2612");
                else if (Boolean.FALSE.equals(value))
                    this.setText("");
            }
            return c;
        }
    };
    render.setOpaque(true);
    for (int i = 0; i < t.getColumnModel().getColumnCount(); ++i) {
        t.getColumnModel().getColumn(i).setCellRenderer(render);
    }
    return t;
}

From source file:SortedTableModel.java

public SimpleTableTestMultilineHeader() {
    Container pane = getContentPane();
    pane.setLayout(new BorderLayout());
    TableValues tv = new TableValues();
    SortedTableModel stm = new SortedTableModel(tv);
    stm.sortRows(TableValues.ACCOUNT_BALANCE, true);
    table = new JTable(stm);
    table.setRowSelectionAllowed(false);
    table.setColumnSelectionAllowed(true);
    TableColumnModel tcm = table.getColumnModel();
    TableColumn tc = tcm.getColumn(TableValues.GENDER);
    MultiLineHeaderRenderer mlhr = new MultiLineHeaderRenderer();
    tc = tcm.getColumn(TableValues.ACCOUNT_BALANCE);
    tc.setHeaderRenderer(mlhr);/*w ww .  j a  v  a  2  s .  c om*/
    renderer = new SortedColumnHeaderRenderer(stm, mlhr);
    int count = tcm.getColumnCount();
    for (int i = 0; i < count; i++) {
        tc = tcm.getColumn(i);
        tc.setHeaderRenderer(renderer);
    }
    JTableHeaderToolTips jthtt = new JTableHeaderToolTips(table.getColumnModel());
    jthtt.setToolTips(new String[] { "Customer's First Name", "Customer's Last Name",
            "Customer's Date of Birth", "Customer's Account Balance", "Customer's Gender" });
    table.setTableHeader(jthtt);
    JScrollPane jsp = new JScrollPane(table);
    pane.add(jsp, BorderLayout.CENTER);
    addHeaderListener();
}

From source file:com.haskins.cloudtrailviewer.sidebar.AbstractChart.java

private void addTable() {

    defaultTableModel.addColumn("");
    defaultTableModel.addColumn("Property");
    defaultTableModel.addColumn("Value");

    final LegendColourRenderer cellRenderer = new LegendColourRenderer();
    final JTable table = new JTable(defaultTableModel) {

        private static final long serialVersionUID = -6272711583089149891L;

        @Override//www.  j av  a2 s  .co m
        public TableCellRenderer getCellRenderer(int row, int column) {
            if (column == 0) {
                return cellRenderer;
            }
            return super.getCellRenderer(row, column);
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    };

    table.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent me) {

            JTable table = (JTable) me.getSource();
            String value = (String) defaultTableModel.getValueAt(table.getSelectedRow(), 1);

            if (me.getClickCount() == 2) {

                if (value.startsWith("i-")) {

                    Event event = null;
                    AllFilter filter = new AllFilter();
                    filter.setNeedle(value);
                    for (Event searchEvent : eventDb.getEvents()) {

                        if (filter.passesFilter(searchEvent)) {

                            event = searchEvent;
                            break;
                        }
                    }

                    if (event != null) {
                        AwsAccount account = AccountDao.getAccountByAcctNum(event.getRecipientAccountId());
                        ResourceDetailRequest request = new ResourceDetailRequest(account, event.getAwsRegion(),
                                "EC2 Instance", value);
                        ResourceDetailDialog.showDialog(CloudTrailViewer.frame, request);
                    }
                }

            } else if (me.getClickCount() == 1) {

                try {
                    eventTablePanel.setFilterString(value);
                } catch (Exception ex) {
                    LOGGER.log(Level.WARNING, "Problem responding to mouse event on chart table", ex);
                }
            }
        }
    });

    TableColumn column;
    for (int i = 0; i < 3; i++) {
        column = table.getColumnModel().getColumn(i);

        switch (i) {
        case 0:
            column.setMinWidth(15);
            column.setMaxWidth(15);
            column.setPreferredWidth(15);
            break;

        case 2:
            column.setMinWidth(70);
            column.setMaxWidth(70);
            column.setPreferredWidth(70);
            break;
        }
    }

    JScrollPane tablecrollPane = new JScrollPane(table);
    tablecrollPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));

    this.add(tablecrollPane, BorderLayout.CENTER);
}

From source file:de.wusel.partyplayer.gui.PartyPlayer.java

private Component createPlayListPanel() {
    final PlaylistTableModel model = new PlaylistTableModel(playerModel, this);
    final JTable playlistTable = new JTable(model);
    playlistTable.getColumn(getText("table.playlist.column.votes.label")).setMaxWidth(40);
    playlistTable.getColumn(getText("table.playlist.column.votes.label")).setResizable(false);
    JScrollPane scrollPane = new JScrollPane(playlistTable);
    playlistTable.setFillsViewportHeight(true);
    return scrollPane;
}