Java Code Examples for javax.swing.JList

The following examples show how to use javax.swing.JList. These examples are extracted from open source projects. 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 Project: whyline   Source File: LineTablesUI.java    License: MIT License 6 votes vote down vote up
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
	 
	 Line line = (Line)value;
	 
	 String label = "<html><b>" + Util.fillOrTruncateString(line.getFile().getShortFileName(), 15) + ": " + Util.fillOrTruncateString(Integer.toString(line.getLineNumber().getNumber()), 10) + "</b>" + line.getLineText();
	 label = label.replace(" ", "&nbsp;");
     setText(label);
              
     if (isSelected) {
    	 setBackground(UI.getHighlightColor());
    	 setForeground(java.awt.Color.white);
     }
     else {
    	 setBackground(list.getBackground());
    	 setForeground(list.getForeground());
     }
     setEnabled(list.isEnabled());
     setFont(list.getFont());
     setOpaque(true);
     return this;
}
 
Example 2
Source Project: wpcleaner   Source File: AbstractPageListPopupListener.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Show popup menu in response to a mouse event.
 * 
 * @param e Event.
 */
@Override
protected void showPopup(MouseEvent e) {

  // Retrieve information
  if (!(e.getComponent() instanceof JList)) {
    return;
  }
  JList tmpList = (JList) e.getComponent();
  int position = tmpList.locationToIndex(e.getPoint());
  if (position < 0) {
    return;
  }
  Object object = tmpList.getModel().getElementAt(position);
  if (!(object instanceof Page)) {
    return;
  }
  Page link = (Page) object;
  showPopup(tmpList, link, e.getX(), e.getY());
}
 
Example 3
Source Project: ermaster-b   Source File: RowHeaderTable.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Component getListCellRendererComponent(JList list,
		Object value, int index, boolean isSelected,
		boolean hasFocus) {
	JLabel label = (JLabel) super.getListCellRendererComponent(
			list, value, index, isSelected, hasFocus);

	if (this.iconEnable && index == 0) {
		label.setIcon(ICON);

	} else if (index == 1) {
		Font font = label.getFont().deriveFont(10.0f);
		label.setFont(font);
		label.setForeground(Color.GRAY);
	}

	return label;
}
 
Example 4
Source Project: marathonv5   Source File: RListTest.java    License: Apache License 2.0 6 votes vote down vote up
public void selectMultipleItemSelection() {
    final LoggingRecorder lr = new LoggingRecorder();
    siw(new Runnable() {
        @Override
        public void run() {
            list = (JList) ComponentUtils.findComponent(JList.class, frame);
            list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            list.setSelectedIndices(new int[] { 0, 2 });
            RList rList = new RList(list, null, null, lr);
            rList.focusLost(null);
        }
    });
    Call call = lr.getCall();
    AssertJUnit.assertEquals("select", call.getFunction());
    AssertJUnit.assertEquals("[Jane Doe, Kathy Green]", call.getState());
}
 
Example 5
@Override
public Component getListCellRendererComponent(
    final JList<? extends Fleet> list, final Fleet value, final int index,
    final boolean isSelected, final boolean cellHasFocus) {
  JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(
      list, value, index, isSelected, cellHasFocus);
  renderer.setFont(GuiStatics.getFontCubellan());
  if (value.getNumberOfShip() == 1) {
    renderer
        .setText(value.getName() + " - " + value.getFirstShip().getName());
  } else {
    renderer.setText(
        value.getName() + " - " + value.getNumberOfShip() + " ships");
  }
  if (isSelected) {
    renderer.setForeground(GuiStatics.COLOR_COOL_SPACE_BLUE);
    renderer.setBackground(GuiStatics.COLOR_DEEP_SPACE_PURPLE);
  } else {
    renderer.setForeground(GuiStatics.COLOR_COOL_SPACE_BLUE_DARK);
    renderer.setBackground(GuiStatics.COLOR_DEEP_SPACE_PURPLE_DARK);
  }
  return renderer;
}
 
