Krolyxon
Commit 7f3597e

Branches

Refs →

jcal7f3597e

use JList for history instead of JTextField

Author
krolxon
Date
2024-02-18 12:55 +0530
Parent
787cbb6
Commit
7f3597e705371d5eeaf4f27375de5fcb38a63498

+40 −41

2 files changed

  1. M src/GFrame.java +40 −24
  2. D src/History.java +0 −17
M src/GFrame.java View
@@ -14,6 +14,7 @@ import java.awt.event.*;
1414 import java.awt.*;
1515 import javax.swing.border.Border;
1616 import java.text.DecimalFormat;
17+import java.lang.StringBuilder;
1718
1819 class RoundBtn implements Border {
1920 private int r;
@@ -76,21 +77,19 @@ public class GFrame extends JFrame {
7677 tf.setFont((new Font("Times New Roman", Font.PLAIN, 20)));
7778 tf.setBorder(new RoundBtn(5));
7879
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);
9089
9190 // 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);
9493 JButton bClear = newButton("CL", 180, 100, 60, 40);
9594 JButton bClearHistory = newButton("Clear History", 380, 300, 200, 40);
9695 JButton bAdd = newButton("+", 250, 300, 100, 40);
@@ -137,7 +136,6 @@ public class GFrame extends JFrame {
137136 this.add(bEight);
138137 this.add(bNine);
139138 this.add(tf);
140− this.add(tHist);
141139 this.setSize(600, 400);
142140 this.setLayout(null);
143141 this.setVisible(true);
@@ -157,16 +155,15 @@ public class GFrame extends JFrame {
157155 p = new Parser(tf.getText());
158156 String formattedResult = format.format(p.eval());
159157 tf.setText(formattedResult);
158+ String formattedExpr = expression + " = " + formattedResult;
160159
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+ }
168165 }
169−
166+ histList.addElement(formattedExpr);
170167 } else {
171168 tf.setText("No input");
172169 }
@@ -201,8 +198,7 @@ public class GFrame extends JFrame {
201198
202199 bClearHistory.addActionListener(new ActionListener() {
203200 public void actionPerformed(ActionEvent e) {
204− history.clearHistory();
205− tHist.setText("");
201+ histList.clear();
206202 }
207203 });
208204
@@ -213,6 +209,26 @@ public class GFrame extends JFrame {
213209 }
214210 });
215211
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+
216232 // bOne.addActionListener(new ActionListener() {
217233 // public void actionPerformed(ActionEvent e) {
218234 // tf.setText(tf.getText() + "1");
D src/History.java View
@@ -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−}