import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class TestCombo extends JDialog {
private static final long serialVersionUID = 1L;
static Font myFont = new Font("Arial", Font.PLAIN, 12);
static Font lastFont = new Font("Noteworthy", Font.PLAIN, 18);
static int index;
static String[] allFonts;
static int maxW = 0;
@SuppressWarnings({ "unchecked", "rawtypes" })
public TestCombo() {
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setModal(true);
setBounds(300, 300, 300, 300);
GraphicsEnvironment gen = GraphicsEnvironment.getLocalGraphicsEnvironment();
allFonts = gen.getAvailableFontFamilyNames();
System.out.println("1. allFonts.length = "+allFonts.length);
Font[] fAll = gen.getAllFonts();
String addFnts = "";
for(Font f : fAll) {
String fNam = f.getFontName();
if(f.canDisplayUpTo(fNam) < 0) {
addFnts += ","+fNam;
}
}
JLabel testTF = new JLabel();
testTF.setText("<html>"+addFnts.substring(1).replaceAll(",", "<br>")+"</html>");
testTF.updateUI();
maxW = testTF.getPreferredSize().width+28; //Breite+ Vertical-Scrollbar plus ein wenig Abstand
allFonts = addFnts.substring(1).split(",");
System.out.println("2. allFonts.length = "+allFonts.length);
String lastUse = "Noteworthy";
setLocationRelativeTo(null);
JPanel center = new JPanel();
getContentPane().add(center, BorderLayout.CENTER);
center.setLayout(null);
JLabel fontMuster = new JLabel();
fontMuster.setText("Muster Text");
fontMuster.setVerticalAlignment(0);
fontMuster.setBounds(20, 200, 150, 50);
fontMuster.setFont(lastFont);
center.add(fontMuster);
Rectangle forNC = new Rectangle(20, 10, 190, 24);
JComboBox<Object[]> nc = new JComboBox<>();
nc.setModel(new DefaultComboBoxModel(allFonts));
nc.setEditable(true);
nc.setBackground(Color.WHITE);
nc.setUI(new BasicComboBoxUI() {
@Override
protected ComboPopup createPopup() {
BasicComboPopup popup = new BasicComboPopup(comboBox) {
private static final long serialVersionUID = 1L;
@Override
protected Rectangle computePopupBounds(int x, int y, int width, int height) {
int desiredWidth = maxW;
// Hier wird die Breite des Popups auf mindestens maxW gesetzt,
// auch wenn das Control selbst schmaler ist.
return super.computePopupBounds(x, y, Math.max(width, desiredWidth), height);
}
};
return popup;
}
});
JTextField cmbText = (JTextField) nc.getEditor().getEditorComponent();
cmbText.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
cmbText.selectAll();
}
});
nc.setName("fontListe");
nc.setDoubleBuffered(true);
nc.setBounds(forNC);
cmbText.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP) {
index--;
if(index < 0) {;
index = nc.getItemCount()-1;
}
nc.setSelectedIndex(index);
}
else if(e.getKeyCode() == KeyEvent.VK_DOWN) {
e.consume();
nc.setPopupVisible(false);
index++;
if(index >= nc.getItemCount()) {;
index = 0;
}
nc.setSelectedIndex(index);
}
else if(e.getKeyCode() != KeyEvent.VK_SHIFT && e.getKeyCode() != KeyEvent.VK_ALT && e.getKeyCode() != KeyEvent.VK_ALT_GRAPH
&& e.getKeyCode() != KeyEvent.VK_CONTROL && e.getKeyCode() != KeyEvent.VK_END
&& e.getKeyCode() != KeyEvent.VK_HOME) {
//-----------------------------------------------------------------------------------------------
String oT = cmbText.getText();
int searchIndex = 0;
String valCMB = "";
for(Object s: allFonts) {
if(((String) s).startsWith(oT)) {
break;
}
searchIndex++;
}
if(searchIndex >= 0 && searchIndex < nc.getItemCount()) {
index = searchIndex;
nc.setSelectedIndex(searchIndex);
valCMB = ""+nc.getSelectedItem();
}
cmbText.setSelectionStart(oT.length());
cmbText.setSelectionEnd(cmbText.getText().length());
if(valCMB.equals(oT)) {
nc.setPopupVisible(false);
}
else {
nc.setPopupVisible(true);
}
}
}
});
nc.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED){
new Thread(new Runnable() {
@Override
public void run() {
String valFnt = ""+nc.getSelectedItem();
System.out.println("Auswahl geändert!\t"+valFnt);
fontMuster.setFont(new Font(valFnt, lastFont.getStyle(), lastFont.getSize()));
}
}).start();
}
}
});
center.add(nc);
for(Object s: allFonts) {
if(((String) s).startsWith(lastUse)) {
break;
}
index++;
}
if(index >= 0 && index < nc.getItemCount()) {
nc.setSelectedIndex(index);
}
setVisible(true);
System.exit(0);
}
//🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️ Start TestCombo (MAIN) 🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭️🇨🇭
public static void main(String[] args) {
new TestCombo();
}
}