Example 6
private Boolean reorderProjects(TransferHandler.TransferSupport support) {
    JList list = (JList) support.getComponent();
    try {
        int[] selectedIndices = (int[]) support.getTransferable().getTransferData(INDICES);
        DefaultListModel model = (DefaultListModel) list.getModel();
        JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
        if (dl.getIndex() != -1) {
            for (int selectedIndex : selectedIndices) {
                Object value = model.get(selectedIndex);
                model.removeElement(value);
                model.add(dl.getIndex(), value);
            }
            return true;
        } else {
            LOG.warning("Invalid Drop Location");
        }
    } catch (UnsupportedFlavorException | IOException ex) {
        LOG.log(Level.SEVERE, ex.getMessage(), ex);
    }
    return false;
}
 
Example 7
Source Project: Spark   Source File: ListIconRenderer.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Component getListCellRendererComponent(JList list,
                                                 Object value,
                                                 int index,
                                                 boolean isSelected,
                                                 boolean cellHasFocus) {
       if (isSelected) {
           setBackground(list.getSelectionBackground());
           setForeground(list.getSelectionForeground());
       }
       else {
           setBackground(list.getBackground());
           setForeground(list.getForeground());
       }

       setHorizontalAlignment(SwingConstants.LEFT);
       ImageIcon icon = (ImageIcon)value;
       setText(icon.getDescription());
       setIcon(icon);
       return this;
   }
 
Example 8
Source Project: netbeans   Source File: SuitePropertiesTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testRemoveAllSubModules() throws Exception {
    SuiteProject suite1 = generateSuite("suite1");
    TestBase.generateSuiteComponent(suite1, "module1a");
    TestBase.generateSuiteComponent(suite1, "module1b");
    SuiteProperties suite1Props = getSuiteProperties(suite1);
    
    SuiteSubModulesListModel model = suite1Props.getModulesListModel();
    assertNotNull(model);
    
    // simulate removing all items from the list
    JList moduleList = new JList(model);
    moduleList.setSelectedIndices(new int[] {0, model.getSize() - 1});
    model.removeModules(Arrays.asList(moduleList.getSelectedValues()));
    assertEquals("no subModule should be left", 0, model.getSize());
    
    saveProperties(suite1Props);
    
    SubprojectProvider spp = getSubProjectProvider(suite1);
    assertEquals("no module should be left", 0, spp.getSubprojects().size());
}
 
Example 9
Source Project: Spark   Source File: JContactItemRenderer.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, 
		boolean isSelected, boolean cellHasFocus) {
	
	basicPanelRenderer.getListCellRendererComponent(list, this, index, isSelected, cellHasFocus);
	ContactItem renderItem = (ContactItem)value;
	setFocusable(false);
	setNickname(renderItem.getNickname());
	setAlias(renderItem.getAlias());
               if (this.getDisplayName().trim().isEmpty()) {
                   // Fallback hack to show something other than empty string.
                   // JID can't be set after object creation, so alias is reset.
                   setAlias(renderItem.getDisplayName());
               }
	setIcon(renderItem.getIcon());
	setStatus(renderItem.getStatus());
	getNicknameLabel().setFont(renderItem.getNicknameLabel().getFont());
	getNicknameLabel().setForeground(renderItem.getNicknameLabel().getForeground());
	getDescriptionLabel().setFont(renderItem.getDescriptionLabel().getFont());
	getDescriptionLabel().setText(renderItem.getDescriptionLabel().getText());
	getSpecialImageLabel().setIcon(renderItem.getSpecialImageLabel().getIcon());
	getSideIcon().setIcon(renderItem.getSideIcon().getIcon());
	return this;
}
 
Example 10
Source Project: netbeans   Source File: IsOverriddenPopup.java    License: Apache License 2.0 6 votes vote down vote up
public Component getListCellRendererComponent(
        JList list,
        Object value,
        int index,
        boolean isSelected,
        boolean cellHasFocus) {
    Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    
    if (value instanceof OverrideDescription) {
        OverrideDescription desc = (OverrideDescription) value;
        
        setIcon(desc.getIcon());
        setText(desc.location.getDisplayHtml(new GsfHtmlFormatter()));
    }
    
    return c;
}
 
Example 11
Source Project: netbeans   Source File: ProjectCellRenderer.java    License: Apache License 2.0 6 votes vote down vote up
@Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    // #89393: GTK needs name to render cell renderer "natively"
    setName("ComboBox.listRenderer"); // NOI18N
    if (value instanceof Project) {
        ProjectInformation pi = ProjectUtils.getInformation((Project) value);
        setText(pi.getDisplayName());
        setIcon(pi.getIcon());
    } else {
        setText(value == null ? "" : value.toString()); // NOI18N
        setIcon(null);
    }
    if (isSelected) {
        setBackground(list.getSelectionBackground());
        setForeground(list.getSelectionForeground());
    } else {
        setBackground(list.getBackground());
        setForeground(list.getForeground());
    }
    return this;
}
 
