Example usage for javax.swing JTable setDefaultRenderer

List of usage examples for javax.swing JTable setDefaultRenderer

Introduction

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

Prototype

public void setDefaultRenderer(Class<?> columnClass, TableCellRenderer renderer) 

Source Link

Document

Sets a default cell renderer to be used if no renderer has been set in a TableColumn.

Usage

From source file:pcgen.gui2.dialog.CharacterHPDialog.java

private void initComponents() {
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    Container pane = getContentPane();
    pane.setLayout(new BorderLayout());
    JTable table = new JTable(tableModel) {

        @Override//from   w  w  w  . jav a 2  s . c o m
        public TableCellEditor getCellEditor(int row, int column) {
            if (column == 5) {//TODO: the max roll should be calculated in a different manner
                String hd = levels.getClassTaken(levels.getElementAt(row)).getHD();
                int max = NumberUtils.toInt(hd);
                return new IntegerEditor(1, max);
            } else {
                return super.getCellEditor(row, column);
            }
        }

    };
    table.setDefaultRenderer(JButton.class, new Renderer());
    table.setDefaultEditor(JButton.class, new Editor());
    table.setCellSelectionEnabled(false);
    table.setRowHeight(new IntegerEditor(1, 10).getPreferredSize().height);
    JTableHeader header = table.getTableHeader();
    header.setReorderingAllowed(false);

    JScrollPane scrollPane = new JScrollPane(table);
    pane.add(scrollPane, BorderLayout.CENTER);

    Box box = Box.createHorizontalBox();
    box.add(new JLabel("Total Hp:"));
    box.add(Box.createHorizontalStrut(3));

    final ReferenceListener<Integer> hpListener = new ReferenceListener<Integer>() {

        @Override
        public void referenceChanged(ReferenceEvent<Integer> e) {
            totalHp.setText(e.getNewReference().toString());
        }

    };
    ReferenceFacade<Integer> hpRef = character.getTotalHPRef();
    totalHp.setText(hpRef.get().toString());
    hpRef.addReferenceListener(hpListener);
    box.add(totalHp);
    box.add(Box.createHorizontalStrut(5));

    JButton button = new JButton("Reroll All");
    button.setActionCommand("Reroll");
    button.addActionListener(this);
    box.add(button);

    box.add(Box.createHorizontalGlue());
    button = new JButton("Close");
    button.setActionCommand("Close");
    button.addActionListener(this);
    box.add(button);
    pane.add(box, BorderLayout.SOUTH);
    addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosed(WindowEvent e) {
            //Make sure to remove the listeners so that the garbage collector can
            //dispose of this dialog and prevent a memory leak
            levels.removeHitPointListener(tableModel);
            character.getTotalHPRef().removeReferenceListener(hpListener);
        }

    });

    Utility.installEscapeCloseOperation(this);
}

From source file:qic.ui.QicFrame.java

public QicFrame(Main main, String query) {
    super("QIC Search - Simple GUI");
    setLayout(new BorderLayout(5, 5));

    RSyntaxTextArea textArea = new RSyntaxTextArea(20, 60);
    textArea.setText("Enter a command in the textfield then press Enter..");
    textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JSON);
    textArea.setCodeFoldingEnabled(true);
    RTextScrollPane sp = new RTextScrollPane(textArea);

    JTextField searchTf = new JTextField(100);
    JButton runBtn = new JButton("Run");
    JPanel northPanel = new JPanel();
    northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.X_AXIS));
    northPanel.add(searchTf);/*  w  w  w.  j  ava  2 s  .  c o  m*/
    northPanel.add(runBtn);
    getContentPane().add(northPanel, BorderLayout.NORTH);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setSize(screenSize.width - 50, screenSize.height - 50);
    setLocationRelativeTo(null);

    searchTf.setText("search bo tmpsc ");
    if (query != null) {
        searchTf.setText(query);
    }

    JTable table = new JTable();
    table.setDefaultRenderer(List.class, new MultiLineTableCellRenderer());

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("Table", new JScrollPane(table));
    tabbedPane.addTab("JSON", new JScrollPane(sp));

    BeanPropertyTableModel<SearchResultItem> model = new BeanPropertyTableModel<>(SearchResultItem.class);
    model.setOrderedProperties(asList("id", "buyout", "item", "seller", "reqs", "mods", "q", "APS", "PDPS",
            "EDPS", "DPS", "ele", "phys", "ar", "ev", "ES", "blk", "crit", "lvl"));
    table.setModel(model);
    setColumnWidths(table.getColumnModel(), asList(1, 15, 280, 230, 50, 420));

    getContentPane().add(tabbedPane, BorderLayout.CENTER);

    ActionListener runCommand = e -> {
        String tfText = searchTf.getText();

        Worker<Command> pathNotesWorker = new Worker<Command>(() -> runQuery(main, tfText), command -> {
            String json = command.toJson();
            textArea.setText(json);
            model.setData(command.itemResults);
        }, ex -> {
            String stackTrace = ExceptionUtils.getStackTrace(ex);
            textArea.setText(stackTrace);
            showError(ex);
        });
        pathNotesWorker.execute();
    };

    searchTf.addActionListener(runCommand);
    runBtn.addActionListener(runCommand);

    table.getSelectionModel().addListSelectionListener(e -> {
        if (e.getValueIsAdjusting()) {
            int selectedRow = table.getSelectedRow();
            if (selectedRow > -1) {
                SearchResultItem searchResultItem = model.getData().get(selectedRow);
                SwingUtil.copyToClipboard(searchResultItem.wtb());
            }
        }
    });

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
}