List of usage examples for javax.swing ListSelectionModel setSelectionMode
void setSelectionMode(int selectionMode);
From source file:Main.java
public static void main(String args[]) { Object rows[][] = { { "one", "ichi" }, { "two", "ni" }, { "three", "san" }, { "four", "shi" }, { "five", "go" }, { "six", "roku" }, { "seven", "shichi" }, { "eight", "hachi" }, { "nine", "kyu" }, { "ten", "ju" } }; Object headers[] = { "English", "Japanese" }; String title = (args.length == 0 ? "JTable Sample" : args[0]); JFrame frame = new JFrame(title); TableColumnModel columnModel = new DefaultTableColumnModel(); TableColumn firstColumn = new TableColumn(1); firstColumn.setHeaderValue(headers[1]); columnModel.addColumn(firstColumn);/*from ww w.j a va 2s.c om*/ TableColumn secondColumn = new TableColumn(0); secondColumn.setHeaderValue(headers[0]); columnModel.addColumn(secondColumn); ListSelectionModel selectionModel = new DefaultListSelectionModel(); selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // JTable table = new JTable(model, columnModel, selectionModel); JTable table = new JTable(rows, headers); JScrollPane scrollPane = new JScrollPane(table); frame.getContentPane().add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:TableColumnTester.java
public static void main(String args[]) { Object rows[][] = { { "one", "ichi" }, { "two", "ni" }, { "three", "san" }, { "four", "shi" }, { "five", "go" }, { "six", "roku" }, { "seven", "shichi" }, { "eight", "hachi" }, { "nine", "kyu" }, { "ten", "ju" } }; Object headers[] = { "English", "Japanese" }; String title = (args.length == 0 ? "JTable Sample" : args[0]); JFrame frame = new JFrame(title); TableModel model = new DefaultTableModel(rows, headers); TableColumnModel columnModel = new DefaultTableColumnModel(); TableColumn firstColumn = new TableColumn(1); firstColumn.setHeaderValue(headers[1]); columnModel.addColumn(firstColumn);// w w w. ja v a2 s. c o m TableColumn secondColumn = new TableColumn(0); secondColumn.setHeaderValue(headers[0]); columnModel.addColumn(secondColumn); ListSelectionModel selectionModel = new DefaultListSelectionModel(); selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // JTable table = new JTable(model, columnModel, selectionModel); JTable table = new JTable(rows, headers); JScrollPane scrollPane = new JScrollPane(table); frame.getContentPane().add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] a) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTable table; String[] columnTitles = { "A", "B", "C", "D" }; Object[][] rowData = { { "11", "12", "13", "14" }, { "21", "22", "23", "24" }, { "31", "32", "33", "34" }, { "41", "42", "44", "44" } }; table = new JTable(rowData, columnTitles); table.setCellSelectionEnabled(true); ListSelectionModel cellSelectionModel = table.getSelectionModel(); cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); cellSelectionModel.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { String selectedData = null; int[] selectedRow = table.getSelectedRows(); int[] selectedColumns = table.getSelectedColumns(); for (int i = 0; i < selectedRow.length; i++) { for (int j = 0; j < selectedColumns.length; j++) { selectedData = (String) table.getValueAt(selectedRow[i], selectedColumns[j]); }/*from w w w . j a v a 2 s . co m*/ } System.out.println("Selected: " + selectedData); } }); frame.add(new JScrollPane(table)); frame.setSize(300, 200); frame.setVisible(true); }
From source file:ListIt.java
public ListIt() { JFrame f = new JFrame(); final PartsListModel pcm = new PartsListModel(); ListCellRenderer lcr = new MyLabelRenderer(); JList jl = new JList(pcm); jl.setCellRenderer(lcr);//from ww w . j ava 2 s . co m ListSelectionModel lsm = jl.getSelectionModel(); lsm.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); jl.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { if (!e.getValueIsAdjusting()) { String element[] = (String[]) pcm.getElementAt(e.getFirstIndex()); System.out.println(element[0] + " : " + element[1] + " : " + element[2]); } } }); JScrollPane jsp = new JScrollPane(jl); JComboBox jc = new JComboBox(pcm); jc.setRenderer(lcr); JButton jb = new JButton("Add Merchandise"); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { pcm.addElement(partsList[(int) (Math.random() * partsList.length)]); } }); Container c = f.getContentPane(); c.add(jsp, BorderLayout.NORTH); c.add(jc, BorderLayout.CENTER); c.add(jb, BorderLayout.SOUTH); f.setSize(250, 250); f.show(); }
From source file:simx.profiler.info.application.MessagesInfoTopComponent.java
public MessagesInfoTopComponent() { initComponents();// www . java2s.c om setName(Bundle.CTL_MessagesInfoTopComponent()); setToolTipText(Bundle.HINT_MessagesInfoTopComponent()); this.content = new InstanceContent(); this.associateLookup(new AbstractLookup(this.content)); this.profilingData = ProfilingData.getLoadedProfilingData(); this.messagesDataSet = new DefaultPieDataset(); this.createPieChart(this.messagesDataSet, this.graphicalPanel); final ListSelectionModel listSelectionModel = this.messageTypeInformationTable.getSelectionModel(); listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); listSelectionModel.addListSelectionListener((final ListSelectionEvent e) -> { if (messageTypeInformationTable.getSelectedRow() != -1) { final List<Map.Entry<MessageType, Integer>> data = new ArrayList<>( communicationData.getCommunicationData().entrySet()); final MessageType messageType = data.get(messageTypeInformationTable.getSelectedRow()).getKey(); content.set(Collections.singleton(messageType), null); } }); }
From source file:net.sf.keystore_explorer.gui.dialogs.extensions.DViewExtensions.java
private void initComponents() { ExtensionsTableModel extensionsTableModel = new ExtensionsTableModel(); jtExtensions = new JKseTable(extensionsTableModel); TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(extensionsTableModel); sorter.setComparator(2, new ObjectIdComparator()); jtExtensions.setRowSorter(sorter);//w ww . ja v a2s. c o m jtExtensions.setShowGrid(false); jtExtensions.setRowMargin(0); jtExtensions.getColumnModel().setColumnMargin(0); jtExtensions.getTableHeader().setReorderingAllowed(false); jtExtensions.setAutoResizeMode(JKseTable.AUTO_RESIZE_ALL_COLUMNS); jtExtensions.setRowHeight(Math.max(18, jtExtensions.getRowHeight())); for (int i = 0; i < jtExtensions.getColumnCount(); i++) { TableColumn column = jtExtensions.getColumnModel().getColumn(i); column.setHeaderRenderer( new ExtensionsTableHeadRend(jtExtensions.getTableHeader().getDefaultRenderer())); column.setCellRenderer(new ExtensionsTableCellRend()); } TableColumn criticalCol = jtExtensions.getColumnModel().getColumn(0); criticalCol.setResizable(false); criticalCol.setMinWidth(28); criticalCol.setMaxWidth(28); criticalCol.setPreferredWidth(28); ListSelectionModel selectionModel = jtExtensions.getSelectionModel(); selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); selectionModel.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent evt) { if (!evt.getValueIsAdjusting()) { try { CursorUtil.setCursorBusy(DViewExtensions.this); updateExtensionValue(); } finally { CursorUtil.setCursorFree(DViewExtensions.this); } } } }); jspExtensionsTable = PlatformUtil.createScrollPane(jtExtensions, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); jspExtensionsTable.getViewport().setBackground(jtExtensions.getBackground()); jpExtensionsTable = new JPanel(new BorderLayout(5, 5)); jpExtensionsTable.setPreferredSize(new Dimension(500, 200)); jpExtensionsTable.add(jspExtensionsTable, BorderLayout.CENTER); jpExtensionValue = new JPanel(new BorderLayout(5, 5)); jlExtensionValue = new JLabel(res.getString("DViewExtensions.jlExtensionValue.text")); jpExtensionValue.add(jlExtensionValue, BorderLayout.NORTH); jepExtensionValue = new JEditorPane(); jepExtensionValue.setFont(new Font(Font.MONOSPACED, Font.PLAIN, LnfUtil.getDefaultFontSize())); jepExtensionValue.setEditable(false); jepExtensionValue.setToolTipText(res.getString("DViewExtensions.jtaExtensionValue.tooltip")); // JGoodies - keep uneditable color same as editable jepExtensionValue.putClientProperty("JTextArea.infoBackground", Boolean.TRUE); // for displaying URLs in extensions as clickable links jepExtensionValue.setContentType("text/html"); jepExtensionValue.addHyperlinkListener(this); // use default font and foreground color from the component jepExtensionValue.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE); jspExtensionValue = PlatformUtil.createScrollPane(jepExtensionValue, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); jpExtensionValueTextArea = new JPanel(new BorderLayout(5, 5)); jpExtensionValueTextArea.setPreferredSize(new Dimension(500, 200)); jpExtensionValueTextArea.add(jspExtensionValue, BorderLayout.CENTER); jpExtensionValue.add(jpExtensionValueTextArea, BorderLayout.CENTER); jbAsn1 = new JButton(res.getString("DViewExtensions.jbAsn1.text")); PlatformUtil.setMnemonic(jbAsn1, res.getString("DViewExtensions.jbAsn1.mnemonic").charAt(0)); jbAsn1.setToolTipText(res.getString("DViewExtensions.jbAsn1.tooltip")); jbAsn1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { try { CursorUtil.setCursorBusy(DViewExtensions.this); asn1DumpPressed(); } finally { CursorUtil.setCursorFree(DViewExtensions.this); } } }); jpExtensionValueAsn1 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); jpExtensionValueAsn1.add(jbAsn1); jpExtensionValue.add(jpExtensionValueAsn1, BorderLayout.SOUTH); jpExtensions = new JPanel(new GridLayout(2, 1, 5, 5)); jpExtensions.setBorder(new CompoundBorder(new EmptyBorder(5, 5, 5, 5), new CompoundBorder(new EtchedBorder(), new EmptyBorder(5, 5, 5, 5)))); jpExtensions.add(jpExtensionsTable); jpExtensions.add(jpExtensionValue); jbOK = new JButton(res.getString("DViewExtensions.jbOK.text")); jbOK.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { okPressed(); } }); jpOK = PlatformUtil.createDialogButtonPanel(jbOK, false); extensionsTableModel.load(extensions); if (extensionsTableModel.getRowCount() > 0) { jtExtensions.changeSelection(0, 0, false, false); } getContentPane().add(jpExtensions, BorderLayout.CENTER); getContentPane().add(jpOK, BorderLayout.SOUTH); setResizable(false); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent evt) { closeDialog(); } }); getRootPane().setDefaultButton(jbOK); pack(); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { jbOK.requestFocus(); } }); }
From source file:net.sf.jabref.gui.plaintextimport.TextInputDialog.java
private void initRawPanel() { rawPanel.setLayout(new BorderLayout()); // Textarea/*from ww w.java 2s. c om*/ textPane.setEditable(false); document = textPane.getStyledDocument(); addStylesToDocument(); try { document.insertString(0, "", document.getStyle("regular")); } catch (BadLocationException ex) { LOGGER.warn("Problem setting style", ex); } OverlayPanel testPanel = new OverlayPanel(textPane, Localization.lang("paste text here")); testPanel.setPreferredSize(new Dimension(450, 255)); testPanel.setMaximumSize(new Dimension(450, Integer.MAX_VALUE)); // Setup fields (required to be done before setting up popup menu) fieldList = new JList<>(getAllFields()); fieldList.setCellRenderer(new SimpleCellRenderer(fieldList.getFont())); ListSelectionModel listSelectionModel = fieldList.getSelectionModel(); listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); listSelectionModel.addListSelectionListener(new FieldListSelectionHandler()); fieldList.addMouseListener(new FieldListMouseListener()); // After the call to getAllFields initPopupMenuAndToolbar(); //Add listener to components that can bring up popup menus. MouseListener popupListener = new PopupListener(inputMenu); textPane.addMouseListener(popupListener); testPanel.addMouseListener(popupListener); JPanel leftPanel = new JPanel(new BorderLayout()); leftPanel.add(toolBar, BorderLayout.NORTH); leftPanel.add(testPanel, BorderLayout.CENTER); JPanel inputPanel = setUpFieldListPanel(); // parse with FreeCite button parseWithFreeCiteButton.addActionListener(event -> { if (parseWithFreeCiteAndAddEntries()) { okPressed = false; // we do not want to have the super method to handle our entries, we do it on our own dispose(); } }); rawPanel.add(leftPanel, BorderLayout.CENTER); rawPanel.add(inputPanel, BorderLayout.EAST); JLabel desc = new JLabel("<html><h3>" + Localization.lang("Plain text import") + "</h3><p>" + Localization.lang("This is a simple copy and paste dialog. First load or paste some text into " + "the text input area.<br>After that, you can mark text and assign it to a BibTeX field.") + "</p></html>"); desc.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); rawPanel.add(desc, BorderLayout.SOUTH); }
From source file:TableIt.java
public TableIt() { JFrame f = new JFrame(); TableModel tm = new MyTableModel(); final JTable table = new JTable(tm); TableColumnModel tcm = table.getColumnModel(); TableColumn column = tcm.getColumn(tcm.getColumnCount() - 1); TableCellRenderer renderer = new MyTableCellRenderer(); column.setCellRenderer(renderer);/*www. j a v a 2s . c om*/ JButton selectionType = new JButton("Next Type"); selectionType.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { boolean rowSet = table.getRowSelectionAllowed(); boolean colSet = table.getColumnSelectionAllowed(); boolean cellSet = table.getCellSelectionEnabled(); boolean setRow = !rowSet; boolean setCol = rowSet ^ colSet; boolean setCell = rowSet & colSet; table.setCellSelectionEnabled(setCell); table.setColumnSelectionAllowed(setCol); table.setRowSelectionAllowed(setRow); System.out.println("Row Selection Allowed? " + setRow); System.out.println("Column Selection Allowed? " + setCol); System.out.println("Cell Selection Enabled? " + setCell); table.repaint(); } }); JButton selectionMode = new JButton("Next Mode"); selectionMode.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ListSelectionModel lsm = table.getSelectionModel(); int mode = lsm.getSelectionMode(); int nextMode; String nextModeString; if (mode == ListSelectionModel.SINGLE_SELECTION) { nextMode = ListSelectionModel.SINGLE_INTERVAL_SELECTION; nextModeString = "Single Interval Selection"; } else if (mode == ListSelectionModel.SINGLE_INTERVAL_SELECTION) { nextMode = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION; nextModeString = "Multiple Interval Selection"; } else { nextMode = ListSelectionModel.SINGLE_SELECTION; nextModeString = "Single Selection"; } lsm.setSelectionMode(nextMode); System.out.println("Selection Mode: " + nextModeString); table.repaint(); } }); JPanel jp = new JPanel(); jp.add(selectionType); jp.add(selectionMode); JScrollPane jsp = new JScrollPane(table); Container c = f.getContentPane(); c.add(jsp, BorderLayout.CENTER); c.add(jp, BorderLayout.SOUTH); f.setSize(300, 250); f.show(); }
From source file:simx.profiler.info.actor.ActorTypeInfoTopComponent.java
/** * This constructor initializes the top component. It configures the * tales creates the visulizations.// w w w . jav a2 s .c om */ public ActorTypeInfoTopComponent() { initComponents(); setName(Bundle.CTL_ActorTypeInfoTopComponent()); setToolTipText(Bundle.HINT_ActorTypeInfoTopComponent()); this.content = new InstanceContent(); this.associateLookup(new AbstractLookup(this.content)); ListSelectionModel listSelectionModel = this.instancesTable.getSelectionModel(); listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); listSelectionModel.addListSelectionListener((final ListSelectionEvent e) -> { if (instancesTable.getSelectedRow() != -1) { final ActorInstance actorInstance = actorInstances.get(instancesTable.getSelectedRow()); final ActorType actorType = actorInstance.type; final Set<Object> selectedObjects = new HashSet<>(); selectedObjects.add(actorType); selectedObjects.add(actorInstance); content.set(selectedObjects, null); } }); listSelectionModel = this.messagesSentTable.getSelectionModel(); listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); listSelectionModel.addListSelectionListener((ListSelectionEvent e) -> { if (messagesSentTable.getSelectedRow() != -1) { messagesReceivedTable.clearSelection(); messagesProcessedTable.clearSelection(); if (selectedMessageType != null) content.remove(selectedMessageType); selectedMessageType = sentMessages.get(messagesSentTable.getSelectedRow()).getKey(); content.add(selectedMessageType); } }); listSelectionModel = this.messagesReceivedTable.getSelectionModel(); listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); listSelectionModel.addListSelectionListener((ListSelectionEvent e) -> { if (messagesReceivedTable.getSelectedRow() != -1) { messagesSentTable.clearSelection(); messagesProcessedTable.clearSelection(); if (selectedMessageType != null) content.remove(selectedMessageType); selectedMessageType = receivedMessages.get(messagesReceivedTable.getSelectedRow()).getKey(); content.add(selectedMessageType); } }); listSelectionModel = this.messagesProcessedTable.getSelectionModel(); listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); listSelectionModel.addListSelectionListener((ListSelectionEvent e) -> { if (messagesProcessedTable.getSelectedRow() != -1) { messagesSentTable.clearSelection(); messagesReceivedTable.clearSelection(); if (selectedMessageType != null) content.remove(selectedMessageType); selectedMessageType = processedMessages.get(messagesProcessedTable.getSelectedRow()).getKey(); content.add(selectedMessageType); } }); this.messagesSentDataSet = new DefaultPieDataset(); this.messagesReceivedDataSet = new DefaultPieDataset(); this.messagesProcessedDataSet = new DefaultPieDataset(); this.createPieChart(this.messagesSentDataSet, this.messagesSentPanel); this.createPieChart(this.messagesReceivedDataSet, this.messagesReceivedPanel); this.createPieChart(this.messagesProcessedDataSet, this.messagesProcessedPanel); }
From source file:simx.profiler.info.actor.ActorInstanceInfoTopComponent.java
/** * This constructor initializes the top component. It configures the * tales creates the visulizations./* w w w .j av a 2 s. com*/ */ public ActorInstanceInfoTopComponent() { initComponents(); setName(Bundle.CTL_ActorInstanceInfoTopComponent()); setToolTipText(Bundle.HINT_ActorInstanceInfoTopComponent()); this.content = new InstanceContent(); this.associateLookup(new AbstractLookup(this.content)); ListSelectionModel listSelectionModel = this.messageSentTable.getSelectionModel(); listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); listSelectionModel.addListSelectionListener((ListSelectionEvent e) -> { if (messageSentTable.getSelectedRow() != -1) { messagesReceivedTable.clearSelection(); messageProcessedTable.clearSelection(); if (selectedMessageType != null) content.remove(selectedMessageType); selectedMessageType = sentMessages.get(messageSentTable.getSelectedRow()).getKey(); content.add(selectedMessageType); } }); listSelectionModel = this.messagesReceivedTable.getSelectionModel(); listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); listSelectionModel.addListSelectionListener((ListSelectionEvent e) -> { if (messagesReceivedTable.getSelectedRow() != -1) { messageSentTable.clearSelection(); messageProcessedTable.clearSelection(); if (selectedMessageType != null) content.remove(selectedMessageType); selectedMessageType = receivedMessages.get(messagesReceivedTable.getSelectedRow()).getKey(); content.add(selectedMessageType); } }); listSelectionModel = this.messageProcessedTable.getSelectionModel(); listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); listSelectionModel.addListSelectionListener((ListSelectionEvent e) -> { if (messageProcessedTable.getSelectedRow() != -1) { messageSentTable.clearSelection(); messagesReceivedTable.clearSelection(); if (selectedMessageType != null) content.remove(selectedMessageType); selectedMessageType = processedMessages.get(messageProcessedTable.getSelectedRow()).getKey(); content.add(selectedMessageType); } }); this.messagesSentDataSet = new DefaultPieDataset(); this.messagesReceivedDataSet = new DefaultPieDataset(); this.messagesProcessedDataSet = new DefaultPieDataset(); this.createPieChart(this.messagesSentDataSet, this.messagesSentPanel); this.createPieChart(this.messagesReceivedDataSet, this.messagesReceivedPanel); this.createPieChart(this.messagesProcessedDataSet, this.messagesProcessedPanel); this.frequencyPlotData = new XYSeriesCollection(); JFreeChart chart = ChartFactory.createXYLineChart("", "", "", this.frequencyPlotData); final ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new java.awt.Dimension(261, 157)); this.frequencyPanel.setLayout(new BorderLayout()); this.frequencyPanel.add(chartPanel, BorderLayout.CENTER); }