Example 12
@Override
public Component getListCellRendererComponent(final JList list, Object value, final int index,
        final boolean isSelected, boolean cellHasFocus) {
    Color back = (index % 2 == 1) ? list.getBackground() : evensColor;
    if (value instanceof Method) {
        final Method method = (Method) value;
        return new MethodCell(list, isSelected, back, method, dlg.getTheClass());
    } else if (value instanceof Field) {
        Field field = (Field) value;
        return new FieldCell(list, isSelected, back, field, dlg.getTheClass());
    } else if (value instanceof Constructor) {
        Constructor cons = (Constructor) value;
        return new ConstructorCell(list, isSelected, back, cons, dlg.getTheClass());
    } else {
        Component comp = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        comp.setBackground(back);
        return comp;
    }
}
 
Example 13
Source Project: openjdk-jdk9   Source File: CheckRenderer.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public Component getListCellRendererComponent(final JList<? extends Object> list, Object value, final int index, boolean isSelected, boolean cellHasFocus) {
    setText(value.toString());
    CheckNode node = ((CheckNodeListModel) list.getModel()).getCheckNodeAt(index);
    this.setSelected(node.isSelected());
    this.setEnabled(list.isEnabled());

    if (isSelected && list.hasFocus()) {
        this.setBackground(list.getSelectionBackground());
        this.setForeground(list.getSelectionForeground());
    } else if (isSelected) {
        assert !list.hasFocus();
        this.setBackground(startBackground);
        this.setForeground(list.getForeground());

    } else {
        this.setBackground(list.getBackground());
        this.setForeground(list.getForeground());
    }
    return this;
}
 
Example 14
Source Project: xdm   Source File: QueuedItemsRenderer.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public Component getListCellRendererComponent(JList<? extends String> list, String value, int index,
		boolean isSelected, boolean cellHasFocus) {
	if (isSelected) {
		setBackground(ColorResource.getSelectionColor());
	} else {
		setBackground(ColorResource.getDarkerBgColor());
	}
	DownloadEntry ent = XDMApp.getInstance().getEntry(value);
	String str = "";
	if (ent != null) {
		str += ent.getFile();
		if (ent.getSize() > 0) {
			str += " [ " + FormatUtilities.formatSize(ent.getSize()) + " ]";
		}
	}
	setText(str);
	return this;
}
 
Example 15
public UpdateUIRecursionTest() {
    super("UpdateUIRecursionTest");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 400);

    String[] listData = {
        "First", "Second", "Third", "Fourth", "Fifth", "Sixth"
    };

    list = new JList(listData);
    list.setCellRenderer(this);
    renderer = new DefaultListCellRenderer();
    getContentPane().add(new JScrollPane(list), BorderLayout.CENTER);

    setVisible(true);
}
 
Example 16
Source Project: netbeans   Source File: FileCompletionPopup.java    License: Apache License 2.0 6 votes vote down vote up
public FileCompletionPopup(JFileChooser chooser, JTextField textField, Vector<File> files) {
    this.list = new JList(files);
    this.textField = textField;
    this.chooser = chooser;
    list.setVisibleRowCount(4);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    
    JScrollPane jsp = new JScrollPane(list);
    add(jsp);
    
    list.setFocusable(false);
    jsp.setFocusable(false);
    setFocusable(false);
    
    list.addFocusListener(new FocusHandler());
    list.addMouseListener(new MouseHandler());
    list.addMouseMotionListener(new MouseHandler());
    
    textField.addKeyListener(this);
}
 
