Java Code Examples for javax.swing.JList#setSelectedIndex()

The following examples show how to use javax.swing.JList#setSelectedIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FBAObjective.java    From iBioSim with Apache License 2.0 6 votes vote down vote up
private static void removeObjective(JList objectiveList) {
	// find where the selected objective is on the list
	int index = objectiveList.getSelectedIndex();
	if (index != -1) {
		objectiveList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		// remove it
		Utility.remove(objectiveList);
		objectiveList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		if (index < objectiveList.getModel().getSize()) {
			objectiveList.setSelectedIndex(index);
		}
		else {
			objectiveList.setSelectedIndex(index - 1);
		}
	}
}
 
Example 2
Source File: WebSocketUiHelper.java    From zap-extensions with Apache License 2.0 6 votes vote down vote up
public void setSelectedChannelIds(List<Integer> channelIds) {
    JList<WebSocketChannelDTO> channelsList = getChannelsList();
    if (channelIds == null || channelIds.contains(-1)) {
        channelsList.setSelectedIndex(0);
    } else {
        int[] selectedIndices = new int[channelIds.size()];
        ListModel<WebSocketChannelDTO> model = channelsList.getModel();
        for (int i = 0, j = 0; i < model.getSize(); i++) {
            WebSocketChannelDTO channel = model.getElementAt(i);
            if (channelIds.contains(channel.id)) {
                selectedIndices[j++] = i;
            }
        }
        channelsList.setSelectedIndices(selectedIndices);
    }
}
 
Example 3
Source File: WebSocketUiHelper.java    From zap-extensions with Apache License 2.0 6 votes vote down vote up
public void setSelectedOpcodes(List<String> opcodes) {
    JList<String> opcodesList = getOpcodeList();
    if (opcodes == null || opcodes.contains(SELECT_ALL_OPCODES)) {
        opcodesList.setSelectedIndex(0);
    } else {
        int j = 0;
        int[] selectedIndices = new int[opcodes.size()];
        ListModel<String> model = opcodesList.getModel();
        for (int i = 0; i < model.getSize(); i++) {
            String item = model.getElementAt(i);
            if (opcodes.contains(item)) {
                selectedIndices[j++] = i;
            }
        }
        opcodesList.setSelectedIndices(selectedIndices);
    }
}
 
Example 4
Source File: AddSourceToProjectDialog.java    From intellij with Apache License 2.0 6 votes vote down vote up
AddSourceToProjectDialog(Project project, List<TargetInfo> targets) {
  super(project, /* canBeParent= */ true, IdeModalityType.MODELESS);
  this.project = project;

  mainPanel = new JPanel(new VerticalLayout(12));

  JList<TargetInfo> targetsComponent = new JBList<>(targets);
  if (targets.size() == 1) {
    targetsComponent.setSelectedIndex(0);
  }
  this.targetsComponent = targetsComponent;

  setTitle("Add Source File to Project");
  setupUi();
  init();
}
 
Example 5
Source File: Validator.java    From dualsub with GNU General Public License v3.0 6 votes vote down vote up
public static void goDown(JList<File> list) {
	int[] selected = isSelected(list);
	if (selected.length > 1) {
		Alert.error(I18N.getHtmlText("Validator.onlyOne.alert"));
		return;
	} else if (selected.length == 1
			&& selected[0] < list.getModel().getSize() - 1) {
		File after = ((DefaultListModel<File>) list.getModel())
				.remove(selected[0] + 1);
		File removed = ((DefaultListModel<File>) list.getModel())
				.remove(selected[0]);
		((DefaultListModel<File>) list.getModel()).add(selected[0], after);
		((DefaultListModel<File>) list.getModel()).add(selected[0] + 1,
				removed);
		list.setSelectedIndex(selected[0] + 1);
	}
}
 
Example 6
Source File: Events.java    From iBioSim with Apache License 2.0 6 votes vote down vote up
/**
 * Remove an event from a list and SBML gcm.getSBMLDocument()
 * 
 * @param events
 *            a list of events
 * @param gcm.getSBMLDocument()
 *            an SBML gcm.getSBMLDocument() from which to remove the event
 * @param usedIDs
 *            a list of all IDs current in use
 * @param ev
 *            an array of all events
 */
