0
我試圖在自定義彈出菜單中上下自動更新滾動窗格上的滾動條。但它不起作用。看看下面我完全可運行的示例,我已經添加了基於自定義JPopupMenu的組件和一個JComboBox進行比較。如您所見,在JComboBox中,滾動窗格會更新其位置,以便當前選定的項目始終可見。但是,這不適用於我的組件,我不知道如何設置。我自定義組件上的JScrollpane沒有更新
我SSCCE:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.ScrollPane;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class ScrollFocus extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ScrollFocus();
}
});
}
public ScrollFocus() {
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Vector<String> values = new Vector<>();
values.add("a");
values.add("b");
values.add("c");
values.add("d");
values.add("e");
values.add("f");
values.add("g");
values.add("h");
values.add("i");
values.add("j");
JComboBox<String> comboBox = new JComboBox<>(values);
JScrollPane scrollPane = new JScrollPane(comboBox);
this.add(scrollPane, BorderLayout.NORTH);
CustomTextField customTextField = new CustomTextField();
this.add(customTextField, BorderLayout.CENTER);
pack();
setVisible(true);
}
class CustomTextField extends JTextField implements FocusListener {
private CustomPopup customPopup = new CustomPopup();
public CustomTextField() {
this.addFocusListener(this);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
System.out.println("printing...");
setPopupSize();
setPosition();
customPopup.setVisible(true);
if(e.getKeyCode() == KeyEvent.VK_UP) {
if(customPopup.index < customPopup.getListSize())
customPopup.setSelectedIndex(customPopup.index--);
}
else if(e.getKeyCode() == KeyEvent.VK_DOWN){
if(customPopup.index >= 0)
customPopup.setSelectedIndex(customPopup.index++);
}
}
});
}
public void setPopupSize() {
customPopup.setPopupSize(new Dimension(this.getWidth(), 110));
}
public void setPosition() {
customPopup.setLocation(this.getLocation().x, this.getLocation().y);
}
@Override
public void focusGained(FocusEvent e) {
}
@Override
public void focusLost(FocusEvent e) {
customPopup.setVisible(false);
}
class CustomPopup extends JPopupMenu {
String[] values = new String[]{"Value1", "Value2", "Value3", "Value4", "Value5", "Value6", "Value7",
"Value8","Value9", "Value10", "Value11", "Value12", "Value13", "Value14", "Value15", "Value16",};
JList<String> list = new JList<>(values);
JScrollPane scrollPane = new JScrollPane(list);
public int index = 0;
public CustomPopup() {
this.setLayout(new GridLayout(0,1));
this.add(scrollPane);
pack();
}
public void setSelectedIndex(int index) {
list.setSelectedIndex(index);
}
public int getListSize() {
return values.length;
}
}
}
}
太簡單了!謝謝! :-) – Rox
謝謝,我會用鍵綁定。一個問題:是否可以使用一行Java代碼在一個actionmap中綁定多個擊鍵,或者是否必須編寫'this.getInputMap()。put(KeyStroke.getKeyStroke(KeyEvent.VK_F,0),「press」 );'爲每個關鍵的中風? – Rox
是否可以一次綁定所有密鑰? – Rox