Example 17
@Override
public Component getListCellRendererComponent(JList list, Object value, int index,
        boolean isSelected, boolean cellHasFocus) {
    if (value instanceof CheckBoxListEntry) {
        CheckBoxListEntry checkbox = (CheckBoxListEntry) value;
        checkbox.setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
        if (checkbox.isRed()) {
            checkbox.setForeground(Color.red);
        } else {
            checkbox.setForeground(isSelected ? list.getSelectionForeground() : list.getForeground());
        }
        checkbox.setEnabled(isEnabled());
        checkbox.setFont(getFont());
        checkbox.setFocusPainted(false);
        checkbox.setBorderPainted(true);
        checkbox.setBorder(isSelected ? UIManager.getBorder("List.focusCellHighlightBorder")
                : noFocusBorder);

        return checkbox;
    } else {
        return super.getListCellRendererComponent(list, value.getClass().getName(), index,
                isSelected, cellHasFocus);
    }
}
 
Example 18
Source Project: freecol   Source File: UnitType.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public int getMaximumIndex(Colony colony, JList<BuildableType> buildQueueList, int UNABLE_TO_BUILD) {
    ListModel<BuildableType> buildQueue = buildQueueList.getModel();
    final int buildQueueLastPos = buildQueue.getSize();

    boolean canBuild = false;
    if (colony.canBuild(this)) {
        canBuild = true;
    }

    // does not depend on anything, nothing depends on it
    // can be built at any time
    if (canBuild) return buildQueueLastPos;
    // check for building in queue that allows builting this unit
    for (int index = 0; index < buildQueue.getSize(); index++) {
        BuildableType toBuild = buildQueue.getElementAt(index);
        if (toBuild == this) continue;
        if (toBuild.hasAbility(Ability.BUILD, this)) {
            return buildQueueLastPos;
        }
    }
    return UNABLE_TO_BUILD;
}
 
Example 19
Source Project: netbeans   Source File: ToDoCustomizer.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    Component comp = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    if (comp instanceof JLabel) {
        JLabel jLabel = (JLabel) comp;
        if (value instanceof FileIdentifier) {
            FileIdentifier identifier = (FileIdentifier) value;
            if (isCommentTagValid(identifier)) {
                jLabel.setIcon(EMPTY_ICON);
            } else {
                jLabel.setIcon(NOT_VALID_ICON);
            }
        } else if (!isSelected) {
            ((JLabel) comp).setForeground(UIManager.getColor("Label.disabledForeground"));
        }
    }
    return comp;
}
 
Example 20
Source Project: pumpernickel   Source File: RepaintingDecoration.java    License: MIT License 6 votes vote down vote up
/**
 * Returns whether this decoration should be visible.
 * <p>
 * Do not override this method. To customize the visibility of this object,
 * change the <code>ListDecoration</code> this decoration delegates to.
 */
@Override
public final boolean isVisible(JList list, Object value, int row,
		boolean isSelected, boolean cellHasFocus) {
	boolean returnValue = decoration.isVisible(list, value, row,
			isSelected, cellHasFocus);
	synchronized (repaintingCells) {
		CellInfo cellInfo = new CellInfo(list, row);
		if (returnValue) {
			if (repaintingCells.add(cellInfo)
					&& (!repaintTimer.isRunning())) {
				repaintTimer.start();
			}
		} else {
			repaintingCells.remove(cellInfo);
		}
	}
	return returnValue;
}
 
Example 21
Source Project: freecol   Source File: TradeRouteInputPanel.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean importData(JComponent target, Transferable data) {
    JList<TradeRouteStop> stl = TradeRouteInputPanel.this.stopList;
    if (target == stl
        && data instanceof StopListTransferable
        && canImport(target, data.getTransferDataFlavors())) {
        List<TradeRouteStop> stops
            = ((StopListTransferable)data).getStops();
        DefaultListModel<TradeRouteStop> model
            = new DefaultListModel<>();
        int index = stl.getMaxSelectionIndex();
        for (TradeRouteStop stop : stops) {
            if (index < 0) {
                model.addElement(stop);
            } else {
                index++;
                model.add(index, stop);
            }
        }
        stl.setModel(model);
        return true;
    }
    return false;
}
 
Example 22
Source Project: jeddict   Source File: TableUISupport.java    License: Apache License 2.0 5 votes vote down vote up
public static Set<Table> getEnabledTables(JList list) {
    Set<Table> result = new HashSet<>();

    for (int i = 0; i < list.getModel().getSize(); i++) {
        Table table = (Table) list.getModel().getElementAt(i);
        if (!list.getCellRenderer().getListCellRendererComponent(list, table, i, false, false).isEnabled()) {
            continue;
        }
        result.add(table);
    }

    return result;
}
 