private void removeEvent(JList events, BioModel gcm) {
	int index = events.getSelectedIndex();
	if (index != -1) {
		String selected = ((String) events.getSelectedValue()).split("\\[")[0];
		removeTheEvent(gcm, selected);
		events.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		Utility.remove(events);
		events.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		if (index < events.getModel().getSize()) {
			events.setSelectedIndex(index);
		}
		else {
			events.setSelectedIndex(index - 1);
		}
		modelEditor.setDirty(true);
		modelEditor.makeUndoPoint();
	}
}
 
Example 7
Source File: InstrumentBrowser.java    From jsyn with Apache License 2.0 6 votes vote down vote up
public InstrumentBrowser(InstrumentLibrary library) {
    this.library = library;
    JPanel horizontalPanel = new JPanel();
    horizontalPanel.setLayout(new GridLayout(1, 2));

    final JList<VoiceDescription> instrumentList = new JList<VoiceDescription>(library.getVoiceDescriptions());
    setupList(instrumentList);
    instrumentList.addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting() == false) {
                int n = instrumentList.getSelectedIndex();
                if (n >= 0) {
                    showPresetList(n);
                }
            }
        }
    });

    JScrollPane listScroller1 = new JScrollPane(instrumentList);
    listScroller1.setPreferredSize(new Dimension(250, 120));
    add(listScroller1);

    instrumentList.setSelectedIndex(0);
}
 
Example 8
Source File: InstrumentBrowser.java    From jsyn with Apache License 2.0 6 votes vote down vote up
public InstrumentBrowser(InstrumentLibrary library) {
    this.library = library;
    JPanel horizontalPanel = new JPanel();
    horizontalPanel.setLayout(new GridLayout(1, 2));

    final JList<VoiceDescription> instrumentList = new JList<VoiceDescription>(library.getVoiceDescriptions());
    setupList(instrumentList);
    instrumentList.addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting() == false) {
                int n = instrumentList.getSelectedIndex();
                if (n >= 0) {
                    showPresetList(n);
                }
            }
        }
    });

    JScrollPane listScroller1 = new JScrollPane(instrumentList);
    listScroller1.setPreferredSize(new Dimension(250, 120));
    add(listScroller1);

    instrumentList.setSelectedIndex(0);
}
 
Example 9
Source File: Validator.java    From dualsub with GNU General Public License v3.0 6 votes vote down vote up
public static void goUp(JList<File> list) {
	int[] selected = isSelected(list);
	if (selected.length > 1) {
		Alert.error(I18N.getHtmlText("Validator.onlyOne.alert"));
		return;
	} else if (selected.length == 1 && selected[0] > 0) {
		File removed = ((DefaultListModel<File>) list.getModel())
				.remove(selected[0]);
		File before = ((DefaultListModel<File>) list.getModel())
				.remove(selected[0] - 1);
		((DefaultListModel<File>) list.getModel()).add(selected[0] - 1,
				removed);
		((DefaultListModel<File>) list.getModel()).add(selected[0], before);
		list.setSelectedIndex(selected[0] - 1);
	}
}
 
Example 10
Source File: GroupWindow.java    From egdownloader with GNU General Public License v2.0 6 votes vote down vote up
public GroupWindow(List<File> groups, final EgDownloaderWindow mainWindow){
	super(Version.NAME + "任务组列表");
	this.mainWindow = mainWindow;
	this.setSize(300, 400);
	this.setResizable(false);
	this.setIconImage(IconManager.getIcon("group").getImage());
	this.setLocationRelativeTo(null);
	this.getContentPane().setLayout(null);
	this.setDefaultCloseOperation(mainWindow == null ? EXIT_ON_CLOSE : DISPOSE_ON_CLOSE);
	JLabel tipLabel = new AJLabel("双击选择任务组", new Color(67,44,1), 15, 15, 100, 30);
	JButton addGroupBtn = new AJButton("新建", IconManager.getIcon("add"), new OperaBtnMouseListener(this, MouseAction.CLICK, new IListenerTask() {
						public void doWork(Window window, MouseEvent e) {
							new AddGroupDialog((GroupWindow) window, mainWindow);
						}
					}) , 215, 15, 62, 30);
	addGroupBtn.setUI(AJButton.blueBtnUi);
	JList list = new GroupList(groups, this, mainWindow);
	list.setSelectedIndex(0);
	JScrollPane listPane = new JScrollPane(list);
	listPane.setBounds(new Rectangle(10, 50, 270, 300));
	listPane.setAutoscrolls(true);
	listPane.getViewport().setBackground(new Color(254,254,254));
	ComponentUtil.addComponents(this.getContentPane(), tipLabel, addGroupBtn, listPane);
	
	this.setVisible(true);
}
 
