List of usage examples for javax.swing ListSelectionModel SINGLE_INTERVAL_SELECTION
int SINGLE_INTERVAL_SELECTION
To view the source code for javax.swing ListSelectionModel SINGLE_INTERVAL_SELECTION.
Click Source Link
From source file:ListSelectionDemo.java
public ListSelectionDemo() { super(new BorderLayout()); String[] listData = { "one", "two", "three", "four", "five", "six", "seven" }; String[] columnNames = { "French", "Spanish", "Italian" }; list = new JList(listData); listSelectionModel = list.getSelectionModel(); listSelectionModel.addListSelectionListener(new SharedListSelectionHandler()); JScrollPane listPane = new JScrollPane(list); JPanel controlPane = new JPanel(); String[] modes = { "SINGLE_SELECTION", "SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION" }; final JComboBox comboBox = new JComboBox(modes); comboBox.setSelectedIndex(2);//w w w . ja va2s . c o m comboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String newMode = (String) comboBox.getSelectedItem(); if (newMode.equals("SINGLE_SELECTION")) { listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); } else if (newMode.equals("SINGLE_INTERVAL_SELECTION")) { listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); } else { listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); } output.append("----------" + "Mode: " + newMode + "----------" + newline); } }); controlPane.add(new JLabel("Selection mode:")); controlPane.add(comboBox); // Build output area. output = new JTextArea(1, 10); output.setEditable(false); JScrollPane outputPane = new JScrollPane(output, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); // Do the layout. JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); add(splitPane, BorderLayout.CENTER); JPanel topHalf = new JPanel(); topHalf.setLayout(new BoxLayout(topHalf, BoxLayout.LINE_AXIS)); JPanel listContainer = new JPanel(new GridLayout(1, 1)); listContainer.setBorder(BorderFactory.createTitledBorder("List")); listContainer.add(listPane); topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5)); topHalf.add(listContainer); // topHalf.add(tableContainer); topHalf.setMinimumSize(new Dimension(100, 50)); topHalf.setPreferredSize(new Dimension(100, 110)); splitPane.add(topHalf); JPanel bottomHalf = new JPanel(new BorderLayout()); bottomHalf.add(controlPane, BorderLayout.PAGE_START); bottomHalf.add(outputPane, BorderLayout.CENTER); // XXX: next line needed if bottomHalf is a scroll pane: // bottomHalf.setMinimumSize(new Dimension(400, 50)); bottomHalf.setPreferredSize(new Dimension(450, 135)); splitPane.add(bottomHalf); }
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);//from w w w . ja v a2s .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:events.ListDataEventDemo.java
public ListDataEventDemo() { super(new BorderLayout()); //Create and populate the list model. listModel = new DefaultListModel(); listModel.addElement("Whistler, Canada"); listModel.addElement("Jackson Hole, Wyoming"); listModel.addElement("Squaw Valley, California"); listModel.addElement("Telluride, Colorado"); listModel.addElement("Taos, New Mexico"); listModel.addElement("Snowbird, Utah"); listModel.addElement("Chamonix, France"); listModel.addElement("Banff, Canada"); listModel.addElement("Arapahoe Basin, Colorado"); listModel.addElement("Kirkwood, California"); listModel.addElement("Sun Valley, Idaho"); listModel.addListDataListener(new MyListDataListener()); //Create the list and put it in a scroll pane. list = new JList(listModel); list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); list.setSelectedIndex(0);/* www . j a v a2 s .co m*/ list.addListSelectionListener(this); JScrollPane listScrollPane = new JScrollPane(list); //Create the list-modifying buttons. addButton = new JButton(addString); addButton.setActionCommand(addString); addButton.addActionListener(new AddButtonListener()); deleteButton = new JButton(deleteString); deleteButton.setActionCommand(deleteString); deleteButton.addActionListener(new DeleteButtonListener()); ImageIcon icon = createImageIcon("Up16"); if (icon != null) { upButton = new JButton(icon); upButton.setMargin(new Insets(0, 0, 0, 0)); } else { upButton = new JButton("Move up"); } upButton.setToolTipText("Move the currently selected list item higher."); upButton.setActionCommand(upString); upButton.addActionListener(new UpDownListener()); icon = createImageIcon("Down16"); if (icon != null) { downButton = new JButton(icon); downButton.setMargin(new Insets(0, 0, 0, 0)); } else { downButton = new JButton("Move down"); } downButton.setToolTipText("Move the currently selected list item lower."); downButton.setActionCommand(downString); downButton.addActionListener(new UpDownListener()); JPanel upDownPanel = new JPanel(new GridLayout(2, 1)); upDownPanel.add(upButton); upDownPanel.add(downButton); //Create the text field for entering new names. nameField = new JTextField(15); nameField.addActionListener(new AddButtonListener()); String name = listModel.getElementAt(list.getSelectedIndex()).toString(); nameField.setText(name); //Create a control panel, using the default FlowLayout. JPanel buttonPane = new JPanel(); buttonPane.add(nameField); buttonPane.add(addButton); buttonPane.add(deleteButton); buttonPane.add(upDownPanel); //Create the log for reporting list data events. log = new JTextArea(10, 20); JScrollPane logScrollPane = new JScrollPane(log); //Create a split pane for the log and the list. JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, listScrollPane, logScrollPane); splitPane.setResizeWeight(0.5); //Put everything together. add(buttonPane, BorderLayout.PAGE_START); add(splitPane, BorderLayout.CENTER); }
From source file:TableSelectionDemo.java
public TableSelectionDemo() { super(new BorderLayout()); String[] columnNames = { "French", "Spanish", "Italian" }; String[][] tableData = { { "un", "uno", "uno" }, { "deux", "dos", "due" }, { "trois", "tres", "tre" }, { "quatre", "cuatro", "quattro" }, { "cinq", "cinco", "cinque" }, { "six", "seis", "sei" }, { "sept", "siete", "sette" } }; table = new JTable(tableData, columnNames); listSelectionModel = table.getSelectionModel(); listSelectionModel.addListSelectionListener(new SharedListSelectionHandler()); table.setSelectionModel(listSelectionModel); JScrollPane tablePane = new JScrollPane(table); // Build control area (use default FlowLayout). JPanel controlPane = new JPanel(); String[] modes = { "SINGLE_SELECTION", "SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION" }; final JComboBox comboBox = new JComboBox(modes); comboBox.setSelectedIndex(2);/*from ww w. j a v a2s . c o m*/ comboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String newMode = (String) comboBox.getSelectedItem(); if (newMode.equals("SINGLE_SELECTION")) { listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); } else if (newMode.equals("SINGLE_INTERVAL_SELECTION")) { listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); } else { listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); } output.append("----------" + "Mode: " + newMode + "----------" + newline); } }); controlPane.add(new JLabel("Selection mode:")); controlPane.add(comboBox); // Build output area. output = new JTextArea(1, 10); output.setEditable(false); JScrollPane outputPane = new JScrollPane(output, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); // Do the layout. JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); add(splitPane, BorderLayout.CENTER); JPanel topHalf = new JPanel(); topHalf.setLayout(new BoxLayout(topHalf, BoxLayout.LINE_AXIS)); JPanel listContainer = new JPanel(new GridLayout(1, 1)); JPanel tableContainer = new JPanel(new GridLayout(1, 1)); tableContainer.setBorder(BorderFactory.createTitledBorder("Table")); tableContainer.add(tablePane); tablePane.setPreferredSize(new Dimension(420, 130)); topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5)); topHalf.add(listContainer); topHalf.add(tableContainer); topHalf.setMinimumSize(new Dimension(250, 50)); topHalf.setPreferredSize(new Dimension(200, 110)); splitPane.add(topHalf); JPanel bottomHalf = new JPanel(new BorderLayout()); bottomHalf.add(controlPane, BorderLayout.PAGE_START); bottomHalf.add(outputPane, BorderLayout.CENTER); // XXX: next line needed if bottomHalf is a scroll pane: // bottomHalf.setMinimumSize(new Dimension(400, 50)); bottomHalf.setPreferredSize(new Dimension(450, 110)); splitPane.add(bottomHalf); }
From source file:net.pandoragames.far.ui.swing.FileListPanel.java
private void init(SwingConfig config, ComponentRepository componentRepository) { this.setLayout(new BorderLayout()); this.setBorder( BorderFactory.createEmptyBorder(0, SwingConfig.PADDING, SwingConfig.PADDING, SwingConfig.PADDING)); tableModel = componentRepository.getTableModel(); componentRepository.getResetDispatcher().addResetable(tableModel); componentRepository.getSearchBaseListener().addResetable(tableModel); componentRepository.getUndoListener().setTableModel(tableModel); JTable fileListTable = componentRepository.getFileSetTable(); int totalWidth = fileListTable.getPreferredSize().width; fileListTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); fileListTable.setColumnSelectionAllowed(true); fileListTable.getTableHeader().addMouseListener(new TableHeaderMouseListener()); fileListTable.getTableHeader().getColumnModel().getColumn(0) .setHeaderRenderer(new TableHeaderCheckBoxColumnRenderer()); fileListPopupMenu = new FileListPopupMenu(fileListTable, tableModel, componentRepository, config); fileListTable.setComponentPopupMenu(fileListPopupMenu); fileListTable.addMouseListener(new FileViewOpener(fileListTable, componentRepository.getRootWindow(), config, componentRepository)); fileListTable.getColumnModel().getColumn(0).setPreferredWidth(20); fileListTable.getColumnModel().getColumn(0).setMaxWidth(20); fileListTable.getColumnModel().getColumn(1).setCellRenderer(new TargetFileListTableCellRenderer()); fileListTable.getColumnModel().getColumn(1).setPreferredWidth(2 * totalWidth / 5); fileListTable.getColumnModel().getColumn(2).setCellRenderer(new PathColumnRenderer()); fileListTable.getColumnModel().getColumn(3).setCellRenderer(new InfoColumnRenderer(config)); JScrollPane scrollPane = new JScrollPane(fileListTable); this.add(scrollPane, BorderLayout.CENTER); SelectCounter fileCounter = new SelectCounter(); fileCounter.setBorder(BorderFactory.createEmptyBorder(1, SwingConfig.PADDING, 2, SwingConfig.PADDING)); fileCounter.setForeground(Color.GRAY); ErrorCounter errorCounter = new ErrorCounter(); errorCounter.setBorder(BorderFactory.createEmptyBorder(1, SwingConfig.PADDING, 2, SwingConfig.PADDING)); JPanel counterLine = new JPanel(); counterLine.setLayout(new BorderLayout()); counterLine.add(fileCounter, BorderLayout.WEST); counterLine.add(errorCounter, BorderLayout.EAST); JProgressBar progressBar = new JProgressBar(); progressBar.setEnabled(false);// w w w . j av a 2 s . co m progressBar.setMaximumSize(new Dimension(100, 20)); progressBar.setBorder(BorderFactory.createEmptyBorder(1, SwingConfig.PADDING, 2, SwingConfig.PADDING)); componentRepository.getProgressBarUpdater().setProgressBar(progressBar); JPanel progressBarPanel = new JPanel(); progressBarPanel.add(progressBar); counterLine.add(progressBarPanel, BorderLayout.CENTER); counterLine.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1)); this.add(counterLine, BorderLayout.SOUTH); tableModel.addTableModelListener(new ColumnCountListener(fileCounter)); tableModel.addTableModelListener(errorCounter); componentRepository.getResetDispatcher().addResetable(fileCounter); componentRepository.getSearchBaseListener().addResetable(fileCounter); componentRepository.getOperationCallBackListener().addComponentStartReseted(fileCounter, OperationType.FIND); componentRepository.getResetDispatcher().addResetable(errorCounter); componentRepository.getSearchBaseListener().addResetable(errorCounter); viewAction = new ActionView(componentRepository, config); }
From source file:GeMSE.GS.Analysis.Stats.OneSampleCovariancePanel.java
/** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor./* ww w. ja va2s . co m*/ */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { BiasCorrectedCB = new javax.swing.JCheckBox(); jTabbedPane1 = new javax.swing.JTabbedPane(); jPanel1 = new javax.swing.JPanel(); HeatmapL = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); matrixDG = new javax.swing.JTable(); BiasCorrectedCB.setText("bias corrected"); BiasCorrectedCB.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BiasCorrectedCBActionPerformed(evt); } }); HeatmapL.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); HeatmapL.setText("GeMSE"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addComponent(HeatmapL, javax.swing.GroupLayout.DEFAULT_SIZE, 607, Short.MAX_VALUE) .addContainerGap())); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addComponent(HeatmapL, javax.swing.GroupLayout.DEFAULT_SIZE, 444, Short.MAX_VALUE) .addContainerGap())); jTabbedPane1.addTab(" Heat Map ", jPanel1); matrixDG.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { {}, {}, {}, {} }, new String[] { })); matrixDG.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF); matrixDG.setCellSelectionEnabled(true); matrixDG.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION); jScrollPane1.setViewportView(matrixDG); jTabbedPane1.addTab(" Grid View ", jScrollPane1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(BiasCorrectedCB) .addGap(0, 0, Short.MAX_VALUE)) .addComponent(jTabbedPane1)) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(BiasCorrectedCB) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTabbedPane1).addContainerGap())); }
From source file:components.SharedModelDemo.java
public SharedModelDemo() { super(new BorderLayout()); Vector data = new Vector(7); String[] columnNames = { "French", "Spanish", "Italian" }; String[] oneData = { "un", "uno", "uno" }; String[] twoData = { "deux", "dos", "due" }; String[] threeData = { "trois", "tres", "tre" }; String[] fourData = { "quatre", "cuatro", "quattro" }; String[] fiveData = { "cinq", "cinco", "cinque" }; String[] sixData = { "six", "seis", "sei" }; String[] sevenData = { "sept", "siete", "sette" }; //Build the model. SharedDataModel dataModel = new SharedDataModel(columnNames); dataModel.addElement(oneData);//from w w w . jav a 2 s . c o m dataModel.addElement(twoData); dataModel.addElement(threeData); dataModel.addElement(fourData); dataModel.addElement(fiveData); dataModel.addElement(sixData); dataModel.addElement(sevenData); list = new JList(dataModel); list.setCellRenderer(new DefaultListCellRenderer() { public Component getListCellRendererComponent(JList l, Object value, int i, boolean s, boolean f) { String[] array = (String[]) value; return super.getListCellRendererComponent(l, array[0], i, s, f); } }); listSelectionModel = list.getSelectionModel(); listSelectionModel.addListSelectionListener(new SharedListSelectionHandler()); JScrollPane listPane = new JScrollPane(list); table = new JTable(dataModel); table.setSelectionModel(listSelectionModel); JScrollPane tablePane = new JScrollPane(table); //Build control area (use default FlowLayout). JPanel controlPane = new JPanel(); String[] modes = { "SINGLE_SELECTION", "SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION" }; final JComboBox comboBox = new JComboBox(modes); comboBox.setSelectedIndex(2); comboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String newMode = (String) comboBox.getSelectedItem(); if (newMode.equals("SINGLE_SELECTION")) { listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); } else if (newMode.equals("SINGLE_INTERVAL_SELECTION")) { listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); } else { listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); } output.append("----------" + "Mode: " + newMode + "----------" + newline); } }); controlPane.add(new JLabel("Selection mode:")); controlPane.add(comboBox); //Build output area. output = new JTextArea(10, 40); output.setEditable(false); JScrollPane outputPane = new JScrollPane(output, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); //Do the layout. JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); add(splitPane, BorderLayout.CENTER); JPanel topHalf = new JPanel(); topHalf.setLayout(new BoxLayout(topHalf, BoxLayout.X_AXIS)); JPanel listContainer = new JPanel(new GridLayout(1, 1)); listContainer.setBorder(BorderFactory.createTitledBorder("List")); listContainer.add(listPane); JPanel tableContainer = new JPanel(new GridLayout(1, 1)); tableContainer.setBorder(BorderFactory.createTitledBorder("Table")); tableContainer.add(tablePane); tablePane.setPreferredSize(new Dimension(300, 100)); topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5)); topHalf.add(listContainer); topHalf.add(tableContainer); topHalf.setMinimumSize(new Dimension(400, 50)); topHalf.setPreferredSize(new Dimension(400, 110)); splitPane.add(topHalf); JPanel bottomHalf = new JPanel(new BorderLayout()); bottomHalf.add(controlPane, BorderLayout.NORTH); bottomHalf.add(outputPane, BorderLayout.CENTER); //XXX: next line needed if bottomHalf is a scroll pane: //bottomHalf.setMinimumSize(new Dimension(400, 50)); bottomHalf.setPreferredSize(new Dimension(450, 135)); splitPane.add(bottomHalf); }
From source file:Main.java
public Main() { super(new BorderLayout()); // Create and populate the list model. listModel = new DefaultListModel(); listModel.addElement("Whistler, Canada"); listModel.addElement("Jackson Hole, Wyoming"); listModel.addElement("Squaw Valley, California"); listModel.addElement("Telluride, Colorado"); listModel.addElement("Taos, New Mexico"); listModel.addElement("Snowbird, Utah"); listModel.addElement("Chamonix, France"); listModel.addElement("Banff, Canada"); listModel.addElement("Arapahoe Basin, Colorado"); listModel.addElement("Kirkwood, California"); listModel.addElement("Sun Valley, Idaho"); listModel.addListDataListener(new MyListDataListener()); // Create the list and put it in a scroll pane. list = new JList(listModel); list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); list.setSelectedIndex(0);//from w ww . j a va 2 s.com list.addListSelectionListener(this); JScrollPane listScrollPane = new JScrollPane(list); // Create the list-modifying buttons. addButton = new JButton(addString); addButton.setActionCommand(addString); addButton.addActionListener(new AddButtonListener()); deleteButton = new JButton(deleteString); deleteButton.setActionCommand(deleteString); deleteButton.addActionListener(new DeleteButtonListener()); ImageIcon icon = createImageIcon("Up16"); if (icon != null) { upButton = new JButton(icon); upButton.setMargin(new Insets(0, 0, 0, 0)); } else { upButton = new JButton("Move up"); } upButton.setToolTipText("Move the currently selected list item higher."); upButton.setActionCommand(upString); upButton.addActionListener(new UpDownListener()); icon = createImageIcon("Down16"); if (icon != null) { downButton = new JButton(icon); downButton.setMargin(new Insets(0, 0, 0, 0)); } else { downButton = new JButton("Move down"); } downButton.setToolTipText("Move the currently selected list item lower."); downButton.setActionCommand(downString); downButton.addActionListener(new UpDownListener()); JPanel upDownPanel = new JPanel(new GridLayout(2, 1)); upDownPanel.add(upButton); upDownPanel.add(downButton); // Create the text field for entering new names. nameField = new JTextField(15); nameField.addActionListener(new AddButtonListener()); String name = listModel.getElementAt(list.getSelectedIndex()).toString(); nameField.setText(name); // Create a control panel, using the default FlowLayout. JPanel buttonPane = new JPanel(); buttonPane.add(nameField); buttonPane.add(addButton); buttonPane.add(deleteButton); buttonPane.add(upDownPanel); // Create the log for reporting list data events. log = new JTextArea(10, 20); JScrollPane logScrollPane = new JScrollPane(log); // Create a split pane for the log and the list. JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, listScrollPane, logScrollPane); splitPane.setResizeWeight(0.5); // Put everything together. add(buttonPane, BorderLayout.PAGE_START); add(splitPane, BorderLayout.CENTER); }
From source file:ListDataEventDemo.java
public ListDataEventDemo() { super(new BorderLayout()); // Create and populate the list model. listModel = new DefaultListModel(); listModel.addElement("Whistler, Canada"); listModel.addElement("Jackson Hole, Wyoming"); listModel.addElement("Squaw Valley, California"); listModel.addElement("Telluride, Colorado"); listModel.addElement("Taos, New Mexico"); listModel.addElement("Snowbird, Utah"); listModel.addElement("Chamonix, France"); listModel.addElement("Banff, Canada"); listModel.addElement("Arapahoe Basin, Colorado"); listModel.addElement("Kirkwood, California"); listModel.addElement("Sun Valley, Idaho"); listModel.addListDataListener(new MyListDataListener()); // Create the list and put it in a scroll pane. list = new JList(listModel); list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); list.setSelectedIndex(0);//from ww w . ja v a2 s.co m list.addListSelectionListener(this); JScrollPane listScrollPane = new JScrollPane(list); // Create the list-modifying buttons. addButton = new JButton(addString); addButton.setActionCommand(addString); addButton.addActionListener(new AddButtonListener()); deleteButton = new JButton(deleteString); deleteButton.setActionCommand(deleteString); deleteButton.addActionListener(new DeleteButtonListener()); ImageIcon icon = createImageIcon("Up16"); if (icon != null) { upButton = new JButton(icon); upButton.setMargin(new Insets(0, 0, 0, 0)); } else { upButton = new JButton("Move up"); } upButton.setToolTipText("Move the currently selected list item higher."); upButton.setActionCommand(upString); upButton.addActionListener(new UpDownListener()); icon = createImageIcon("Down16"); if (icon != null) { downButton = new JButton(icon); downButton.setMargin(new Insets(0, 0, 0, 0)); } else { downButton = new JButton("Move down"); } downButton.setToolTipText("Move the currently selected list item lower."); downButton.setActionCommand(downString); downButton.addActionListener(new UpDownListener()); JPanel upDownPanel = new JPanel(new GridLayout(2, 1)); upDownPanel.add(upButton); upDownPanel.add(downButton); // Create the text field for entering new names. nameField = new JTextField(15); nameField.addActionListener(new AddButtonListener()); String name = listModel.getElementAt(list.getSelectedIndex()).toString(); nameField.setText(name); // Create a control panel, using the default FlowLayout. JPanel buttonPane = new JPanel(); buttonPane.add(nameField); buttonPane.add(addButton); buttonPane.add(deleteButton); buttonPane.add(upDownPanel); // Create the log for reporting list data events. log = new JTextArea(10, 20); JScrollPane logScrollPane = new JScrollPane(log); // Create a split pane for the log and the list. JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, listScrollPane, logScrollPane); splitPane.setResizeWeight(0.5); // Put everything together. add(buttonPane, BorderLayout.PAGE_START); add(splitPane, BorderLayout.CENTER); }
From source file:Main.java
public Main() { super(new BorderLayout()); //Create and populate the list model. listModel = new DefaultListModel(); listModel.addElement("Whistler, Canada"); listModel.addElement("Jackson Hole, Wyoming"); listModel.addElement("Squaw Valley, California"); listModel.addElement("Telluride, Colorado"); listModel.addElement("Taos, New Mexico"); listModel.addElement("Snowbird, Utah"); listModel.addElement("Chamonix, France"); listModel.addElement("Banff, Canada"); listModel.addElement("Arapahoe Basin, Colorado"); listModel.addElement("Kirkwood, California"); listModel.addElement("Sun Valley, Idaho"); listModel.addListDataListener(new MyListDataListener()); //Create the list and put it in a scroll pane. list = new JList(listModel); list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); list.setSelectedIndex(0);// w w w . j av a2 s . c o m list.addListSelectionListener(this); JScrollPane listScrollPane = new JScrollPane(list); //Create the list-modifying buttons. addButton = new JButton(addString); addButton.setActionCommand(addString); addButton.addActionListener(new AddButtonListener()); deleteButton = new JButton(deleteString); deleteButton.setActionCommand(deleteString); deleteButton.addActionListener(new DeleteButtonListener()); ImageIcon icon = createImageIcon("Up16"); if (icon != null) { upButton = new JButton(icon); upButton.setMargin(new Insets(0, 0, 0, 0)); } else { upButton = new JButton("Move up"); } upButton.setToolTipText("Move the currently selected list item higher."); upButton.setActionCommand(upString); upButton.addActionListener(new UpDownListener()); icon = createImageIcon("Down16"); if (icon != null) { downButton = new JButton(icon); downButton.setMargin(new Insets(0, 0, 0, 0)); } else { downButton = new JButton("Move down"); } downButton.setToolTipText("Move the currently selected list item lower."); downButton.setActionCommand(downString); downButton.addActionListener(new UpDownListener()); JPanel upDownPanel = new JPanel(new GridLayout(2, 1)); upDownPanel.add(upButton); upDownPanel.add(downButton); //Create the text field for entering new names. nameField = new JTextField(15); nameField.addActionListener(new AddButtonListener()); String name = listModel.getElementAt(list.getSelectedIndex()).toString(); nameField.setText(name); //Create a control panel, using the default FlowLayout. JPanel buttonPane = new JPanel(); buttonPane.add(nameField); buttonPane.add(addButton); buttonPane.add(deleteButton); buttonPane.add(upDownPanel); //Create the log for reporting list data events. log = new JTextArea(10, 20); JScrollPane logScrollPane = new JScrollPane(log); //Create a split pane for the log and the list. JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, listScrollPane, logScrollPane); splitPane.setResizeWeight(0.5); //Put everything together. add(buttonPane, BorderLayout.PAGE_START); add(splitPane, BorderLayout.CENTER); }