Example 23
Source Project: netbeans   Source File: RepositoryUserRenderer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    if (value instanceof RepositoryUser) {
        RepositoryUser user = (RepositoryUser)value;
        value = MessageFormat.format(pattern, user.getFullName(), user.getUserName());
    }
    return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
}
 
Example 24
Source Project: chipster   Source File: ToolFilterPanel.java    License: MIT License 5 votes vote down vote up
public ToolFilterPanel(ToolPanel parent,
       List<ToolCategory> categories) {
    super(new GridLayout(1, 2));
    this.toolPanel = parent;
    this.categories = categories;
    
    categoryList = new JList();
    categoryList.setSelectedIndex(0);
    categoryList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    categoryList.addListSelectionListener(this);
    categoryList.setCellRenderer(new ToolSelectorPanel.CategoryListRenderer());
    categoryList.getInsets().right = 1;
    categoryList.setName("categoryList");
    
    // Operation list shows the results
    visibleOperationsList = new JList();
    visibleOperationsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    visibleOperationsList.addListSelectionListener(this);
    visibleOperationsList.setCellRenderer(new ToolSelectorPanel.FontSizeFriendlyListRenderer());
    visibleOperationsList.getInsets().right = 1;
    visibleOperationsList.setName("operationList");
    
    JScrollPane categoryListScroller = new JScrollPane(categoryList);       
    JScrollPane operationListScroller = new JScrollPane(visibleOperationsList);
    categoryListScroller.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1,
            VisualConstants.TOOL_LIST_BORDER_COLOR));
    operationListScroller.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));

    this.add(categoryListScroller);
    this.add(operationListScroller);        
}
 
Example 25
Source Project: netbeans   Source File: MainClassChooser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Component getListCellRendererComponent (JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    String displayName;
    if (value instanceof ElementHandle) {
        displayName = ((ElementHandle)value).getQualifiedName();
    } else {
        displayName = value.toString ();
    }
    return super.getListCellRendererComponent (list, displayName, index, isSelected, cellHasFocus);
}
 
Example 26
Source Project: openjdk-jdk9   Source File: GUIBrowser.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Component getListCellRendererComponent(JList<? extends ClassNode> list,
        ClassNode value,
        int index,
        boolean isSelected,
        boolean cellHasFocus) {
    return get(value, isSelected);
}
 
Example 27
Source Project: marathonv5   Source File: RList.java    License: Apache License 2.0 5 votes vote down vote up
public RList(Component source, JSONOMapConfig omapConfig, Point point, IJSONRecorder recorder) {
    super(source, omapConfig, point, recorder);
    JList list = (JList) source;
    this.point = point;
    index = JListJavaElement.getIndexAt(list, point);
    cellInfo = index != -1 ? getText(list, index) : null;
}
 
Example 28
Source Project: netbeans   Source File: RunAnalysisPanel.java    License: Apache License 2.0 5 votes vote down vote up
@Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    if (value instanceof AnalyzerAndWarning) {
        value = "    " + SPIAccessor.ACCESSOR.getWarningDisplayName(((AnalyzerAndWarning) value).wd);
    } else if (value instanceof String) {
        setFont(getFont().deriveFont(Font.ITALIC));
        setText((String) value);
        setEnabled(false);
        setBackground(list.getBackground());
        setForeground(UIManager.getColor("Label.disabledForeground"));

        return this;
    }

    return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
}
 
Example 29
Source Project: pumpernickel   Source File: IOLocationTileList.java    License: MIT License 5 votes vote down vote up
private void initialize() {
	JLabel label = new JLabel();
	label.setUI(new ThumbnailLabelUI());
	setCellRenderer(new BasicTileCellRenderer(new GraphicCache(), label));

	setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
	setFixedCellWidth(100);
	setFixedCellHeight(100);
	setVisibleRowCount(-1);
	setLayoutOrientation(JList.HORIZONTAL_WRAP);
	addKeyListener(commitKeyListener);
	addKeyListener(typingListener);
	addMouseListener(commitMouseListener);
}
 
Example 30
Source Project: netbeans   Source File: ListValuePicker.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    if(value instanceof ListValue) {
        ListValue lv = (ListValue) value;
        return super.getListCellRendererComponent(list, lv.displayValue, index, isSelected, cellHasFocus);
    }
    return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
}