下面列出了javax.swing.JList#setModel ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void loadListData(JList list, String optionID, Preferences node) {
DefaultListModel model = new DefaultListModel();
String value = node.get(optionID, getDefaultAsString(optionID));
for (String s : value.trim().split("\\s*[,;]\\s*")) { //NOI18N
if (classMembersOrder.equals(optionID)) {
Element e = new Element();
if (s.startsWith("STATIC ")) { //NOI18N
e.isStatic = true;
s = s.substring(7);
}
e.kind = ElementKind.valueOf(s);
model.addElement(e);
} else {
Visibility v = new Visibility();
v.kind = s;
model.addElement(v);
}
}
list.setModel(model);
}
public ListParameter(JList list, String parameter, String encoding) {
super(parameter, encoding);
this.list = list;
list.setModel(new DefaultListModel());
list.addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent e) {
int[] s = ListParameter.this.list.getSelectedIndices();
if(e.getValueIsAdjusting()) {
return;
}
fireStateChanged();
};
});
original = list.getSelectedIndices();
fireStateChanged();
}
/**
* {@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;
}
public ListPanel(FilterModel fltrmodel, Function<T, String> mapper) {
setLayout(new java.awt.BorderLayout());
JScrollPane sp = new javax.swing.JScrollPane();
list = new JList();
list.setModel(fltrmodel);
list.setCellRenderer(new CheckBoxListRenderer(mapper));
sp.setViewportView(list);
add(sp, BorderLayout.CENTER);
list.setSelectionModel(new MultiSelectionModel(this::onSelect));
list.addKeyListener(onDelete());
list.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ctrl A"), "SelectAll");
list.getActionMap().put("SelectAll", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
list.setSelectionInterval(0, list.getModel().getSize() - 1);
}
});
}
/**
* 初始化数据库表列表的数据模型
*/
@SuppressWarnings("unchecked")
public void initListModel()
{
JList<String> v_Tables = (JList<String>)XJava.getObject("xlTables");
this.listModel = new DefaultListModel<String>();
v_Tables.setModel(this.listModel);
v_Tables.setBackground(this.getBackground());
}
private static JComponent getInner3() {
DefaultListModel<String> model = new DefaultListModel<String>();
model.add(0, "Steve Jobs");
JList<String> list = new JList<String>();
list.setModel(model);
return list;
}
public static void connectSelected(JList selectedTablesList, TableClosure tableClosure) {
selectedTablesList.setModel(new SelectedTablesModel(tableClosure));
if (!(selectedTablesList.getCellRenderer() instanceof SelectedTableRenderer)) {
selectedTablesList.setCellRenderer(new SelectedTableRenderer());
}
}
private static JList<String> getInner4() {
DefaultListModel<String> model = new DefaultListModel<String>();
model.add(0, "Bill Gates");
model.add(1, "Steven Spielberg");
model.add(2, "Donald Trump");
model.add(3, "Steve Jobs");
JList<String> list = new JList<String>();
list.setModel(model);
return list;
}
private void initExcluderList(JList jList, String list) {
DefaultListModel model = new DefaultListModel();
String [] entries = list.split(","); //NOI18N
for (String entry : entries){
if (entry.length() != 0)
model.addElement(entry);
}
jList.setModel(model);
}
private static JComponent getInner1() {
DefaultListModel<String> model = new DefaultListModel<String>();
model.add(0, "Bill Gates");
model.add(1, "Steven Spielberg");
JList<String> list = new JList<String>();
list.setModel(model);
return list;
}
/** Creates a new instance of PathController */
public PathController(JList l, JLabel label, DefaultListModel model, JButton add, JFileChooser chooser, JButton remove, JButton up, JButton down, ListDataListener lstnr) {
this.l = l;
this.label = label;
this.model = model;
this.add = add;
this.remove = remove;
this.up = up;
this.down = down;
this.chooser = chooser;
this.lstnr = lstnr;
l.setModel(model);
if (model != null) {
model.addListDataListener(this);
}
add.setActionCommand("add");// NOI18N
remove.setActionCommand("remove");// NOI18N
up.setActionCommand("up");// NOI18N
down.setActionCommand("down");// NOI18N
add.addActionListener(this);
remove.addActionListener(this);
up.addActionListener(this);
down.addActionListener(this);
l.addListSelectionListener(this);
remove.setEnabled(false);
up.setEnabled(false);
down.setEnabled(false);
}
/**
* Creates a new instance of JCarouselMenu
* @param border The border to use to draw items in the menu
*/
public JCarouselMenu(ImageBorder border) {
carousel = new JCarosel();
carousel.setLayout(new OffsetCaroselLayout(carousel));
carousel.setBackground(null);
carousel.setOpaque(false);
carousel.setContentWidth(256);
super.setLayout(new GridLayout(1,2));
super.add(carousel);
upButton.setForeground(Color.WHITE);
downButton.setForeground(Color.WHITE);
JPanel menuPanel = new JPanel();
menuPanel.setBackground(null);
menuPanel.setOpaque(false);
menuPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
menu = new JList();
menuScroll = new JScrollPane(menu, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
menuScroll.getViewport().setOpaque(false);
menuScroll.setBorder(null);
menuScroll.getViewport().addChangeListener(this);
menu.setModel(menuModel);
menu.setCellRenderer(new CarouselListCellRenderer(border));
menu.setBackground(null);
menu.setOpaque(false);
menu.addListSelectionListener(this);
menuScroll.setOpaque(true);
menuScroll.setBackground(Color.BLACK);
menuScroll.setBorder(BorderFactory.createEmptyBorder());
gbc.weightx=0.0;
gbc.weighty=0.0;
gbc.gridy=0;
gbc.fill=GridBagConstraints.HORIZONTAL;
menuPanel.add(upButton,gbc);
gbc.weighty=1.0;
gbc.weightx=1.0;
gbc.gridy++;
gbc.fill=GridBagConstraints.BOTH;
menuPanel.add(menuScroll,gbc);
gbc.weighty=0.0;
gbc.weightx=0.0;
gbc.gridy++;
gbc.fill=GridBagConstraints.HORIZONTAL;
menuPanel.add(downButton,gbc);
menu.addMouseListener(this);
menu.addKeyListener(this);
//Don't want it to listen to itself...
carousel.removeMouseWheelListener(carousel);
carousel.addMouseWheelListener(this);
menu.addMouseWheelListener(this);
menuScroll.addMouseWheelListener(this);
menuPanel.addMouseWheelListener(this);
super.add(menuPanel);
}
private RouterClientTest() {
setResizable(false);
setTitle("jRUDP Client Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(289, 500);
setLocationRelativeTo(null);
getContentPane().setLayout(null);
try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {}
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 69, 263, 156);
getContentPane().add(scrollPane);
lblRecPacketQueue = new JLabel("Received Packet Queue (Front==index#0)");
scrollPane.setColumnHeaderView(lblRecPacketQueue);
JList<String> listPacketQueue = new JList<>();
listPacketQueue.setEnabled(false);
listPacketQueue.setModel(modelRecPackets);
scrollPane.setViewportView(listPacketQueue);
btnConnection = new JButton("Connect");
btnConnection.addActionListener((action)->{
if(clientInstance != null && clientInstance.isConnected()) {
disconnectWGui();
}
else {
connectWGui();
}
});
btnConnection.setBounds(10, 438, 263, 23);
getContentPane().add(btnConnection);
tfServerPort = new JTextField();
tfServerPort.setText(ST_SERVER_PORT + "");
tfServerPort.setBounds(96, 407, 177, 20);
tfServerPort.setColumns(10);
getContentPane().add(tfServerPort);
tfServerHost = new JTextField();
tfServerHost.setText(ST_SERVER_HOST);
tfServerHost.setColumns(10);
tfServerHost.setBounds(96, 376, 177, 20);
getContentPane().add(tfServerHost);
JLabel lblServerHost = new JLabel("Server Host:");
lblServerHost.setBounds(23, 379, 71, 14);
getContentPane().add(lblServerHost);
JLabel lblServerPort = new JLabel("Server Port:");
lblServerPort.setBounds(23, 410, 71, 14);
getContentPane().add(lblServerPort);
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane_1.setBounds(10, 236, 263, 126);
getContentPane().add(scrollPane_1);
taConsole = new JTextArea();
taConsole.setLineWrap(true);
taConsole.setWrapStyleWord(true);
taConsole.setEditable(false);
taConsole.setBackground(Color.LIGHT_GRAY);
taConsole.setFont(new Font("SansSerif", Font.BOLD, 11));
scrollPane_1.setViewportView(taConsole);
taHandledPacket = new JTextArea();
taHandledPacket.setEditable(false);
taHandledPacket.setEnabled(false);
taHandledPacket.setFont(new Font("SansSerif", Font.BOLD, 11));
taHandledPacket.setText("Last Handled Packet:\r\nnull");
taHandledPacket.setBounds(10, 11, 263, 47);
getContentPane().add(taHandledPacket);
setVisible(true);
System.setOut(new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
taConsole.append("" + (char)b);
taConsole.setSize(taConsole.getPreferredSize());
JScrollBar sb = scrollPane_1.getVerticalScrollBar();
sb.setValue( sb.getMaximum() );
}
}));
System.out.println("[INFO]Console: on");
setVisible(true);
}
private void updateList(JList<IsochronModel> jlist, AbstractListModel<IsochronModel> listModel) {
jlist.setModel(new IsochronModelList());
jlist.setModel(listModel);
jlist.validate();
}
public static void disconnect(JList list) {
list.setModel(new DefaultListModel());
}
protected void setupInstance(JList instance) {
super.setupInstance(instance);
ListModel model = dataModel == null ? null : dataModel.createInstance();
if (model != null) instance.setModel(model);
}
static List<BuildExecutionSupport.Item> selectProcessToKill(List<BuildExecutionSupport.Item> toStop) {
// Add all threads, sorted by display name.
DefaultListModel model = new DefaultListModel();
StopBuildingAlert alert = new StopBuildingAlert();
final JList list = alert.buildsList;
Comparator<BuildExecutionSupport.Item> comp = new Comparator<BuildExecutionSupport.Item>() {
private final Collator coll = Collator.getInstance();
@Override
public int compare(BuildExecutionSupport.Item t1, BuildExecutionSupport.Item t2) {
String n1 = t1.getDisplayName();
String n2 = t2.getDisplayName();
int r = coll.compare(n1, n2);
if (r != 0) {
return r;
} else {
// Arbitrary. XXX Note that there is no way to predict which is
// which if you have more than one build running. Ideally it
// would be subsorted by creation time, probably.
return System.identityHashCode(t1) - System.identityHashCode(t2);
}
}
};
SortedSet<BuildExecutionSupport.Item> items = new TreeSet<BuildExecutionSupport.Item>(comp);
items.addAll(toStop);
for (BuildExecutionSupport.Item t : items) {
model.addElement(t);
}
list.setModel(model);
list.setSelectedIndex(0);
// Make a dialog with buttons "Stop Building" and "Cancel".
DialogDescriptor dd = new DialogDescriptor(alert, NbBundle.getMessage(StopBuildingAlert.class, "TITLE_SBA"));
dd.setMessageType(NotifyDescriptor.PLAIN_MESSAGE);
final JButton stopButton = new JButton(NbBundle.getMessage(StopBuildingAlert.class, "LBL_SBA_stop"));
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
stopButton.setEnabled(list.getSelectedValue() != null);
}
});
dd.setOptions(new Object[] {stopButton, DialogDescriptor.CANCEL_OPTION});
DialogDisplayer.getDefault().createDialog(dd).setVisible(true);
List<BuildExecutionSupport.Item> toRet = new ArrayList<BuildExecutionSupport.Item>();
if (dd.getValue() == stopButton) {
Object[] selectedItems = list.getSelectedValues();
for (Object o : selectedItems) {
toRet.add((BuildExecutionSupport.Item)o);
}
}
return toRet;
}
/**
* ListDemo Constructor.
*
* @param swingset the swingset
*/
public ListDemo(SwingSet2 swingset) {
super(swingset, "ListDemo"
, "toolbar/JList.gif");
loadImages();
//modified by jb2011
JLabel description = N9ComponentFactory.createLabel_style2(getString("ListDemo.description"));
getDemoPanel().add(description, BorderLayout.NORTH);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.X_AXIS));
centerPanel.add(Box.createRigidArea(HGAP10));
getDemoPanel().add(centerPanel, BorderLayout.CENTER);
JPanel listPanel = new JPanel();
listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.Y_AXIS));
listPanel.add(Box.createRigidArea(VGAP10));
centerPanel.add(listPanel);
centerPanel.add(Box.createRigidArea(HGAP30));
// Create the list
list = new JList();
//* 由jb2011注释掉,以便测试Ui里的通用renderer
// list.setCellRenderer(new CompanyLogoListCellRenderer());
listModel = new GeneratedListModel(this);
list.setModel(listModel);
// Set the preferred row count. This affects the preferredSize
// of the JList when it's in a scrollpane.
list.setVisibleRowCount(22);
// Add list to a scrollpane
JScrollPane scrollPane = new JScrollPane(list);
listPanel.add(scrollPane);
listPanel.add(Box.createRigidArea(VGAP10));
// Add the control panel (holds the prefix/suffix list and prefix/suffix checkboxes)
centerPanel.add(createControlPanel());
// create prefixes and suffixes
addPrefix("Tera", true);
addPrefix("Micro", false);
addPrefix("Southern", false);
addPrefix("Net", true);
addPrefix("YoYo", true);
addPrefix("Northern", false);
addPrefix("Tele", false);
addPrefix("Eastern", false);
addPrefix("Neo", false);
addPrefix("Digi", false);
addPrefix("National", false);
addPrefix("Compu", true);
addPrefix("Meta", true);
addPrefix("Info", false);
addPrefix("Western", false);
addPrefix("Data", false);
addPrefix("Atlantic", false);
addPrefix("Advanced", false);
addPrefix("Euro", false);
addPrefix("Pacific", false);
addPrefix("Mobile", false);
addPrefix("In", false);
addPrefix("Computa", false);
addPrefix("Digital", false);
addPrefix("Analog", false);
addSuffix("Tech", true);
addSuffix("Soft", true);
addSuffix("Telecom", true);
addSuffix("Solutions", false);
addSuffix("Works", true);
addSuffix("Dyne", false);
addSuffix("Services", false);
addSuffix("Vers", false);
addSuffix("Devices", false);
addSuffix("Software", false);
addSuffix("Serv", false);
addSuffix("Systems", true);
addSuffix("Dynamics", true);
addSuffix("Net", false);
addSuffix("Sys", false);
addSuffix("Computing", false);
addSuffix("Scape", false);
addSuffix("Com", false);
addSuffix("Ware", false);
addSuffix("Widgets", false);
addSuffix("Media", false);
addSuffix("Computer", false);
addSuffix("Hardware", false);
addSuffix("Gizmos", false);
addSuffix("Concepts", false);
}
@Override
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
switch (command)
{
case SAVE_COMMAND:
final JList sourcesList = new JList<>();
final JTextField nameField = new JTextField();
ListFacade<SourceSelectionFacade> sources = new SortedListFacade<>(Comparators.toStringIgnoreCaseCollator(),
FacadeFactory.getCustomSourceSelections());
sourcesList.setModel(new FacadeListModel<>(sources));
sourcesList.addListSelectionListener(lse -> nameField.setText(sourcesList.getSelectedValue().toString()));
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JScrollPane(sourcesList), BorderLayout.CENTER);
panel.add(nameField, BorderLayout.SOUTH);
int ret = JOptionPane.showOptionDialog(this, panel, "Save the source selection as...",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null);
if (ret == JOptionPane.OK_OPTION)
{
String name = nameField.getText();
List<Campaign> selectedCampaigns = advancedPanel.getSelectedCampaigns();
GameMode selectedGameMode = advancedPanel.getSelectedGameMode();
SourceSelectionFacade selection = null;
for (SourceSelectionFacade sourceSelectionFacade : sources)
{
if (sourceSelectionFacade.toString().equals(name))
{
selection = sourceSelectionFacade;
break;
}
}
if (selection == null)
{
selection = FacadeFactory.createCustomSourceSelection(name);
}
selection.setCampaigns(selectedCampaigns);
selection.setGameMode(selectedGameMode);
basicPanel.setSourceSelection(selection);
}
break;
case DELETE_COMMAND:
FacadeFactory.deleteCustomSourceSelection(basicPanel.getSourceSelection());
break;
case LOAD_COMMAND:
fireSourceLoad();
break;
case INSTALLDATA_COMMAND:
// Swap to the install data dialog.
setVisible(false);
DataInstaller di = new DataInstaller();
di.setVisible(true);
break;
default: //must be the cancel command
setVisible(false);
break;
}
}
protected void setupInstance(JList instance) {
super.setupInstance(instance);
ListModel model = dataModel == null ? null : dataModel.createInstance();
if (model != null) instance.setModel(model);
}