I wrote a small Java class with documentation that is going to be a part of a library I am writing. I learned Java as my first programming language from many different sources on the web, and therefore I have developed my own style without any direct criticism along the way. So I would like some feedback on the class because I am unsure of what is good practice and what is just unnecessary. I am thinking mainly of the design and style, especially on the javadoc, but any other corrections or suggestions are also welcome.
Some specific questions:
- Are the
DEFAULT_ACCEPT_TEXT
andDEFAULT_CANCEL_TEXT
variables
overkill? - Should I create methods for common operations on hidden
objects (e.g.setAcceptText()
) or should I just add a method that
returns the object? In this case I did both. - Should I merge the
accept()
andcancel()
methods to something like:setAccepted(boolean accepted)
? Likejava.awt.Window
'sshow()
andhide()
methods were replaced bysetVisible(boolean visible)
.
I realize that a lot of what I am asking is about personal preference and that as long as I am persistent, it is fine.
Test.java
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.sakratt.gutil.ConfirmDialog;
import com.sakratt.gutil.LookAndFeel;
public class Test {
public static void main(String[] args) {
JPanel content = new JPanel(new FlowLayout());
JTextField input = new JTextField(12);
content.add(new JLabel("Enter name:"));
content.add(input);
ConfirmDialog dialog = new ConfirmDialog("Test dialog", content);
boolean yes = dialog.display();
System.out.println(input.getText() + " " + (yes ? "accepted" : "did not accept") + ".");
}
}
ConfirmDialog.java
package com.sakratt.gutil;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
/**
* A dialog that lets the user either confirm or cancel.
*
* Display the dialog to the user by calling the {@link #display()} method. The text on the option
* buttons can be changed by modifying the buttons returned by {@link #getAcceptButton()} and
* {@link #getCancelButton()}.
*
* @author Peter André Johansen
*
*/
public class ConfirmDialog extends JDialog {
/**
* The default accept button text.
*/
private static final String DEFAULT_ACCEPT_TEXT = "Accept";
/**
* The default cancel button text.
*/
private static final String DEFAULT_CANCEL_TEXT = "Cancel";
/**
* Whether the dialog is going to be accepted or not.
*/
private boolean accepted;
/**
* The main component of the dialog.
*/
private JComponent body;
/**
* The accept button.
*/
private JButton acceptButton;
/**
* The cancel button.
*/
private JButton cancelButton;
/**
* Creates a new confirm dialog without a body.
*/
public ConfirmDialog() {
this(null, null, null);
}
/**
* Creates a new confirm dialog without a body.
*
* @title the title
*/
public ConfirmDialog(String title) {
this(title, null, null);
}
/**
* Creates a new confirm dialog.
*
* @title the title
* @param body the main component
*/
public ConfirmDialog(String title, JComponent body) {
this(title, body, null);
}
/**
* Creates a new confirm dialog with a location relative to the parent.
*
* @param title the title
* @param body the main component
* @param parent the parent
*/
public ConfirmDialog(String title, JComponent body, JFrame parent) {
// Accept button
acceptButton = new JButton(DEFAULT_ACCEPT_TEXT);
acceptButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
accept();
}
});
// Cancel button
cancelButton = new JButton(DEFAULT_CANCEL_TEXT);
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
cancel();
}
});
// Add the option buttons
JPanel optionPanel = new JPanel();
optionPanel.add(acceptButton);
optionPanel.add(cancelButton);
// Create the panel
JPanel panel = new JPanel();
panel.setBorder(new EmptyBorder(5, 5, 5, 5));
panel.setLayout(new BorderLayout(0, 5));
if (body != null) panel.add(body, BorderLayout.CENTER);
panel.add(optionPanel, BorderLayout.SOUTH);
// Prepare the dialog
setContentPane(panel);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
getRootPane().setDefaultButton(acceptButton);
pack();
setLocationRelativeTo(parent);
setMinimumSize(getPreferredSize());
setModal(true);
setTitle(title);
}
/**
* Accepts the dialog.
*/
public void accept() {
accepted = true;
dispose();
}
/**
* Cancels the dialog.
*/
public void cancel() {
accepted = false;
dispose();
}
/**
* Display the dialog until it is closed.
*
* @return true if it closed because the accept option was chosen
*/
public boolean display() {
setVisible(true);
return accepted;
}
/**
* Returns the accept button.
*
* @return the button
*/
public JButton getAcceptButton() {
return acceptButton;
}
/**
* Returns the cancel button.
*
* @return the button
*/
public JButton getCancelButton() {
return cancelButton;
}
/**
* Returns the main component.
*
* @return the body
*/
public JComponent getBody() {
return body;
}
/**
* Sets the text of the accept button.
*
* @param text the text
*/
public void setAcceptText(String text) {
acceptButton.setText(text);
}
/**
* Sets the text of the cancel button.
*
* @param text the text
*/
public void setCancelText(String text) {
cancelButton.setText(text);
}
}
DEFAULT_ACCEPT_TEXT, DEFAULT_CANCEL_TEXT
follows java naming convention for constants. – Dark Knight Nov 2 '13 at 13:53