My first Java app: a calculator in Swing
up vote
1
down vote
favorite
I have created my first Java app. I am very familiar with C++ and thus without reading any books (just introduction, googling and IntelliJ auto-completion) I directly jumped into Java.
So I made a calculator.
Main.java:
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.*;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.KeyEvent;
class Main extends JFrame {
public static void main(String args) throws AWTException {
System.out.println("init");
var app = new Main();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
private JButton numButtons = new JButton[10];
private JButton opeButtons = new JButton[5];
private JButton lb = new JButton("(");
private JButton rb = new JButton(")");
private JButton cb = new JButton("C");
private JButton decb = new JButton(".");
private JTextField text = new JTextField();
private Robot keyboard = new Robot();
private ScriptEngine jsEng;
public Main() throws AWTException {
super();
jsEng = (new ScriptEngineManager()).getEngineByName("js");
var textDoc = (PlainDocument)text.getDocument();
textDoc.setDocumentFilter(new EquationFilter());
initGui();
for(JButton i : numButtons) {
i.addActionListener(e -> simKey(i.getText().charAt(0)));
}
for(int i = 0;i<4;i++) {
int finalI = i;
opeButtons[i].addActionListener(e -> simKey(opeButtons[finalI].getText().charAt(0)));
}
decb.addActionListener(e -> simKey(decb.getText().charAt(0)));
rb.addActionListener(e -> simKey(rb.getText().charAt(0)));
lb.addActionListener(e -> simKey(lb.getText().charAt(0)));
cb.addActionListener(e-> text.setText(""));
opeButtons[4].addActionListener(e -> {
try {
text.setText(jsEng.eval(text.getText()).toString());
} catch (ScriptException e1) {
e1.printStackTrace();
}
});
}
private void simKey(char code){
text.requestFocus();
keyboard.keyPress(KeyEvent.VK_SHIFT);
switch (code) {
case '+':
keyboard.keyPress(KeyEvent.VK_EQUALS);
keyboard.keyRelease(KeyEvent.VK_EQUALS);
break;
case '(':
keyboard.keyPress(KeyEvent.VK_LEFT_PARENTHESIS);
keyboard.keyRelease(KeyEvent.VK_LEFT_PARENTHESIS);
break;
case ')':
keyboard.keyPress(KeyEvent.VK_RIGHT_PARENTHESIS);
keyboard.keyRelease(KeyEvent.VK_RIGHT_PARENTHESIS);
break;
case '*':
keyboard.keyPress('8');
keyboard.keyRelease('8');
break;
default:
keyboard.keyRelease(KeyEvent.VK_SHIFT);
keyboard.keyPress(code);
keyboard.keyRelease(code);
return;
}
keyboard.keyRelease(KeyEvent.VK_SHIFT);
}
private void initGui(){
setTitle("Calculator");
setSize(400,500);
var grid = new GridBagLayout();
setLayout(grid);
GridBagConstraints gridCon = new GridBagConstraints();
gridCon.weightx = gridCon.weighty = 1.0;
gridCon.gridy = 0;
gridCon.gridx = 0;
gridCon.gridwidth = 4;
gridCon.fill = gridCon.BOTH;
add(text,gridCon);
gridCon.gridwidth = 1 ;
String names = {"+","-","/","*","="};
for(int i = 0;i < 5; i++){
opeButtons[i] = new JButton(names[i]);
}
gridCon.gridx = 3;
for( int y = 1; y < 6; y++ ){
gridCon.gridy = y;
add(opeButtons[y-1],gridCon);
}
for(int y = 2, i = 1; y < 5; y++ ){
for(int x = 0 ; x < 3; x++,i++) {
gridCon.gridx = x;
gridCon.gridy = y;
numButtons[i] = new JButton(Integer.toString(i));
add(numButtons[i],gridCon);
}
}
gridCon.gridx = 0;
gridCon.gridy = 1;
add(lb,gridCon);
gridCon.gridx = 1;
add(rb,gridCon);
gridCon.gridx = 2;
add(cb,gridCon);
numButtons[0] = new JButton("0");
gridCon.gridx = 0;
gridCon.gridy = 5;
gridCon.gridwidth = 2;
add(numButtons[0],gridCon);
gridCon.gridwidth = 1;
gridCon.gridx = 2;
add(decb,gridCon);
}
}
EquationFilter.java:
import javax.swing.text.AttributeSet;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;
import javax.swing.text.BadLocationException;
import java.awt.event.KeyEvent;
import java.security.Key;
public class EquationFilter extends DocumentFilter {
@Override
public void insertString(FilterBypass fb, int offset, String string,
AttributeSet attr) throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.insert(offset, string);
if (test(sb.toString())) {
super.insertString(fb, offset, string, attr);
}
}
private boolean test(String text) {
char validInput = {'1','2','3','4','5','6','7','8','9','0','.','/','*','-','+','(',')',' ','','b'};
if(text.isEmpty())
return true;
for(char i : validInput){
if(i == text.charAt(text.length()-1))
return true;
}
return false;
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text,
AttributeSet attrs) throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.replace(offset, offset + length, text);
if (test(sb.toString())) {
super.replace(fb, offset, length, text, attrs);
}
}
@Override
public void remove(FilterBypass fb, int offset, int length)
throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.delete(offset, offset + length);
try {
if (test(sb.toString())) {
super.remove(fb, offset, length);
}
}
catch (StringIndexOutOfBoundsException e) {
super.remove(fb,0,1);
}
}
}
https://gitlab.com/SmitTheTux/javacalculator
Please suggest me improvments.
java beginner calculator swing
New contributor
SmitTheLastFirefoxUser is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
1
down vote
favorite
I have created my first Java app. I am very familiar with C++ and thus without reading any books (just introduction, googling and IntelliJ auto-completion) I directly jumped into Java.
So I made a calculator.
Main.java:
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.*;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.KeyEvent;
class Main extends JFrame {
public static void main(String args) throws AWTException {
System.out.println("init");
var app = new Main();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
private JButton numButtons = new JButton[10];
private JButton opeButtons = new JButton[5];
private JButton lb = new JButton("(");
private JButton rb = new JButton(")");
private JButton cb = new JButton("C");
private JButton decb = new JButton(".");
private JTextField text = new JTextField();
private Robot keyboard = new Robot();
private ScriptEngine jsEng;
public Main() throws AWTException {
super();
jsEng = (new ScriptEngineManager()).getEngineByName("js");
var textDoc = (PlainDocument)text.getDocument();
textDoc.setDocumentFilter(new EquationFilter());
initGui();
for(JButton i : numButtons) {
i.addActionListener(e -> simKey(i.getText().charAt(0)));
}
for(int i = 0;i<4;i++) {
int finalI = i;
opeButtons[i].addActionListener(e -> simKey(opeButtons[finalI].getText().charAt(0)));
}
decb.addActionListener(e -> simKey(decb.getText().charAt(0)));
rb.addActionListener(e -> simKey(rb.getText().charAt(0)));
lb.addActionListener(e -> simKey(lb.getText().charAt(0)));
cb.addActionListener(e-> text.setText(""));
opeButtons[4].addActionListener(e -> {
try {
text.setText(jsEng.eval(text.getText()).toString());
} catch (ScriptException e1) {
e1.printStackTrace();
}
});
}
private void simKey(char code){
text.requestFocus();
keyboard.keyPress(KeyEvent.VK_SHIFT);
switch (code) {
case '+':
keyboard.keyPress(KeyEvent.VK_EQUALS);
keyboard.keyRelease(KeyEvent.VK_EQUALS);
break;
case '(':
keyboard.keyPress(KeyEvent.VK_LEFT_PARENTHESIS);
keyboard.keyRelease(KeyEvent.VK_LEFT_PARENTHESIS);
break;
case ')':
keyboard.keyPress(KeyEvent.VK_RIGHT_PARENTHESIS);
keyboard.keyRelease(KeyEvent.VK_RIGHT_PARENTHESIS);
break;
case '*':
keyboard.keyPress('8');
keyboard.keyRelease('8');
break;
default:
keyboard.keyRelease(KeyEvent.VK_SHIFT);
keyboard.keyPress(code);
keyboard.keyRelease(code);
return;
}
keyboard.keyRelease(KeyEvent.VK_SHIFT);
}
private void initGui(){
setTitle("Calculator");
setSize(400,500);
var grid = new GridBagLayout();
setLayout(grid);
GridBagConstraints gridCon = new GridBagConstraints();
gridCon.weightx = gridCon.weighty = 1.0;
gridCon.gridy = 0;
gridCon.gridx = 0;
gridCon.gridwidth = 4;
gridCon.fill = gridCon.BOTH;
add(text,gridCon);
gridCon.gridwidth = 1 ;
String names = {"+","-","/","*","="};
for(int i = 0;i < 5; i++){
opeButtons[i] = new JButton(names[i]);
}
gridCon.gridx = 3;
for( int y = 1; y < 6; y++ ){
gridCon.gridy = y;
add(opeButtons[y-1],gridCon);
}
for(int y = 2, i = 1; y < 5; y++ ){
for(int x = 0 ; x < 3; x++,i++) {
gridCon.gridx = x;
gridCon.gridy = y;
numButtons[i] = new JButton(Integer.toString(i));
add(numButtons[i],gridCon);
}
}
gridCon.gridx = 0;
gridCon.gridy = 1;
add(lb,gridCon);
gridCon.gridx = 1;
add(rb,gridCon);
gridCon.gridx = 2;
add(cb,gridCon);
numButtons[0] = new JButton("0");
gridCon.gridx = 0;
gridCon.gridy = 5;
gridCon.gridwidth = 2;
add(numButtons[0],gridCon);
gridCon.gridwidth = 1;
gridCon.gridx = 2;
add(decb,gridCon);
}
}
EquationFilter.java:
import javax.swing.text.AttributeSet;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;
import javax.swing.text.BadLocationException;
import java.awt.event.KeyEvent;
import java.security.Key;
public class EquationFilter extends DocumentFilter {
@Override
public void insertString(FilterBypass fb, int offset, String string,
AttributeSet attr) throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.insert(offset, string);
if (test(sb.toString())) {
super.insertString(fb, offset, string, attr);
}
}
private boolean test(String text) {
char validInput = {'1','2','3','4','5','6','7','8','9','0','.','/','*','-','+','(',')',' ','','b'};
if(text.isEmpty())
return true;
for(char i : validInput){
if(i == text.charAt(text.length()-1))
return true;
}
return false;
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text,
AttributeSet attrs) throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.replace(offset, offset + length, text);
if (test(sb.toString())) {
super.replace(fb, offset, length, text, attrs);
}
}
@Override
public void remove(FilterBypass fb, int offset, int length)
throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.delete(offset, offset + length);
try {
if (test(sb.toString())) {
super.remove(fb, offset, length);
}
}
catch (StringIndexOutOfBoundsException e) {
super.remove(fb,0,1);
}
}
}
https://gitlab.com/SmitTheTux/javacalculator
Please suggest me improvments.
java beginner calculator swing
New contributor
SmitTheLastFirefoxUser is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
It looks to me that thetestmethod doesn't do what one might expect. As it is, it only checks if the last character of the input string is valid. It seems to me that one would want to check that all the characters of the input string are valid. Is the current behavior what you expected?
– tinstaafl
9 hours ago
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have created my first Java app. I am very familiar with C++ and thus without reading any books (just introduction, googling and IntelliJ auto-completion) I directly jumped into Java.
So I made a calculator.
Main.java:
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.*;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.KeyEvent;
class Main extends JFrame {
public static void main(String args) throws AWTException {
System.out.println("init");
var app = new Main();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
private JButton numButtons = new JButton[10];
private JButton opeButtons = new JButton[5];
private JButton lb = new JButton("(");
private JButton rb = new JButton(")");
private JButton cb = new JButton("C");
private JButton decb = new JButton(".");
private JTextField text = new JTextField();
private Robot keyboard = new Robot();
private ScriptEngine jsEng;
public Main() throws AWTException {
super();
jsEng = (new ScriptEngineManager()).getEngineByName("js");
var textDoc = (PlainDocument)text.getDocument();
textDoc.setDocumentFilter(new EquationFilter());
initGui();
for(JButton i : numButtons) {
i.addActionListener(e -> simKey(i.getText().charAt(0)));
}
for(int i = 0;i<4;i++) {
int finalI = i;
opeButtons[i].addActionListener(e -> simKey(opeButtons[finalI].getText().charAt(0)));
}
decb.addActionListener(e -> simKey(decb.getText().charAt(0)));
rb.addActionListener(e -> simKey(rb.getText().charAt(0)));
lb.addActionListener(e -> simKey(lb.getText().charAt(0)));
cb.addActionListener(e-> text.setText(""));
opeButtons[4].addActionListener(e -> {
try {
text.setText(jsEng.eval(text.getText()).toString());
} catch (ScriptException e1) {
e1.printStackTrace();
}
});
}
private void simKey(char code){
text.requestFocus();
keyboard.keyPress(KeyEvent.VK_SHIFT);
switch (code) {
case '+':
keyboard.keyPress(KeyEvent.VK_EQUALS);
keyboard.keyRelease(KeyEvent.VK_EQUALS);
break;
case '(':
keyboard.keyPress(KeyEvent.VK_LEFT_PARENTHESIS);
keyboard.keyRelease(KeyEvent.VK_LEFT_PARENTHESIS);
break;
case ')':
keyboard.keyPress(KeyEvent.VK_RIGHT_PARENTHESIS);
keyboard.keyRelease(KeyEvent.VK_RIGHT_PARENTHESIS);
break;
case '*':
keyboard.keyPress('8');
keyboard.keyRelease('8');
break;
default:
keyboard.keyRelease(KeyEvent.VK_SHIFT);
keyboard.keyPress(code);
keyboard.keyRelease(code);
return;
}
keyboard.keyRelease(KeyEvent.VK_SHIFT);
}
private void initGui(){
setTitle("Calculator");
setSize(400,500);
var grid = new GridBagLayout();
setLayout(grid);
GridBagConstraints gridCon = new GridBagConstraints();
gridCon.weightx = gridCon.weighty = 1.0;
gridCon.gridy = 0;
gridCon.gridx = 0;
gridCon.gridwidth = 4;
gridCon.fill = gridCon.BOTH;
add(text,gridCon);
gridCon.gridwidth = 1 ;
String names = {"+","-","/","*","="};
for(int i = 0;i < 5; i++){
opeButtons[i] = new JButton(names[i]);
}
gridCon.gridx = 3;
for( int y = 1; y < 6; y++ ){
gridCon.gridy = y;
add(opeButtons[y-1],gridCon);
}
for(int y = 2, i = 1; y < 5; y++ ){
for(int x = 0 ; x < 3; x++,i++) {
gridCon.gridx = x;
gridCon.gridy = y;
numButtons[i] = new JButton(Integer.toString(i));
add(numButtons[i],gridCon);
}
}
gridCon.gridx = 0;
gridCon.gridy = 1;
add(lb,gridCon);
gridCon.gridx = 1;
add(rb,gridCon);
gridCon.gridx = 2;
add(cb,gridCon);
numButtons[0] = new JButton("0");
gridCon.gridx = 0;
gridCon.gridy = 5;
gridCon.gridwidth = 2;
add(numButtons[0],gridCon);
gridCon.gridwidth = 1;
gridCon.gridx = 2;
add(decb,gridCon);
}
}
EquationFilter.java:
import javax.swing.text.AttributeSet;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;
import javax.swing.text.BadLocationException;
import java.awt.event.KeyEvent;
import java.security.Key;
public class EquationFilter extends DocumentFilter {
@Override
public void insertString(FilterBypass fb, int offset, String string,
AttributeSet attr) throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.insert(offset, string);
if (test(sb.toString())) {
super.insertString(fb, offset, string, attr);
}
}
private boolean test(String text) {
char validInput = {'1','2','3','4','5','6','7','8','9','0','.','/','*','-','+','(',')',' ','','b'};
if(text.isEmpty())
return true;
for(char i : validInput){
if(i == text.charAt(text.length()-1))
return true;
}
return false;
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text,
AttributeSet attrs) throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.replace(offset, offset + length, text);
if (test(sb.toString())) {
super.replace(fb, offset, length, text, attrs);
}
}
@Override
public void remove(FilterBypass fb, int offset, int length)
throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.delete(offset, offset + length);
try {
if (test(sb.toString())) {
super.remove(fb, offset, length);
}
}
catch (StringIndexOutOfBoundsException e) {
super.remove(fb,0,1);
}
}
}
https://gitlab.com/SmitTheTux/javacalculator
Please suggest me improvments.
java beginner calculator swing
New contributor
SmitTheLastFirefoxUser is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have created my first Java app. I am very familiar with C++ and thus without reading any books (just introduction, googling and IntelliJ auto-completion) I directly jumped into Java.
So I made a calculator.
Main.java:
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.*;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.KeyEvent;
class Main extends JFrame {
public static void main(String args) throws AWTException {
System.out.println("init");
var app = new Main();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
private JButton numButtons = new JButton[10];
private JButton opeButtons = new JButton[5];
private JButton lb = new JButton("(");
private JButton rb = new JButton(")");
private JButton cb = new JButton("C");
private JButton decb = new JButton(".");
private JTextField text = new JTextField();
private Robot keyboard = new Robot();
private ScriptEngine jsEng;
public Main() throws AWTException {
super();
jsEng = (new ScriptEngineManager()).getEngineByName("js");
var textDoc = (PlainDocument)text.getDocument();
textDoc.setDocumentFilter(new EquationFilter());
initGui();
for(JButton i : numButtons) {
i.addActionListener(e -> simKey(i.getText().charAt(0)));
}
for(int i = 0;i<4;i++) {
int finalI = i;
opeButtons[i].addActionListener(e -> simKey(opeButtons[finalI].getText().charAt(0)));
}
decb.addActionListener(e -> simKey(decb.getText().charAt(0)));
rb.addActionListener(e -> simKey(rb.getText().charAt(0)));
lb.addActionListener(e -> simKey(lb.getText().charAt(0)));
cb.addActionListener(e-> text.setText(""));
opeButtons[4].addActionListener(e -> {
try {
text.setText(jsEng.eval(text.getText()).toString());
} catch (ScriptException e1) {
e1.printStackTrace();
}
});
}
private void simKey(char code){
text.requestFocus();
keyboard.keyPress(KeyEvent.VK_SHIFT);
switch (code) {
case '+':
keyboard.keyPress(KeyEvent.VK_EQUALS);
keyboard.keyRelease(KeyEvent.VK_EQUALS);
break;
case '(':
keyboard.keyPress(KeyEvent.VK_LEFT_PARENTHESIS);
keyboard.keyRelease(KeyEvent.VK_LEFT_PARENTHESIS);
break;
case ')':
keyboard.keyPress(KeyEvent.VK_RIGHT_PARENTHESIS);
keyboard.keyRelease(KeyEvent.VK_RIGHT_PARENTHESIS);
break;
case '*':
keyboard.keyPress('8');
keyboard.keyRelease('8');
break;
default:
keyboard.keyRelease(KeyEvent.VK_SHIFT);
keyboard.keyPress(code);
keyboard.keyRelease(code);
return;
}
keyboard.keyRelease(KeyEvent.VK_SHIFT);
}
private void initGui(){
setTitle("Calculator");
setSize(400,500);
var grid = new GridBagLayout();
setLayout(grid);
GridBagConstraints gridCon = new GridBagConstraints();
gridCon.weightx = gridCon.weighty = 1.0;
gridCon.gridy = 0;
gridCon.gridx = 0;
gridCon.gridwidth = 4;
gridCon.fill = gridCon.BOTH;
add(text,gridCon);
gridCon.gridwidth = 1 ;
String names = {"+","-","/","*","="};
for(int i = 0;i < 5; i++){
opeButtons[i] = new JButton(names[i]);
}
gridCon.gridx = 3;
for( int y = 1; y < 6; y++ ){
gridCon.gridy = y;
add(opeButtons[y-1],gridCon);
}
for(int y = 2, i = 1; y < 5; y++ ){
for(int x = 0 ; x < 3; x++,i++) {
gridCon.gridx = x;
gridCon.gridy = y;
numButtons[i] = new JButton(Integer.toString(i));
add(numButtons[i],gridCon);
}
}
gridCon.gridx = 0;
gridCon.gridy = 1;
add(lb,gridCon);
gridCon.gridx = 1;
add(rb,gridCon);
gridCon.gridx = 2;
add(cb,gridCon);
numButtons[0] = new JButton("0");
gridCon.gridx = 0;
gridCon.gridy = 5;
gridCon.gridwidth = 2;
add(numButtons[0],gridCon);
gridCon.gridwidth = 1;
gridCon.gridx = 2;
add(decb,gridCon);
}
}
EquationFilter.java:
import javax.swing.text.AttributeSet;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;
import javax.swing.text.BadLocationException;
import java.awt.event.KeyEvent;
import java.security.Key;
public class EquationFilter extends DocumentFilter {
@Override
public void insertString(FilterBypass fb, int offset, String string,
AttributeSet attr) throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.insert(offset, string);
if (test(sb.toString())) {
super.insertString(fb, offset, string, attr);
}
}
private boolean test(String text) {
char validInput = {'1','2','3','4','5','6','7','8','9','0','.','/','*','-','+','(',')',' ','','b'};
if(text.isEmpty())
return true;
for(char i : validInput){
if(i == text.charAt(text.length()-1))
return true;
}
return false;
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text,
AttributeSet attrs) throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.replace(offset, offset + length, text);
if (test(sb.toString())) {
super.replace(fb, offset, length, text, attrs);
}
}
@Override
public void remove(FilterBypass fb, int offset, int length)
throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.delete(offset, offset + length);
try {
if (test(sb.toString())) {
super.remove(fb, offset, length);
}
}
catch (StringIndexOutOfBoundsException e) {
super.remove(fb,0,1);
}
}
}
https://gitlab.com/SmitTheTux/javacalculator
Please suggest me improvments.
java beginner calculator swing
java beginner calculator swing
New contributor
SmitTheLastFirefoxUser is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
SmitTheLastFirefoxUser is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 8 hours ago
200_success
128k15149412
128k15149412
New contributor
SmitTheLastFirefoxUser is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 10 hours ago
SmitTheLastFirefoxUser
1064
1064
New contributor
SmitTheLastFirefoxUser is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
SmitTheLastFirefoxUser is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
SmitTheLastFirefoxUser is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
It looks to me that thetestmethod doesn't do what one might expect. As it is, it only checks if the last character of the input string is valid. It seems to me that one would want to check that all the characters of the input string are valid. Is the current behavior what you expected?
– tinstaafl
9 hours ago
add a comment |
It looks to me that thetestmethod doesn't do what one might expect. As it is, it only checks if the last character of the input string is valid. It seems to me that one would want to check that all the characters of the input string are valid. Is the current behavior what you expected?
– tinstaafl
9 hours ago
It looks to me that the
test method doesn't do what one might expect. As it is, it only checks if the last character of the input string is valid. It seems to me that one would want to check that all the characters of the input string are valid. Is the current behavior what you expected?– tinstaafl
9 hours ago
It looks to me that the
test method doesn't do what one might expect. As it is, it only checks if the last character of the input string is valid. It seems to me that one would want to check that all the characters of the input string are valid. Is the current behavior what you expected?– tinstaafl
9 hours ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
SmitTheLastFirefoxUser is a new contributor. Be nice, and check out our Code of Conduct.
SmitTheLastFirefoxUser is a new contributor. Be nice, and check out our Code of Conduct.
SmitTheLastFirefoxUser is a new contributor. Be nice, and check out our Code of Conduct.
SmitTheLastFirefoxUser is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209449%2fmy-first-java-app-a-calculator-in-swing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It looks to me that the
testmethod doesn't do what one might expect. As it is, it only checks if the last character of the input string is valid. It seems to me that one would want to check that all the characters of the input string are valid. Is the current behavior what you expected?– tinstaafl
9 hours ago