2
我已將JComoboBox
中的箭頭按鈕移除,使其看起來像JTextField
並將其添加爲celleditor。其目的是創建一個AutoSuggest(而不是AutoComplete)JTable單元。移除JTable內的JCombobox邊框
在做這樣做的邊界有點看起來irking.How更改邊框,使其看起來像右邊的文本框邊框。我曾嘗試刪除邊框創建的線條邊框。但它並沒有消除藍色的邊界。
使用Nimbus UI。
MCVE的問題
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class Sample extends JFrame {
public Sample() {
init();
}
private void init() {
JTable table = new JTable(5, 5);
DefaultCellEditor cellEditor = new DefaultCellEditor(new EditorCombo());
cellEditor.setClickCountToStart(2);
table.getColumnModel().getColumn(0)
.setCellEditor(cellEditor);
table.setRowHeight(30);
table.setCellSelectionEnabled(true);
add(new JScrollPane(table));
}
public static void main(String[] args) {
setUpUI("Nimbus");
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Sample samp = new Sample();
samp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
samp.setTitle("Table Test");
samp.pack();
samp.setLocationRelativeTo(null);
samp.setVisible(true);
}
});
}
private static void setUpUI(String ui) {
try {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if (ui.equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
Logger.getLogger(Sample.class.getName()).log(Level.SEVERE, null, ex);
}
}
class EditorCombo extends JComboBox {
public EditorCombo() {
setEditable(true);
for (int i = 0; i < 10; i++) {
addItem("Sample" + i);
}
setUI(new javax.swing.plaf.synth.SynthComboBoxUI() {
@Override
protected JButton createArrowButton() {
JButton button = new JButton() {
@Override
public int getWidth() {
return 0;
}
};
button.setBorder(BorderFactory.createEmptyBorder());
button.setVisible(false);
return button;
}
@Override
public void configureArrowButton() {
}
});
}
}
}