Example usage for javax.swing JTable getSelectionModel

List of usage examples for javax.swing JTable getSelectionModel

Introduction

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

Prototype

public ListSelectionModel getSelectionModel() 

Source Link

Document

Returns the ListSelectionModel that is used to maintain row selection state.

Usage

From source file:pcgen.gui2.converter.panel.CampaignPanel.java

private void initSourceSelection(CampaignTableModel model, JTable table) {
    // Select any previous selections
    PCGenSettings context = PCGenSettings.getInstance();
    String sourceString = context.initProperty(PCGenSettings.CONVERT_SOURCES, "");
    String[] sources = sourceString.split("\\|");
    for (String srcName : sources) {
        for (Campaign camp : gameModeCampaigns) {
            if (camp.toString().equals(srcName)) {
                for (int i = 0; i < model.getRowCount(); i++) {
                    if (camp.equals(model.getValueAt(i, 0))) {
                        table.getSelectionModel().addSelectionInterval(i, i);
                        break;
                    }// w  ww  . j  av  a 2 s.co  m
                }
                break;
            }
        }
    }
}

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);//from  ww w  .  j av a2  s  . c om
    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);
}