jcal7f3597e
use JList for history instead of JTextField
+40 −41
2 files changed
- M src/GFrame.java +40 −24
- D src/History.java +0 −17
| @@ -14,6 +14,7 @@ import java.awt.event.*; | |||
| 14 | 14 | import java.awt.*; | |
| 15 | 15 | import javax.swing.border.Border; | |
| 16 | 16 | import java.text.DecimalFormat; | |
| 17 | + | import java.lang.StringBuilder; | |
| 17 | 18 | ||
| 18 | 19 | class RoundBtn implements Border { | |
| 19 | 20 | private int r; | |
| @@ -76,21 +77,19 @@ public class GFrame extends JFrame { | |||
| 76 | 77 | tf.setFont((new Font("Times New Roman", Font.PLAIN, 20))); | |
| 77 | 78 | tf.setBorder(new RoundBtn(5)); | |
| 78 | 79 | ||
| 79 | − | // History Text Field | |
| 80 | − | History history = new History(); | |
| 81 | − | JTextArea tHist = new JTextArea(); | |
| 82 | − | tHist.setBounds(380, 50, 200, 240); | |
| 83 | − | tHist.setEditable(false); | |
| 84 | − | tHist.setBorder(new RoundBtn(5)); | |
| 85 | − | tHist.setBackground(Color.decode("#2B2B2B")); | |
| 86 | − | tHist.setForeground(Color.decode("#e5e5e5")); | |
| 87 | − | tHist.setFont((new Font("Times New Roman", Font.PLAIN, 20))); | |
| 88 | − | tHist.setBorder(new RoundBtn(5)); | |
| 89 | − | tHist.setMargin(new Insets(0, 10, 0, 10)); | |
| 80 | + | // History | |
| 81 | + | DefaultListModel<String> histList = new DefaultListModel<>(); | |
| 82 | + | JList<String> lHist = new JList<>(histList); | |
| 83 | + | lHist.setBackground(Color.decode("#2B2B2B")); | |
| 84 | + | lHist.setForeground(Color.decode("#e5e5e5")); | |
| 85 | + | lHist.setBounds(380, 50, 200, 240); | |
| 86 | + | lHist.setFont(new Font("Times New Roman", Font.PLAIN, 20)); | |
| 87 | + | lHist.setBorder(new RoundBtn(5)); | |
| 88 | + | this.add(lHist); | |
| 90 | 89 | ||
| 91 | 90 | // Buttons | |
| 92 | − | JButton bEval= newButton("=", 290, 50, 60, 40); | |
| 93 | − | JButton bExp= newButton("^", 250, 100, 100, 40); | |
| 91 | + | JButton bEval = newButton("=", 290, 50, 60, 40); | |
| 92 | + | JButton bExp = newButton("^", 250, 100, 100, 40); | |
| 94 | 93 | JButton bClear = newButton("CL", 180, 100, 60, 40); | |
| 95 | 94 | JButton bClearHistory = newButton("Clear History", 380, 300, 200, 40); | |
| 96 | 95 | JButton bAdd = newButton("+", 250, 300, 100, 40); | |
| @@ -137,7 +136,6 @@ public class GFrame extends JFrame { | |||
| 137 | 136 | this.add(bEight); | |
| 138 | 137 | this.add(bNine); | |
| 139 | 138 | this.add(tf); | |
| 140 | − | this.add(tHist); | |
| 141 | 139 | this.setSize(600, 400); | |
| 142 | 140 | this.setLayout(null); | |
| 143 | 141 | this.setVisible(true); | |
| @@ -157,16 +155,15 @@ public class GFrame extends JFrame { | |||
| 157 | 155 | p = new Parser(tf.getText()); | |
| 158 | 156 | String formattedResult = format.format(p.eval()); | |
| 159 | 157 | tf.setText(formattedResult); | |
| 158 | + | String formattedExpr = expression + " = " + formattedResult; | |
| 160 | 159 | ||
| 161 | − | tHist.setText(""); | |
| 162 | − | Vector<String> s = history.getHistory(); | |
| 163 | − | s.add(expression + " = " + formattedResult); | |
| 164 | − | for (int i = 0; i < s.size(); i++) { | |
| 165 | − | // tHist.setText(new String(s.get(i).concat(tHist.getText())).concat("\n")); | |
| 166 | − | tHist.append(s.get(i)); | |
| 167 | − | tHist.append("\n"); | |
| 160 | + | // Dont add to history if its already present in histList | |
| 161 | + | for (int i = 0; i < histList.size(); i++) { | |
| 162 | + | if (histList.get(i).equals(formattedExpr)) { | |
| 163 | + | return; | |
| 164 | + | } | |
| 168 | 165 | } | |
| 169 | − | ||
| 166 | + | histList.addElement(formattedExpr); | |
| 170 | 167 | } else { | |
| 171 | 168 | tf.setText("No input"); | |
| 172 | 169 | } | |
| @@ -201,8 +198,7 @@ public class GFrame extends JFrame { | |||
| 201 | 198 | ||
| 202 | 199 | bClearHistory.addActionListener(new ActionListener() { | |
| 203 | 200 | public void actionPerformed(ActionEvent e) { | |
| 204 | − | history.clearHistory(); | |
| 205 | − | tHist.setText(""); | |
| 201 | + | histList.clear(); | |
| 206 | 202 | } | |
| 207 | 203 | }); | |
| 208 | 204 | ||
| @@ -213,6 +209,26 @@ public class GFrame extends JFrame { | |||
| 213 | 209 | } | |
| 214 | 210 | }); | |
| 215 | 211 | ||
| 212 | + | lHist.addMouseListener(new MouseAdapter() { | |
| 213 | + | public void mouseClicked(MouseEvent evt) { | |
| 214 | + | JList list = (JList) evt.getSource(); | |
| 215 | + | if (evt.getClickCount() == 2) { | |
| 216 | + | // Double-click detected | |
| 217 | + | int index = list.locationToIndex(evt.getPoint()); | |
| 218 | + | String expr = histList.get(index); | |
| 219 | + | StringBuilder str = new StringBuilder(""); | |
| 220 | + | int endIndex = 0; | |
| 221 | + | for (int i = 0; i < expr.length(); i++) { | |
| 222 | + | if (expr.charAt(i) == '=') { | |
| 223 | + | break; | |
| 224 | + | } | |
| 225 | + | endIndex++; | |
| 226 | + | } | |
| 227 | + | tf.setText(expr.substring(0, endIndex)); | |
| 228 | + | } | |
| 229 | + | } | |
| 230 | + | }); | |
| 231 | + | ||
| 216 | 232 | // bOne.addActionListener(new ActionListener() { | |
| 217 | 233 | // public void actionPerformed(ActionEvent e) { | |
| 218 | 234 | // tf.setText(tf.getText() + "1"); | |
| @@ -1,17 +0,0 @@ | |||
| 1 | − | import java.util.Vector; | |
| 2 | − | ||
| 3 | − | public class History { | |
| 4 | − | private Vector<String> hist; | |
| 5 | − | ||
| 6 | − | History() { | |
| 7 | − | hist = new Vector<String>(); | |
| 8 | − | } | |
| 9 | − | ||
| 10 | − | public void clearHistory() { | |
| 11 | − | hist.clear(); | |
| 12 | − | } | |
| 13 | − | ||
| 14 | − | public Vector<String> getHistory() { | |
| 15 | − | return hist; | |
| 16 | − | } | |
| 17 | − | } | |