Example 11
Source File: StatusMessageDialog.java    From WorldGrower with GNU General Public License v3.0 5 votes vote down vote up
public StatusMessageDialog(List<StatusMessage> statusMessages, ImageInfoReader imageInfoReader, SoundIdReader soundIdReader, JFrame parentFrame) {
	super(700, 475, imageInfoReader);
	
	JScrollPane scrollPane = JScrollPaneFactory.createScrollPane();
	scrollPane.setBounds(16, 16, 665, 380);
	addComponent(scrollPane);
	
	JList<StatusMessage> list = JListFactory.createJList(statusMessages.toArray(new StatusMessage[0]));
	list.setSelectedIndex(statusMessages.size() - 1);
	list.setCellRenderer(new StatusMessageListRenderer());
	scrollPane.setViewportView(list);
	list.ensureIndexIsVisible(list.getSelectedIndex());
	
	JPanel buttonPane = new JPanel();
	buttonPane.setLayout(new BorderLayout());
	buttonPane.setOpaque(false);
	buttonPane.setBounds(16, 417, 665, 40);
	addComponent(buttonPane);

	JButton okButton = JButtonFactory.createButton(" OK ", imageInfoReader, soundIdReader);
	okButton.setActionCommand("OK");
	buttonPane.add(okButton, BorderLayout.EAST);
	getRootPane().setDefaultButton(okButton);

	addActions(list, okButton);
	DialogUtils.createDialogBackPanel(this, parentFrame.getContentPane());
}
 
Example 12
Source File: FrameConfig.java    From JAVA-MVC-Swing-Monopoly with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * ��ͼѡ�����
 * 
 */
private JPanel createMapSelectPanel() {
	JPanel jp = new JPanel();
	jp.setLayout(new GridLayout());
	jp.setBackground(new Color(235,236,237));
	JPanel lPane = new JPanel(new BorderLayout());
	String[] maps = { "\"LOVE��ͼ\"", "\"���ݵ�ͼ\"", "\"���˵�ͼ\"" };
	final ImageIcon[] maps1 = {
			new ImageIcon("images/other/1.png"),
			new ImageIcon("images/other/2.png"),
			new ImageIcon("images/other/3.png") };
	final JList jlst = new JList(maps);
	jlst.setSelectedIndex(0);
	final JLabel mapV = new JLabel(maps1[0]);
	final JButton ok = new JButton("ȷ��");
	ok.addActionListener(new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			GameRunning.MAP = jlst.getSelectedIndex() + 1;
			ok.setText("��ѡ");
		}
	});
	jlst.addListSelectionListener(new ListSelectionListener() {
		@Override
		public void valueChanged(ListSelectionEvent e) {
			mapV.setIcon(maps1[jlst.getSelectedIndex()]);
			ok.setText("ȷ��");
		}
	});
	lPane.add(jlst);
	lPane.add(ok, BorderLayout.SOUTH);
	JPanel rPane = new JPanel();
	rPane.add(mapV);
	JSplitPane jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
			false, lPane, rPane);
	jp.add(jSplitPane);
	return jp;
}
 
Example 13
Source File: ImageSetsPanel.java    From magarena with GNU General Public License v3.0 5 votes vote down vote up
ImageSetsPanel(final AvatarImagesScreen screen) {

        // List of avatar image sets.
        final JList<AvatarImageSet> imageSetsList = new JList<>(getAvatarImageSetsArray());
        imageSetsList.setOpaque(false);
        imageSetsList.addListSelectionListener(e -> {
            if (!e.getValueIsAdjusting()) {
                SwingUtilities.invokeLater(() -> {
                    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                    screen.displayImageSetIcons(imageSetsList.getSelectedValue());
                    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                });
            }
        });
        imageSetsList.setSelectedIndex(0);

        final AvatarListCellRenderer renderer = new AvatarListCellRenderer();
        imageSetsList.setCellRenderer(renderer);

        final JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewportView(imageSetsList);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);

        setLayout(new MigLayout("insets 0, gap 0, flowy"));
        setBorder(FontsAndBorders.BLACK_BORDER);
        add(scrollPane, "w 100%, h 100%");

        refreshStyle();
    }
 
