use JList for history instead of JTextField

This commit is contained in:
krolxon 2024-02-18 12:55:39 +05:30
parent 787cbb6eb4
commit 7f3597e705
2 changed files with 40 additions and 41 deletions

View File

@ -14,6 +14,7 @@ import java.awt.event.*;
import java.awt.*; import java.awt.*;
import javax.swing.border.Border; import javax.swing.border.Border;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.lang.StringBuilder;
class RoundBtn implements Border { class RoundBtn implements Border {
private int r; private int r;
@ -76,17 +77,15 @@ public class GFrame extends JFrame {
tf.setFont((new Font("Times New Roman", Font.PLAIN, 20))); tf.setFont((new Font("Times New Roman", Font.PLAIN, 20)));
tf.setBorder(new RoundBtn(5)); tf.setBorder(new RoundBtn(5));
// History Text Field // History
History history = new History(); DefaultListModel<String> histList = new DefaultListModel<>();
JTextArea tHist = new JTextArea(); JList<String> lHist = new JList<>(histList);
tHist.setBounds(380, 50, 200, 240); lHist.setBackground(Color.decode("#2B2B2B"));
tHist.setEditable(false); lHist.setForeground(Color.decode("#e5e5e5"));
tHist.setBorder(new RoundBtn(5)); lHist.setBounds(380, 50, 200, 240);
tHist.setBackground(Color.decode("#2B2B2B")); lHist.setFont(new Font("Times New Roman", Font.PLAIN, 20));
tHist.setForeground(Color.decode("#e5e5e5")); lHist.setBorder(new RoundBtn(5));
tHist.setFont((new Font("Times New Roman", Font.PLAIN, 20))); this.add(lHist);
tHist.setBorder(new RoundBtn(5));
tHist.setMargin(new Insets(0, 10, 0, 10));
// Buttons // Buttons
JButton bEval = newButton("=", 290, 50, 60, 40); JButton bEval = newButton("=", 290, 50, 60, 40);
@ -137,7 +136,6 @@ public class GFrame extends JFrame {
this.add(bEight); this.add(bEight);
this.add(bNine); this.add(bNine);
this.add(tf); this.add(tf);
this.add(tHist);
this.setSize(600, 400); this.setSize(600, 400);
this.setLayout(null); this.setLayout(null);
this.setVisible(true); this.setVisible(true);
@ -157,16 +155,15 @@ public class GFrame extends JFrame {
p = new Parser(tf.getText()); p = new Parser(tf.getText());
String formattedResult = format.format(p.eval()); String formattedResult = format.format(p.eval());
tf.setText(formattedResult); tf.setText(formattedResult);
String formattedExpr = expression + " = " + formattedResult;
tHist.setText(""); // Dont add to history if its already present in histList
Vector<String> s = history.getHistory(); for (int i = 0; i < histList.size(); i++) {
s.add(expression + " = " + formattedResult); if (histList.get(i).equals(formattedExpr)) {
for (int i = 0; i < s.size(); i++) { return;
// tHist.setText(new String(s.get(i).concat(tHist.getText())).concat("\n"));
tHist.append(s.get(i));
tHist.append("\n");
} }
}
histList.addElement(formattedExpr);
} else { } else {
tf.setText("No input"); tf.setText("No input");
} }
@ -201,8 +198,7 @@ public class GFrame extends JFrame {
bClearHistory.addActionListener(new ActionListener() { bClearHistory.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
history.clearHistory(); histList.clear();
tHist.setText("");
} }
}); });
@ -213,6 +209,26 @@ public class GFrame extends JFrame {
} }
}); });
lHist.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
JList list = (JList) evt.getSource();
if (evt.getClickCount() == 2) {
// Double-click detected
int index = list.locationToIndex(evt.getPoint());
String expr = histList.get(index);
StringBuilder str = new StringBuilder("");
int endIndex = 0;
for (int i = 0; i < expr.length(); i++) {
if (expr.charAt(i) == '=') {
break;
}
endIndex++;
}
tf.setText(expr.substring(0, endIndex));
}
}
});
// bOne.addActionListener(new ActionListener() { // bOne.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent e) { // public void actionPerformed(ActionEvent e) {
// tf.setText(tf.getText() + "1"); // tf.setText(tf.getText() + "1");

View File

@ -1,17 +0,0 @@
import java.util.Vector;
public class History {
private Vector<String> hist;
History() {
hist = new Vector<String>();
}
public void clearHistory() {
hist.clear();
}
public Vector<String> getHistory() {
return hist;
}
}