use JList for history instead of JTextField
This commit is contained in:
parent
787cbb6eb4
commit
7f3597e705
|
|
@ -14,6 +14,7 @@ import java.awt.event.*;
|
|||
import java.awt.*;
|
||||
import javax.swing.border.Border;
|
||||
import java.text.DecimalFormat;
|
||||
import java.lang.StringBuilder;
|
||||
|
||||
class RoundBtn implements Border {
|
||||
private int r;
|
||||
|
|
@ -76,17 +77,15 @@ public class GFrame extends JFrame {
|
|||
tf.setFont((new Font("Times New Roman", Font.PLAIN, 20)));
|
||||
tf.setBorder(new RoundBtn(5));
|
||||
|
||||
// History Text Field
|
||||
History history = new History();
|
||||
JTextArea tHist = new JTextArea();
|
||||
tHist.setBounds(380, 50, 200, 240);
|
||||
tHist.setEditable(false);
|
||||
tHist.setBorder(new RoundBtn(5));
|
||||
tHist.setBackground(Color.decode("#2B2B2B"));
|
||||
tHist.setForeground(Color.decode("#e5e5e5"));
|
||||
tHist.setFont((new Font("Times New Roman", Font.PLAIN, 20)));
|
||||
tHist.setBorder(new RoundBtn(5));
|
||||
tHist.setMargin(new Insets(0, 10, 0, 10));
|
||||
// History
|
||||
DefaultListModel<String> histList = new DefaultListModel<>();
|
||||
JList<String> lHist = new JList<>(histList);
|
||||
lHist.setBackground(Color.decode("#2B2B2B"));
|
||||
lHist.setForeground(Color.decode("#e5e5e5"));
|
||||
lHist.setBounds(380, 50, 200, 240);
|
||||
lHist.setFont(new Font("Times New Roman", Font.PLAIN, 20));
|
||||
lHist.setBorder(new RoundBtn(5));
|
||||
this.add(lHist);
|
||||
|
||||
// Buttons
|
||||
JButton bEval = newButton("=", 290, 50, 60, 40);
|
||||
|
|
@ -137,7 +136,6 @@ public class GFrame extends JFrame {
|
|||
this.add(bEight);
|
||||
this.add(bNine);
|
||||
this.add(tf);
|
||||
this.add(tHist);
|
||||
this.setSize(600, 400);
|
||||
this.setLayout(null);
|
||||
this.setVisible(true);
|
||||
|
|
@ -157,16 +155,15 @@ public class GFrame extends JFrame {
|
|||
p = new Parser(tf.getText());
|
||||
String formattedResult = format.format(p.eval());
|
||||
tf.setText(formattedResult);
|
||||
String formattedExpr = expression + " = " + formattedResult;
|
||||
|
||||
tHist.setText("");
|
||||
Vector<String> s = history.getHistory();
|
||||
s.add(expression + " = " + formattedResult);
|
||||
for (int i = 0; i < s.size(); i++) {
|
||||
// tHist.setText(new String(s.get(i).concat(tHist.getText())).concat("\n"));
|
||||
tHist.append(s.get(i));
|
||||
tHist.append("\n");
|
||||
// Dont add to history if its already present in histList
|
||||
for (int i = 0; i < histList.size(); i++) {
|
||||
if (histList.get(i).equals(formattedExpr)) {
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
histList.addElement(formattedExpr);
|
||||
} else {
|
||||
tf.setText("No input");
|
||||
}
|
||||
|
|
@ -201,8 +198,7 @@ public class GFrame extends JFrame {
|
|||
|
||||
bClearHistory.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
history.clearHistory();
|
||||
tHist.setText("");
|
||||
histList.clear();
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -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() {
|
||||
// public void actionPerformed(ActionEvent e) {
|
||||
// tf.setText(tf.getText() + "1");
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue