下面列出了怎么用java.awt.List的API类实例代码及写法,或者点击链接到github查看源代码。
public static void main(final String[] args) throws HeadlessException {
final Frame frame = new Frame("Test Frame");
final List list = new List();
frame.setSize(300, 200);
list.add(ITEM_NAME);
list.select(0);
frame.add(list);
frame.validate();
frame.setVisible(true);
sleep();
if (!ITEM_NAME.equals(list.getSelectedItem())) {
throw new RuntimeException("List item not selected item.");
}
list.removeAll();
frame.dispose();
}
public void put(IValidatable bean) {
try {
boolean selected = "true".equals(BeanUtils.getProperty(bean,
_stateProperty));
_button.setSelected(selected);
_textArea.setEnabled(selected);
List list = (List) PropertyUtils.getProperty(bean, _property);
StringBuffer sb = new StringBuffer();
if (list != null) {
for (int i = 0; i < list.size(); i++) {
sb.append(list.get(i));
if (i < list.size() - 1) {
sb.append("\n");
}
}
}
_textArea.setText(sb.toString());
} catch (Exception e) {
throw new BindingException(e);
}
}
public static void main(final String[] args) throws HeadlessException {
final Frame frame = new Frame("Test Frame");
final List list = new List();
frame.setSize(300, 200);
list.add(ITEM_NAME);
list.select(0);
frame.add(list);
frame.validate();
frame.setVisible(true);
sleep();
if (!ITEM_NAME.equals(list.getSelectedItem())) {
throw new RuntimeException("List item not selected item.");
}
list.removeAll();
frame.dispose();
}
public static void main(final String[] args) throws HeadlessException {
final Frame frame = new Frame("Test Frame");
final List list = new List();
frame.setSize(300, 200);
list.add(ITEM_NAME);
list.select(0);
frame.add(list);
frame.validate();
frame.setVisible(true);
sleep();
if (!ITEM_NAME.equals(list.getSelectedItem())) {
throw new RuntimeException("List item not selected item.");
}
list.removeAll();
frame.dispose();
}
private MethodTree m(TreeMaker make) {
// create method modifiers
ModifiersTree parMods = make.Modifiers(Collections.<Modifier>emptySet(), Collections.<AnnotationTree>emptyList());
// create parameters
VariableTree par1 = make.Variable(parMods, "a", make.PrimitiveType(TypeKind.INT), null);
VariableTree par2 = make.Variable(parMods, "b", make.PrimitiveType(TypeKind.FLOAT), null);
List<VariableTree> parList = new ArrayList<VariableTree>(2);
parList.add(par1);
parList.add(par2);
// create method
MethodTree newMethod = make.Method(
make.Modifiers(
Collections.singleton(Modifier.PUBLIC), // modifiers
Collections.<AnnotationTree>emptyList() // annotations
), // modifiers and annotations
"newlyCreatedMethod", // name
make.PrimitiveType(TypeKind.VOID), // return type
Collections.<TypeParameterTree>emptyList(), // type parameters for parameters
parList, // parameters
Collections.singletonList(make.Identifier("java.io.IOException")), // throws
make.Block(Collections.<StatementTree>emptyList(), false), // empty statement block
null // default value - not applicable here, used by annotations
);
return newMethod;
}
public static void main(final String[] args) throws HeadlessException {
final Frame frame = new Frame("Test Frame");
final List list = new List();
frame.setSize(300, 200);
list.add(ITEM_NAME);
list.select(0);
frame.add(list);
frame.validate();
frame.setVisible(true);
sleep();
if (!ITEM_NAME.equals(list.getSelectedItem())) {
throw new RuntimeException("List item not selected item.");
}
list.removeAll();
frame.dispose();
}
public ItemEventTest()
{
try {
robot = new Robot();
} catch(AWTException e) {
throw new RuntimeException(e.getMessage());
}
expectedSelectionOrder = "01230123";
list = new List(4, true);
list.add("0");
list.add("1");
list.add("2");
list.add("3");
add(list);
setSize(400,400);
setLayout(new FlowLayout());
pack();
setVisible(true);
robot.waitForIdle();
}
public void get(IValidatable bean) {
try {
String text = _textArea.getText();
if (!text.equals("")) {
String[] items = text.split("\n");
List list = new ArrayList();
for (int i = 0; i < items.length; i++) {
list.add(items[i]);
}
PropertyUtils.setProperty(bean, _property, list);
} else {
PropertyUtils.setProperty(bean, _property, null);
}
} catch (Exception e) {
throw new BindingException(e);
}
}
public void testRangeHighlighterLinesInRangeForLongLinePerformance() throws Exception {
final int N = 50000;
Document document = EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol('x', 2 * N));
final MarkupModelEx markupModel = (MarkupModelEx)DocumentMarkupModel.forDocument(document, ourProject, true);
for (int i=0; i<N-1;i++) {
markupModel.addRangeHighlighter(2*i, 2*i+1, 0, null, HighlighterTargetArea.EXACT_RANGE);
}
markupModel.addRangeHighlighter(N / 2, N / 2 + 1, 0, null, HighlighterTargetArea.LINES_IN_RANGE);
PlatformTestUtil.startPerformanceTest("slow highlighters lookup", (int)(N*Math.log(N)/1000), new ThrowableRunnable() {
@Override
public void run() {
List<RangeHighlighterEx> list = new ArrayList<RangeHighlighterEx>();
CommonProcessors.CollectProcessor<RangeHighlighterEx> coll = new CommonProcessors.CollectProcessor<RangeHighlighterEx>(list);
for (int i=0; i<N-1;i++) {
list.clear();
markupModel.processRangeHighlightersOverlappingWith(2*i, 2*i+1, coll);
assertEquals(2, list.size()); // 1 line plus one exact range marker
}
}
}).assertTiming();
}
public static void main(final String[] args) throws HeadlessException {
final Frame frame = new Frame("Test Frame");
final List list = new List();
frame.setSize(300, 200);
list.add(ITEM_NAME);
list.select(0);
frame.add(list);
frame.validate();
frame.setVisible(true);
sleep();
if (!ITEM_NAME.equals(list.getSelectedItem())) {
throw new RuntimeException("List item not selected item.");
}
list.removeAll();
frame.dispose();
}
public static void main(final String[] args) throws HeadlessException {
final Frame frame = new Frame("Test Frame");
final List list = new List();
frame.setSize(300, 200);
list.add(ITEM_NAME);
list.select(0);
frame.add(list);
frame.validate();
frame.setVisible(true);
sleep();
if (!ITEM_NAME.equals(list.getSelectedItem())) {
throw new RuntimeException("List item not selected item.");
}
list.removeAll();
frame.dispose();
}
@Override
public boolean checkComponent(Component comp) {
if (comp instanceof List) {
if (label == null) {
return true;
}
if (((List) comp).getItemCount() > itemIndex) {
int ii = itemIndex;
if (ii == -1) {
ii = ((List) comp).getSelectedIndex();
if (ii == -1) {
return false;
}
}
return (comparator.equals(((List) comp).getItem(ii),
label));
}
}
return false;
}
public void testE4() {
DocumentEx document = (DocumentEx)EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', 10));
List<RangeMarker> mm =
add(document, 3,5, 5,6, 4,8, 6,9, 8,9
);
edit(document, 6,0,0, 3,0,2);
delete(mm, 1,0);
}
public static void main(String[] args) throws IntrospectionException {
if (getLength(com.foo.test.Component.class) == getLength(Component.class)) {
throw new Error("test failed for Component");
}
if (getLength(java.util.List.class) == getLength(List.class)) {
throw new Error("test failed for List");
}
}
public static void main(String[] args) throws IntrospectionException {
if (getLength(com.foo.test.Component.class) == getLength(Component.class)) {
throw new Error("test failed for Component");
}
if (getLength(java.util.List.class) == getLength(List.class)) {
throw new Error("test failed for List");
}
}
public void testX2() {
DocumentEx document = (DocumentEx)EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', 10));
List<RangeMarker> mm =
add(document, 2,9, 0,0, 7,7
);
delete(mm, 0);
}
@Override
public final ListPeer createList(List target) {
PlatformComponent platformComponent = createPlatformComponent();
LWListPeer peer = new LWListPeer(target, platformComponent);
targetCreatedPeer(target, peer);
peer.initialize();
return peer;
}
public void testE1() {
DocumentEx document = (DocumentEx)EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', 10));
List<RangeMarker> mm =
add(document, 3,5, 0,1, 9,9
);
edit(document, 3,6,0);
delete(mm, 0);
}
@Override
public final ListPeer createList(List target) {
PlatformComponent platformComponent = createPlatformComponent();
LWListPeer peer = new LWListPeer(target, platformComponent);
targetCreatedPeer(target, peer);
peer.initialize();
return peer;
}
public static void main(String[] args) {
// ensure that 4168475 does not regress
test4168475(Component.class);
// AWT classes (com.sun.beans.infos.ComponentBeanInfo)
test(null, Button.class, Component.class, List.class, Menu.class, Panel.class);
// Swing classes (dt.jar)
test(null, JApplet.class, JButton.class, JCheckBox.class);
// user defined classes
test(Boolean.TRUE, Wombat.class, Foo.class, FooBar.class);
}
public void testRangeHighlighterIteratorOrder() throws Exception {
Document document = EditorFactory.getInstance().createDocument("1234567890");
final MarkupModelEx markupModel = (MarkupModelEx)DocumentMarkupModel.forDocument(document, ourProject, true);
RangeHighlighter exact = markupModel.addRangeHighlighter(3, 6, 0, null, HighlighterTargetArea.EXACT_RANGE);
RangeHighlighter line = markupModel.addRangeHighlighter(4, 5, 0, null, HighlighterTargetArea.LINES_IN_RANGE);
List<RangeHighlighter> list = new ArrayList<RangeHighlighter>();
markupModel.processRangeHighlightersOverlappingWith(2, 9, new CommonProcessors.CollectProcessor<RangeHighlighter>(list));
assertEquals(Arrays.asList(line, exact), list);
}
private void initializeLists() {
noteList = new List(6);
noteList.add( "Rest" );
noteList.add( "A" );
noteList.add( "A#" );
noteList.add( "B" );
noteList.add( "C" );
noteList.add( "C#" );
noteList.add( "D" );
noteList.add( "D#" );
noteList.add( "E" );
noteList.add( "F" );
noteList.add( "F#" );
noteList.add( "G" );
noteList.add( "G#" );
octaveList = new List(6);
octaveList.add("-1");
octaveList.add("0");
octaveList.add("1");
octaveList.add("2");
octaveList.add("3");
octaveList.add("4");
octaveList.add("5");
octaveList.add("6");
octaveList.add("7");
octaveList.add("8");
octaveList.add("9");
}
public static void main(String[] args) throws IntrospectionException {
if (getLength(com.foo.test.Component.class) == getLength(Component.class)) {
throw new Error("test failed for Component");
}
if (getLength(java.util.List.class) == getLength(List.class)) {
throw new Error("test failed for List");
}
}
public static void main(String[] args) {
// ensure that 4168475 does not regress
test4168475(Component.class);
// AWT classes (com.sun.beans.infos.ComponentBeanInfo)
test(null, Button.class, Component.class, List.class, Menu.class, Panel.class);
// Swing classes (dt.jar)
test(null, JApplet.class, JButton.class, JCheckBox.class);
// user defined classes
test(Boolean.TRUE, Wombat.class, Foo.class, FooBar.class);
}
@Override
public ListPeer createList(List target) {
PlatformComponent platformComponent = createPlatformComponent();
LWListPeer peer = new LWListPeer(target, platformComponent);
targetCreatedPeer(target, peer);
peer.initialize();
return peer;
}
public void testE3() {
DocumentEx document = (DocumentEx)EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', 10));
List<RangeMarker> mm =
add(document, 4,5, 6,8, 3,4, 4,9, 2,9
);
edit(document, 4,6,0);
delete(mm, 0);
}
/** @return Propertydescriptors */
@Override
protected PropertyDescriptor[] createPDs() throws IntrospectionException {
return new PropertyDescriptor[] {
new PropertyDescriptor("multipleMode", List.class), // NOI18N
};
}
public void testX3() {
DocumentEx document = (DocumentEx)EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', 10));
List<RangeMarker> mm =
add(document, 1,9, 8,8, 8,8, 0,5, 4,5
);
delete(mm, 0);
}
@Override
public final ListPeer createList(List target) {
PlatformComponent platformComponent = createPlatformComponent();
LWListPeer peer = new LWListPeer(target, platformComponent);
targetCreatedPeer(target, peer);
peer.initialize();
return peer;
}
public static void main(String[] args) {
// ensure that 4168475 does not regress
test4168475(Component.class);
// AWT classes (com.sun.beans.infos.ComponentBeanInfo)
test(null, Button.class, Component.class, List.class, Menu.class, Panel.class);
// Swing classes (dt.jar)
test(null, JApplet.class, JButton.class, JCheckBox.class);
// user defined classes
test(Boolean.TRUE, Wombat.class, Foo.class, FooBar.class);
}