From 6d33fb730ee3b10b7c97dac531750fdec26494db Mon Sep 17 00:00:00 2001 From: Niclas Thobaben Date: Wed, 20 Jun 2018 17:20:44 +0200 Subject: [PATCH] JSearchBar WIP --- src/nt/UI/NtUIHelper.java | 20 ++++++++++++++++++++ src/nt/UI/control/JSearchBarController.java | 20 +++++++++++++------- src/nt/UI/control/JSearchTextField.java | 14 ++++---------- src/nt/UI/tests/UITestMain.java | 2 ++ 4 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 src/nt/UI/NtUIHelper.java diff --git a/src/nt/UI/NtUIHelper.java b/src/nt/UI/NtUIHelper.java new file mode 100644 index 0000000..1b1617e --- /dev/null +++ b/src/nt/UI/NtUIHelper.java @@ -0,0 +1,20 @@ +package nt.UI; + +import java.awt.Font; +import java.awt.FontFormatException; +import java.io.File; +import java.io.IOException; +import java.net.URL; + +public class NtUIHelper { + + public static Font getSymbolFont() { + try { + return Font.createFont(Font.TYPE1_FONT, NtUIHelper.class.getResourceAsStream("/nt/UI/LigatureSymbols-2.11.ttf")); + } catch (FontFormatException | IOException e) { + e.printStackTrace(); + } + return Font.getFont("Arial"); + } + +} diff --git a/src/nt/UI/control/JSearchBarController.java b/src/nt/UI/control/JSearchBarController.java index 413e6a0..97388d8 100644 --- a/src/nt/UI/control/JSearchBarController.java +++ b/src/nt/UI/control/JSearchBarController.java @@ -113,7 +113,7 @@ public class JSearchBarController extends KeyAdapter implements DocumentListener @Override public void keyPressed(KeyEvent e) { if(this.searchBar.completionActive) { - JList list = searchBar.dropDown.list; + JList list = searchBar.dropDown.list; int index = list.getSelectedIndex(); if(e.getKeyCode() == KeyEvent.VK_DOWN) { index = index >= list.getModel().getSize()? list.getModel().getSize()-1:++index; @@ -122,6 +122,12 @@ public class JSearchBarController extends KeyAdapter implements DocumentListener } list.setSelectedIndex(index); list.ensureIndexIsVisible(index); + + if(e.getKeyCode()==KeyEvent.VK_ENTER) { + searchBar.txtSearch.setText(list.getSelectedValue()); + System.out.println(list.getSelectedValue()); + System.out.println(searchBar.txtSearch.getText()); + } } } @@ -165,12 +171,12 @@ public class JSearchBarController extends KeyAdapter implements DocumentListener @Override public void valueChanged(ListSelectionEvent e) { - String val = searchBar.dropDown.list.getSelectedValue(); - if(val != null) { - JTextField field = searchBar.txtSearch; - field.setText(val); - field.setCaretPosition(field.getText().length()); - } +// String val = searchBar.dropDown.list.getSelectedValue(); +// if(val != null) { +// JTextField field = searchBar.txtSearch; +// field.setText(val); +// field.setCaretPosition(field.getText().length()); +// } } diff --git a/src/nt/UI/control/JSearchTextField.java b/src/nt/UI/control/JSearchTextField.java index 47ec2e8..11221c6 100644 --- a/src/nt/UI/control/JSearchTextField.java +++ b/src/nt/UI/control/JSearchTextField.java @@ -6,6 +6,8 @@ import javax.swing.border.EmptyBorder; import javax.swing.border.LineBorder; import javax.swing.text.JTextComponent; +import nt.UI.NtUIHelper; + import java.awt.Color; import java.awt.Component; import java.awt.Font; @@ -33,7 +35,7 @@ public class JSearchTextField extends JPanel{ super(); setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); - Font symbols = new Font("LigatureSymbols", Font.PLAIN, 18); + Font symbols = NtUIHelper.getSymbolFont(); horizontalStrut_1 = Box.createHorizontalStrut(20); add(horizontalStrut_1); @@ -44,15 +46,7 @@ public class JSearchTextField extends JPanel{ horizontalStrut = Box.createHorizontalStrut(10); add(horizontalStrut); - txtSearch = new JTextField() { - private static final long serialVersionUID = 1L; - - @Override - public void setText(String txt) { - - } - - }; + txtSearch = new JTextField(); txtSearch.setText("search"); txtSearch.setBorder(new EmptyBorder(0, 0, 0, 0)); txtSearch.setBackground(getBackground()); diff --git a/src/nt/UI/tests/UITestMain.java b/src/nt/UI/tests/UITestMain.java index 690a0eb..a429dab 100644 --- a/src/nt/UI/tests/UITestMain.java +++ b/src/nt/UI/tests/UITestMain.java @@ -5,6 +5,7 @@ import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.plaf.metal.MetalLookAndFeel; +import nt.UI.NtUIHelper; import nt.UI.control.JSearchBar; import nt.UI.control.JSearchTextField; import nt.UI.control.interfaces.SearchBarFinder; @@ -31,6 +32,7 @@ public class UITestMain extends JFrame{ } } + NtUIHelper.getSymbolFont(); UITestMain frame = new UITestMain(); frame.setVisible(true);