- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的最终目标是拥有两个 JList
,用户可以在它们之间来回移动元素。我使用 TreeSet
以便按字母顺序插入元素。这是一个直观的表示:
下面是我迄今为止的代码,位于 SSCCE 中。它大部分工作正常,但有时我会收到 ArrayOutOfBoundsException 异常。问题与程序认为选择的元素比实际选择的元素多有关。重现问题的一种方法是选择左侧的两个元素,然后按两次右侧按钮。
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
public class GUI extends JFrame {
private GridBagLayout gridBag = new GridBagLayout();
private static final String[] ALL_STRINGS = { "B", "A", "C" };
private JButton leftButton = new JButton("<");
private JButton rightButton = new JButton(">");
private StringListModel listModel = new StringListModel(Arrays.asList(ALL_STRINGS));
private StringListModel queueModel = new StringListModel();
private JList<String> list = new JList<String>(listModel);
private JList<String> queue = new JList<String>(queueModel);
public GUI() {
setWindowProperties();
addComponents();
}
private void setWindowProperties() {
setLayout(gridBag);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(new Dimension(300, 200));
setTitle("JList, ListModel, and TreeSet");
setResizable(false);
setLocationRelativeTo(null);
}
private void addComponents() {
leftButton.addActionListener(new QueueListener());
rightButton.addActionListener(new QueueListener());
JScrollPane listScroll = new JScrollPane(list);
JScrollPane queueScroll = new JScrollPane(queue);
Dimension scrollSize = new Dimension(50, 100);
listScroll.setPreferredSize(scrollSize);
queueScroll.setPreferredSize(scrollSize);
add(listScroll);
add(leftButton);
add(rightButton);
add(queueScroll);
}
private class QueueListener implements ActionListener {
private JButton button;
@Override
public void actionPerformed(ActionEvent e) {
button = (JButton) e.getSource();
if (button.equals(leftButton)) {
removeFromQueue();
} else if (button.equals(rightButton)) {
addToQueue();
}
}
private void removeFromQueue() {
List<String> Strings = queue.getSelectedValuesList();
queueModel.removeAll(Strings);
listModel.addAll(Strings);
}
private void addToQueue() {
List<String> Strings = list.getSelectedValuesList();
listModel.removeAll(Strings);
queueModel.addAll(Strings);
}
}
private class StringListModel extends DefaultListModel<String> {
private TreeSet<String> model = new TreeSet<String>();
public StringListModel() {
}
public StringListModel(List<String> Strings) {
addAll(Strings);
}
public int getSize() {
return model.size();
}
public String getElementAt(int index) {
return (String) model.toArray()[index];
}
public void add(String String) {
if (model.add(String)) {
fireContentsChanged(this, 0, getSize());
}
}
public void addAll(List<String> quets) {
for (String String : quets) {
model.add(String);
}
fireContentsChanged(this, 0, getSize());
}
public void clear() {
model.clear();
fireContentsChanged(this, 0, getSize());
}
public boolean contains(Object element) {
return model.contains(element);
}
public String firstElement() {
return model.first();
}
public Iterator iterator() {
return model.iterator();
}
public String lastElement() {
return model.last();
}
public void removeAll(Collection<?> elements) {
for (Object element : elements) {
removeElement(element);
}
fireContentsChanged(this, 0, getSize());
}
public boolean removeElement(Object element) {
boolean removed = model.remove(element);
if (removed) {
fireContentsChanged(this, 0, getSize());
}
return removed;
}
}
public static void main(String[] args) {
new GUI().setVisible(true);
}
}
最佳答案
这个实现似乎有效。请注意,我将 TreeSet
更改为简单的 List
。缺点是插入/删除会慢一些,但至少我可以触发正确的事件。鉴于它是一个 UI 小部件,我认为这是可以接受的,因为底层模型永远不会很大(否则 UI 将变得不可用)。
我从 AbstractListModel
扩展,但您也可以从 DefaultListModel
扩展。在这种情况下,您可能希望确保 addElement
方法计算正确的索引并调用 super.add(calculatedIndex, element)
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
public class GUI extends JFrame {
private GridBagLayout gridBag = new GridBagLayout();
private static final String[] ALL_STRINGS = {"B", "A", "C"};
private JButton leftButton = new JButton( "<" );
private JButton rightButton = new JButton( ">" );
private StringListModel listModel = new StringListModel( Arrays.asList( ALL_STRINGS ) );
private StringListModel queueModel = new StringListModel();
private JList<String> list = new JList<String>( listModel );
private JList<String> queue = new JList<String>( queueModel );
public GUI() {
setWindowProperties();
addComponents();
}
private void setWindowProperties() {
setLayout( gridBag );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize( new Dimension( 300, 200 ) );
setTitle( "JList, ListModel, and TreeSet" );
setResizable( false );
setLocationRelativeTo( null );
}
private void addComponents() {
leftButton.addActionListener( new QueueListener() );
rightButton.addActionListener( new QueueListener() );
JScrollPane listScroll = new JScrollPane( list );
JScrollPane queueScroll = new JScrollPane( queue );
Dimension scrollSize = new Dimension( 50, 100 );
listScroll.setPreferredSize( scrollSize );
queueScroll.setPreferredSize( scrollSize );
add( listScroll );
add( leftButton );
add( rightButton );
add( queueScroll );
}
private class QueueListener implements ActionListener {
private JButton button;
@Override
public void actionPerformed( ActionEvent e ) {
button = ( JButton ) e.getSource();
if ( button.equals( leftButton ) ) {
removeFromQueue();
}
else if ( button.equals( rightButton ) ) {
addToQueue();
}
}
private void removeFromQueue() {
List<String> Strings = queue.getSelectedValuesList();
queueModel.removeAll( Strings );
listModel.addAll( Strings );
}
private void addToQueue() {
List<String> Strings = list.getSelectedValuesList();
listModel.removeAll( Strings );
queueModel.addAll( Strings );
}
}
private class StringListModel extends AbstractListModel<String> {
private List<String> model = new ArrayList<>();
public StringListModel() {
}
public StringListModel( List<String> strings ) {
addAll( strings );
}
@Override
public int getSize() {
return model.size();
}
@Override
public String getElementAt( int index ) {
return model.toArray( new String[model.size()])[index];
}
public void addAll( Collection<String> strings ){
for ( String string : strings ) {
add( string );
}
}
public void add( String string ){
int index = calculateIndex( string );
model.add( index, string );
fireIntervalAdded( this, index, index );
}
public void remove( String string ){
int index = model.indexOf( string );
if ( index != -1 ){
model.remove( index );
fireIntervalRemoved( this, index, index );
}
}
public void removeAll( Collection<String> strings ){
for ( String next : strings ) {
remove( next );
}
}
private int calculateIndex( String input ){
if ( model.size() == 0 ){
return 0;
}
int index = 0;
while ( model.get( index ).compareTo( input ) <=0 ){
index++;
if ( index == model.size() ){
return index;
}
}
return index;
}
}
public static void main( String[] args ) {
EventQueue.invokeLater( new Runnable() {
@Override
public void run() {
new GUI().setVisible( true );
}
} );
}
}
关于java - 使用 ListModel 来隐藏 TreeSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18560930/
如何判断两个TreeSet对象是否相等?我使用open-jdk-10。 ModifiebleObject class ModifiebleObject implements Comparable{
我正在使用 TreeSet 并在调用 TreeSet#add() 方法时发现了 ClassCastException。 代码: public class Testing { public st
假设我有一个自然排序的 TreeSet。我可以使用什么接口(interface)和方法来使新的 TreeSet 保持与第一个列表相同的顺序。 最佳答案 使用相同的Comparator(如果您的元素实现
是否有与 java.util.TreeSet 等效的 VB.NET? 最佳答案 您会发现最接近的是 SortedSet(T) class . 关于treeset - VB.NET 相当于 java.u
我有一个 TreeMap,其中的值是 TreeSet。现在我需要遍历键,对于 TreeSet 的每个元素,我必须删除该元素(然后继续做某事),然后删除该 TreeSet 的第二个元素等。 我试过: f
我今天接受了采访,接受我采访的人对他的陈述感到困惑,询问是否有可能 TreeSet等于 HashSet但不是 HashSet等于 TreeSet .我说“不”,但据他说,答案是"is"。 怎么可能?
这对我来说是一个很深的谜。 看看这个: TreeSet s = new TreeSet(); s.add(Long.valueOf(1)); s.add(Long.valueOf(4)); s.add
如果我想在 Java 的 TreeSet 中删除 log(n) 时间内的最高条目,我使用 treeSet.pollFirst() - Scala 的 mutable.TreeSet 类的等价物是什么?
例如,有一个二叉搜索树,其中包含一系列值。在添加新值之前,我需要检查它是否已经包含它“几乎重复”。我有 Java 解决方案,它只是执行地板和天花板以及完成这项工作的进一步条件。 JAVA : 给定一个
作为最佳实践, float 的集合类型实例不应超过一个。例如,Nil 是 scala 库中的一个 case 对象。 但是, TreeMap 和 TreeSet 在每次 empty() 调用时都会创建一
1、TreeSet 概述 1、TreeSet是 SortedSet 接口的实现类, TreeSet 可以确保集合元素处于排序状态。 2、TreeSet顾名思义他内部维护的是一个TreeMap,
我正在尝试创建一个 TreeSet 来对插入的字符串进行升序排序。我正在使用以下代码在 TreeSet 中输入值。 TreeSet ts = new TreeSet(); ts.add("@Test0
对于此作业,我需要将每个包含 2 个字符串的自定义数据类(称为 User)的实例保存到 TreeSet 中。然后,我必须在我创建的 TreeSet 中搜索从另一个文件的每一行中提取的字符串。第一个文件
好的,我有这个问题要解决: 创建一个名为 VirtualLibrary 的泛型类具有单个属性 totalNumberOfEntries , 以及使用户能够设置和返回条目的方法。条目类型为 Book ,
我不知道如何解释/理解以下有关 TreeSet 和 map 函数的行为。 我想我遗漏了一 block 拼图。任何有关此事的线索都将受到欢迎。 scala> class Person(val name:
我有一个间隔的TreeSet(带有开始和结束的案例类)。如果对此树集进行过滤,例如 treeSet.filter(x => input = x.start) 这预计会在 logN 时间内运行吗? 最佳
我需要一种方法来非常快速地计算整数 TreeSet 中小于 X 的元素数量。 我可以使用 子集() headSet() tailSet() 方法,但它们真的很慢(我只需要计数,而不是数字本身)。有办法
我是java新手,我正在尝试访问与其他类不同的类中的ArrayList和TreeSet。我知道这种从一个类访问它的方式。 这是我拥有 TreeSet 的类(class): public class
这个问题已经有答案了: Java : Comparable vs Comparator [duplicate] (2 个回答) 已关闭 3 年前。 这是我的代码 public class SetTes
我需要保留一个排序的节点列表,从第一个开始,然后获取所有相邻节点。第一个节点和所有其他节点都带有一个种子值,用于根据最低种子值确定接下来将使用哪个节点,一旦一个节点被用于获取相邻节点,它就会被标记为已
我是一名优秀的程序员,十分优秀!