HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Prime Number Calculator with GUI

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
numberguiwithprimecalculator

Problem

I wrote a Java program with a Swing UI that calculates prime numbers. The primes are displayed into a listbox. I used a few optimizations to generate the primes faster. I’m using the NetBeans IDE, so there’s a lot of generated Swing code.

Currently, the program finds as many primes as the user has input into a spinner box. Ideally, I’d like to change the program to include just a simple start/stop button, and the primes are displayed in the list realtime, but that’s for a another day.

The program has 2 classes.

PrimeNumberCalculator.java

```
import javax.swing.DefaultListModel;
import java.text.NumberFormat;
import javax.swing.JOptionPane;
import primenumber.PrimeCalculator;

public class PrimeNumberCalculator extends javax.swing.JFrame {

private int currentIteration = 2; // Stores the next number to be checked

public DefaultListModel numListModel = new DefaultListModel(); // List model for the jList
NumberFormat numberFormatter = NumberFormat.getNumberInstance();

public PrimeNumberCalculator() {
initComponents(); // Do not delete
this.setLocationRelativeTo(null); // Center the window
}

@SuppressWarnings("unchecked")
//
private void initComponents() {

StartButton = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
PrimesList = new javax.swing.JList();
StatusBar = new javax.swing.JPanel();
StatusLabel = new javax.swing.JLabel();
StatusNumber = new javax.swing.JLabel();
PrimeAmountSpinner = new javax.swing.JSpinner();
ClearButton = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Prime Number Calculator");
setMinimumSize(new java.awt.Dimension(250, 250));
setSize(new java.awt.Dimension(300, 400));

StartButton.setFont(StartButton.getFont().deriveFont(StartButton.getFont().getStyle() | java.awt.Fon

Solution

PrimeNumberCalculator and PrimeCalculator; those names are very similar. I can't tell what they do differently just from the name. You should name them based on what they do. PrimeNumberCalculator would be better named something along the lines of PrimeNumberGUI.

I'm just going to review PrimeNumberCalculator.

javax.swing.JButton ...


Please stop using fully-qualified names. You have such a large number of them that the code is hard to read. Instead, import them (NetBeans has a shortcut to automatically import such classes). Also, it's good to organize your imports into some logical order, but don't worry about it if the IDE put them in that order anyway.

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame; // and so on

import java.text.NumberFormat;

import primenumber.PrimeCalculator;


public class PrimeNumberCalculator extends javax.swing.JFrame {


It is considered bad to extend JFrame. For most purposes, you don't even have to extend Swing components. The only time you'd need to is if you want a custom paint method, so just stay away from it. Favor composition over extending.

This is one function:

```
@SuppressWarnings("unchecked")
//
private void initComponents() {

StartButton = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
PrimesList = new javax.swing.JList();
StatusBar = new javax.swing.JPanel();
StatusLabel = new javax.swing.JLabel();
StatusNumber = new javax.swing.JLabel();
PrimeAmountSpinner = new javax.swing.JSpinner();
ClearButton = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Prime Number Calculator");
setMinimumSize(new java.awt.Dimension(250, 250));
setSize(new java.awt.Dimension(300, 400));

StartButton.setFont(StartButton.getFont().deriveFont(StartButton.getFont().getStyle() | java.awt.Font.BOLD));
StartButton.setText("Calculate");
StartButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
StartButtonActionPerformed(evt);
}
});

jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

PrimesList.setFont(PrimesList.getFont());
PrimesList.setModel(numListModel);
jScrollPane1.setViewportView(PrimesList);

StatusLabel.setFont(StatusLabel.getFont());
StatusLabel.setText("Checked: ");
StatusLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

StatusNumber.setFont(StatusNumber.getFont());
StatusNumber.setText("-");

javax.swing.GroupLayout StatusBarLayout = new javax.swing.GroupLayout(StatusBar);
StatusBar.setLayout(StatusBarLayout);
StatusBarLayout.setHorizontalGroup(
StatusBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(StatusBarLayout.createSequentialGroup()
.addContainerGap()
.addComponent(StatusLabel)
.addGap(0, 0, 0)
.addComponent(StatusNumber)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
StatusBarLayout.setVerticalGroup(
StatusBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, StatusBarLayout.createSequentialGroup()
.addContainerGap()
.addGroup(StatusBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(StatusLabel)
.addComponent(StatusNumber))
.addContainerGap())
);

PrimeAmountSpinner.setFont(PrimeAmountSpinner.getFont());
PrimeAmountSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));
PrimeAmountSpinner.setToolTipText("Prime numbers to generate");

ClearButton.setFont(ClearButton.getFont());
ClearButton.setText("Clear");
ClearButton.setEnabled(false);
ClearButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ClearButtonActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()

Code Snippets

javax.swing.JButton ...
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame; // and so on

import java.text.NumberFormat;

import primenumber.PrimeCalculator;
public class PrimeNumberCalculator extends javax.swing.JFrame {
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                       
private void initComponents() {

    StartButton = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    PrimesList = new javax.swing.JList();
    StatusBar = new javax.swing.JPanel();
    StatusLabel = new javax.swing.JLabel();
    StatusNumber = new javax.swing.JLabel();
    PrimeAmountSpinner = new javax.swing.JSpinner();
    ClearButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Prime Number Calculator");
    setMinimumSize(new java.awt.Dimension(250, 250));
    setSize(new java.awt.Dimension(300, 400));

    StartButton.setFont(StartButton.getFont().deriveFont(StartButton.getFont().getStyle() | java.awt.Font.BOLD));
    StartButton.setText("Calculate");
    StartButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            StartButtonActionPerformed(evt);
        }
    });

    jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    PrimesList.setFont(PrimesList.getFont());
    PrimesList.setModel(numListModel);
    jScrollPane1.setViewportView(PrimesList);

    StatusLabel.setFont(StatusLabel.getFont());
    StatusLabel.setText("Checked: ");
    StatusLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

    StatusNumber.setFont(StatusNumber.getFont());
    StatusNumber.setText("-");

    javax.swing.GroupLayout StatusBarLayout = new javax.swing.GroupLayout(StatusBar);
    StatusBar.setLayout(StatusBarLayout);
    StatusBarLayout.setHorizontalGroup(
        StatusBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(StatusBarLayout.createSequentialGroup()
            .addContainerGap()
            .addComponent(StatusLabel)
            .addGap(0, 0, 0)
            .addComponent(StatusNumber)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    StatusBarLayout.setVerticalGroup(
        StatusBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, StatusBarLayout.createSequentialGroup()
            .addContainerGap()
            .addGroup(StatusBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(StatusLabel)
                .addComponent(StatusNumber))
            .addContainerGap())
    );

    PrimeAmountSpinner.setFont(PrimeAmountSpinner.getFont());
    PrimeAmountSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));
    PrimeAmountSpinner.setToolTipText("Prime numbers to generate");

    ClearButton.setFont(ClearButton.getFont());
    ClearButton.setText("Clear");
    ClearButton.setEnabled(false);
    ClearB
private javax.swing.JButton ClearButton;
private javax.swing.JSpinner PrimeAmountSpinner;
private javax.swing.JList PrimesList;
private javax.swing.JButton StartButton;
private javax.swing.JPanel StatusBar;
private javax.swing.JLabel StatusLabel;
private javax.swing.JLabel StatusNumber;

Context

StackExchange Code Review Q#123965, answer score: 2

Revisions (0)

No revisions yet.