Example 14
Source File: DownloadMapsWindow.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
private static JList<String> newGameSelectionList(
    final DownloadFileDescription selectedMap,
    final List<DownloadFileDescription> maps,
    final JEditorPane descriptionPanel,
    final DefaultListModel<String> model) {
  final JList<String> gamesList = new JList<>(model);
  final int selectedIndex = maps.indexOf(selectedMap);
  gamesList.setSelectedIndex(selectedIndex);

  final String text = maps.get(selectedIndex).toHtmlString();
  descriptionPanel.setText(text);
  descriptionPanel.scrollRectToVisible(new Rectangle(0, 0, 0, 0));
  return gamesList;
}
 
Example 15
Source File: TripleAFrame.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Prompts the user to select the territory on which they wish to conduct a rocket attack.
 *
 * @param candidates The collection of territories on which the user may conduct a rocket attack.
 * @param from The territory from which the rocket attack is conducted.
 * @return The selected territory or {@code null} if no territory was selected.
 */
public Territory getRocketAttack(final Collection<Territory> candidates, final Territory from) {
  messageAndDialogThreadPool.waitForAll();
  mapPanel.centerOn(from);

  final Supplier<Territory> action =
      () -> {
        final JList<Territory> list = new JList<>(SwingComponents.newListModel(candidates));
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setSelectedIndex(0);
        final JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        final JScrollPane scroll = new JScrollPane(list);
        panel.add(scroll, BorderLayout.CENTER);
        if (from != null) {
          panel.add(BorderLayout.NORTH, new JLabel("Targets for rocket in " + from.getName()));
        }
        final String[] options = {"OK", "Dont attack"};
        final String message = "Select Rocket Target";
        final int selection =
            JOptionPane.showOptionDialog(
                TripleAFrame.this,
                panel,
                message,
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.PLAIN_MESSAGE,
                null,
                options,
                null);
        return (selection == 0) ? list.getSelectedValue() : null;
      };
  return Interruptibles.awaitResult(() -> SwingAction.invokeAndWaitResult(action))
      .result
      .orElse(null);
}
 
Example 16
Source File: UseSpecificCatchCustomizer.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void addNewType(String t, JList list, String prefKey) {
    ((DefaultListModel)list.getModel()).addElement(t);
    list.setSelectedIndex(list.getModel().getSize() - 1);
    updatePreference(list, prefKey);
}
 
Example 17
Source File: Constraints.java    From iBioSim with Apache License 2.0 4 votes vote down vote up
public Constraints(BioModel bioModel, ModelEditor modelEditor) {
	super(new BorderLayout());
	this.bioModel = bioModel;
	this.modelEditor = modelEditor;
	Model model = bioModel.getSBMLDocument().getModel();
	addConstraint = new JButton("Add Constraint");
	removeConstraint = new JButton("Remove Constraint");
	editConstraint = new JButton("Edit Constraint");
	constraints = new JList();
	ListOf<Constraint> listOfConstraints = model.getListOfConstraints();
	String[] cons = new String[model.getConstraintCount()];
	for (int i = 0; i < model.getConstraintCount(); i++) {
		Constraint constraint = listOfConstraints.get(i);
		if (!constraint.isSetMetaId()) {
			String constraintId = "c0";
			int cn = 0;
			while (bioModel.isSIdInUse(constraintId)) {
				cn++;
				constraintId = "c" + cn;
			}
			SBMLutilities.setMetaId(constraint, constraintId);
		}
		cons[i] = constraint.getMetaId();
		cons[i] += SBMLutilities.getDimensionString(constraint);
	}
	JPanel addRem = new JPanel();
	addRem.add(addConstraint);
	addRem.add(removeConstraint);
	addRem.add(editConstraint);
	addConstraint.addActionListener(this);
	removeConstraint.addActionListener(this);
	editConstraint.addActionListener(this);
	JLabel panelLabel = new JLabel("List of Constraints:");
	JScrollPane scroll = new JScrollPane();
	scroll.setMinimumSize(new Dimension(260, 220));
	scroll.setPreferredSize(new Dimension(276, 152));
	scroll.setViewportView(constraints);
	edu.utah.ece.async.ibiosim.dataModels.biomodel.util.Utility.sort(cons);
	constraints.setListData(cons);
	constraints.setSelectedIndex(0);
	constraints.addMouseListener(this);
	this.add(panelLabel, "North");
	this.add(scroll, "Center");
	this.add(addRem, "South");
}
 
