Swing に基づく単純な GUI を使用して、Java で小さなアノテーターをコーディングしましたが、私を驚かせる問題に直面しました。問題は、jlist と、そのような jlist を変更する 2 つの jbuttons があります。2 つのボタンには同じリスナーがありますが、同じように動作しません。jlist で行を選択すると、ON TOPIC または OFF TOPIC (2 つのボタンを使用) としてタグ付けできます。行の色が変わり、次の行が選択されます。選択が右側の行 (次の行) にある場合でも、[トピックをオフ] ボタンの次の行が強調表示されるのはなぜですか?
コードは次のとおりです。
public class TweetsAnnotator {
static Boolean[] annotations = null;
@SuppressWarnings("rawtypes")
static JList jl;
static JButton offbutton = new JButton("OFF Topic");
static JButton onbutton = new JButton("ON Topic");
static String file = "inception_TweetList";
public TweetsAnnotator() {
}
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
* @throws ClassNotFoundException
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
// Read Tweets from file
ObjectInputStream load = new ObjectInputStream(new FileInputStream(file));
ArrayList<String> list = (ArrayList<String>) load.readObject();
load.close();
System.out.println(list.size() + " Tweets read from: " + file);
// Check and read annotations
File fileannot = new File(file + "Annotations");
if (fileannot.exists()) {
System.out.println("esiste, leggo");
ObjectInputStream loadannot = new ObjectInputStream(new FileInputStream(file + "Annotations"));
annotations = (Boolean[]) loadannot.readObject();
loadannot.close();
} else {
System.out.println("non esiste, creo poi leggo");
ObjectOutputStream save = new ObjectOutputStream(new FileOutputStream(file + "Annotations"));
Boolean[] creatannotations = new Boolean[list.size()];
for (int i=0; i<list.size(); i++) {
creatannotations[i] = (Boolean) null;
}
save.writeObject(creatannotations);
save.close();
ObjectInputStream loadannot = new ObjectInputStream(new FileInputStream(file + "Annotations"));
annotations = (Boolean[]) loadannot.readObject();
loadannot.close();
}
System.out.println(annotations.length + " Annotations loaded");
// Buttons
offbutton.setActionCommand("off");
offbutton.addActionListener(new ButtonListener());
offbutton.setEnabled(false);
onbutton.setActionCommand("on");
onbutton.addActionListener(new ButtonListener());
onbutton.setEnabled(false);
// ButtonPanel
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
buttonPane.add(onbutton);
buttonPane.add(offbutton);
// JList
jl = new JList((Object[])list.toArray());
jl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jl.setLayoutOrientation(JList.VERTICAL);
jl.setVisibleRowCount(-1);
jl.setCellRenderer(new MyCellRenderer());
ListSelectionListener listSelectionListener = new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
if (jl.getSelectedIndex() == -1) {
//No selection, disable buttons.
onbutton.setEnabled(false);
offbutton.setEnabled(false);
} else {
//Selection, enable buttons.
onbutton.setEnabled(true);
offbutton.setEnabled(true);
}
}
}
};
jl.addListSelectionListener(listSelectionListener);
// JScrollPane
JScrollPane listScroller = new JScrollPane(jl);
// JFrame
JFrame frame = new JFrame(file);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
frame.addWindowListener(new WindowCloseHandler());
// Add and show
frame.getContentPane().add(listScroller, BorderLayout.CENTER);
frame.getContentPane().add(buttonPane, BorderLayout.PAGE_END);
frame.pack();
frame.setVisible(true);
}
private static class MyCellRenderer extends DefaultListCellRenderer {
private static final long serialVersionUID = 1L;
@SuppressWarnings("rawtypes")
public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus ) {
Component c = super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus );
if ( annotations[index] == null ) {
c.setBackground( Color.white );
}
else if (annotations[index] == true) {
c.setBackground( Color.green );
} else {
c.setBackground( Color.red);
}
return c;
}
}
private static class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int ind = jl.getSelectedIndex() +1;
if (e.getActionCommand().equals("on")) {
System.out.println("ON");
annotations[jl.getSelectedIndex()] = true;
}
if (e.getActionCommand().equals("off")) {
System.out.println("OFF");
annotations[jl.getSelectedIndex()] = false;
}
jl.clearSelection();
jl.setSelectedIndex(ind);
}
}
private static class WindowCloseHandler extends WindowAdapter {
public void windowClosing(WindowEvent evt) {
ObjectOutputStream save = null;
try {
save = new ObjectOutputStream(new FileOutputStream(file + "Annotations"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
save.writeObject(annotations);
} catch (IOException e) {
e.printStackTrace();
}
try {
save.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Saved.");
}
}
}
jl.requestFocus(); でも 正常に動作します。動作させる別のことを試しました:これらの2行を交換しました
buttonPane.add(onbutton);
buttonPane.add(offbutton);
しかし、なぜ?もう一度聞いたら申し訳ありませんが、それは本当に奇妙ですよね?