Example 18
Source File: LicenseWindow.java    From AMIDST with GNU General Public License v3.0 4 votes vote down vote up
public LicenseWindow() {
	super("Licenses");
	setIconImage(Amidst.icon);
	licenseText.setEditable(false);
	licenseText.setLineWrap(true);
	licenseText.setWrapStyleWord(true);
	
	licenses.add(new License("AMIDST",		     "licenses/amidst.txt"));
	licenses.add(new License("Args4j",		     "licenses/args4j.txt"));
	licenses.add(new License("Gson",			 "licenses/gson.txt"));
	licenses.add(new License("JGoogleAnalytics", "licenses/jgoogleanalytics.txt"));
	licenses.add(new License("JNBT",			 "licenses/jnbt.txt"));
	licenses.add(new License("Kryonet",          "licenses/kryonet.txt"));
	licenses.add(new License("MiG Layout",	     "licenses/miglayout.txt"));
	licenses.add(new License("Rhino",			 "licenses/rhino.txt"));
	licenseList = new JList(licenses.toArray());
	licenseList.setBorder(new LineBorder(Color.darkGray, 1));
	Container contentPane = this.getContentPane();
	MigLayout layout = new MigLayout();
	contentPane.setLayout(layout);
	contentPane.add(licenseList, "w 100!, h 0:2400:2400");
	JScrollPane scrollPane = new JScrollPane(licenseText);
	scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	contentPane.add(scrollPane, "w 0:4800:4800, h 0:2400:2400");
	setSize(870, 550);
	setVisible(true);
	licenseList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	licenseList.addListSelectionListener(new ListSelectionListener() {
		@Override
		public void valueChanged(ListSelectionEvent e) {
			License license = (License)licenseList.getSelectedValue();
			license.load();
			
			if (license.isLoaded()) {
				licenseText.setText(license.getContents());
				licenseText.setCaretPosition(0);
			}
		}
	});
	licenseList.setSelectedIndex(0);
}
 
Example 19
Source File: ComboBoxAutoCompleteSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void matchSelection( DocumentEvent e ) {
    if( isIgnoreSelectionEvents( combo ) )
        return;
    try {
        setIgnoreSelectionEvents( combo, true );
        if( !combo.isDisplayable() )
            return;
        String editorText;
        try {
            editorText = e.getDocument().getText( 0, e.getDocument().getLength() );
        } catch( BadLocationException ex ) {
            //ignore
            return;
        }

        if( null != combo.getSelectedItem() && combo.getSelectedItem().toString().equals(editorText) )
            return;

        if( !combo.isPopupVisible() ) {
            combo.showPopup();
        }

        JList list = getPopupList( combo );
        if( null == list )
            return;

        int matchIndex = findMatch( combo, editorText );

        if( matchIndex >= 0 ) {
            list.setSelectedIndex( matchIndex );
            Rectangle rect = list.getCellBounds(matchIndex, matchIndex);
            if( null != rect )
                list.scrollRectToVisible( rect );
        } else {
            list.clearSelection();
            list.scrollRectToVisible( new Rectangle( 1, 1 ) );
        }
    } finally {
        setIgnoreSelectionEvents( combo, false );
    }
}
 
Example 20
Source File: YogaCombinationsView.java    From Astrosoft with GNU General Public License v2.0 3 votes vote down vote up
public YogaCombinationsView(String title, YogaResults yogaResults, PlanetaryInfo planetaryInfo) {
	
	super(viewSize, viewLoc);
	this.planetaryInfo = planetaryInfo;
	this.yogaResults = yogaResults;
	
	JPanel yogaPanel = new JPanel();
	
	yogaList = new JList(yogaResults.getYogas().toArray());
	yogaList.setFont(UIUtil.getFont("Tahoma", Font.PLAIN, 11));
	
	yogaList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	yogaList.setSelectedIndex(0);
	
	yogaPanel.add(yogaList);
	
	yogaPanel.setPreferredSize(yogaSize);	
	
	
	final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, yogaPanel, createResultPane());
	
	yogaPanel.setBorder(BorderFactory.createEtchedBorder());
	splitPane.setBorder(BorderFactory.createEmptyBorder());
	
	yogaList.addListSelectionListener(new ListSelectionListener(){

		public void valueChanged(ListSelectionEvent e) {
			//splitPane.remove(chartPanel);
			yogaChanged((YogaResults.Result)yogaList.getSelectedValue());
			//splitPane.add(chartPanel);
		}
	});
	
	add(splitPane,BorderLayout.CENTER);
}