What I'm saying is that you should consider extracting the core logic and data from your console program to create a model class or classes, and then use them in your console program or in a GUI.
For instance say you had a simple console program that got name and birth date information from someone:
class SimpleConsole {
public static void main(String[] args) throws ParseException {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter your name: ");
String name = scanner.nextLine();
System.out.print("Please enter your date of birth as mm/dd/yyyy: ");
String dateString = scanner.nextLine();
Date dateOfBirth = new SimpleDateFormat("MM/dd/yyyy").parse(dateString);
Calendar birthday = Calendar.getInstance();
birthday.setTime(dateOfBirth);
Calendar today = Calendar.getInstance();
int age = today.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
birthday.set(Calendar.YEAR, today.get(Calendar.YEAR));
if (birthday.compareTo(today) > 0) {
age--;
}
System.out.println("Hello, " + name + ". Your age is: " + age);
}
}
First thing I'd do would be to extract the key information and logic from this program and make a class, say called Person, that holds a name String, a dateOfBirth Date field, and a calculateAge() method:
class Person {
String name;
Date dateOfBirth;
public Person(String name, Date dateOfBirth) {
this.name = name;
this.dateOfBirth = dateOfBirth;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public int getAge() {
Calendar birthday = Calendar.getInstance();
birthday.setTime(dateOfBirth);
Calendar today = Calendar.getInstance();
int age = today.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
birthday.set(Calendar.YEAR, today.get(Calendar.YEAR));
if (birthday.compareTo(today) > 0) {
age--;
}
return age;
}
}
and now you can create a console program that uses Person:
class BetterConsole {
public static void main(String[] args) throws ParseException {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter your name: ");
String name = scanner.nextLine();
System.out.print("Please enter your date of birth as mm/dd/yyyy: ");
String dateString = scanner.nextLine();
Date dateOfBirth = new SimpleDateFormat("MM/dd/yyyy").parse(dateString);
Person person = new Person(name, dateOfBirth);
System.out.println("Hello, " + person.getName() + ". Your age is: " + person.getAge());
scanner.close();
}
}
But more importantly, the Person class can be easily used in any type of GUI program that you desire. I must go to sleep so I can't show a GUI now, but may tomorrow.
Ah heck, here's the GUI example:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
public class BirthdayGui extends JPanel {
private static final String PATTERN = "MM/dd/yyyy";
private JTextField nameField = new JTextField(10);
private SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN);
private JFormattedTextField birthDayField = new JFormattedTextField(simpleDateFormat);
private PersonTableModel tableModel = new PersonTableModel();
private JTable table = new JTable(tableModel);
public BirthdayGui() {
setLayout(new BorderLayout());;
add(new JScrollPane(table), BorderLayout.CENTER);
add(createDataEntryPanel(), BorderLayout.PAGE_END);
}
private JPanel createDataEntryPanel() {
birthDayField.setColumns(nameField.getColumns());
JPanel dataInPanel = new JPanel();
dataInPanel.add(new JLabel("Enter Name:"));
dataInPanel.add(nameField);
dataInPanel.add(new JLabel("Enter Birthday as " + PATTERN + ":"));
dataInPanel.add(birthDayField);
dataInPanel.add(new JButton(new AddPersonAction("Add Person", KeyEvent.VK_A)));
return dataInPanel;
}
private class AddPersonAction extends AbstractAction {
public AddPersonAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
Date dateOfBirth = (Date) birthDayField.getValue();
Person person = new Person(name, dateOfBirth);
tableModel.addRow(person);
}
}
private class PersonTableModel extends AbstractTableModel {
public final String[] COL_NAMES = {"Name", "Age"};
List<Person> personList = new ArrayList<>();
@Override
public String getColumnName(int column) {
return COL_NAMES[column];
}
@Override
public int getColumnCount() {
return COL_NAMES.length;
}
@Override
public int getRowCount() {
return personList.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
if (rowIndex < 0 || rowIndex >= getRowCount()) {
throw new ArrayIndexOutOfBoundsException(rowIndex);
}
if (columnIndex < 0 || columnIndex >= getColumnCount()) {
throw new ArrayIndexOutOfBoundsException(columnIndex);
}
Person person = personList.get(rowIndex);
if (columnIndex == 0) {
return person.getName();
} else if (columnIndex == 1) {
return person.getAge();
}
return null;
}
@Override
public java.lang.Class<?> getColumnClass(int column) {
if (column == 0) {
return String.class;
} else if (column == 1) {
return Integer.class;
} else {
return super.getColumnClass(column);
}
};
public void addRow(Person person) {
personList.add(person);
int firstRow = personList.size() - 1;
fireTableRowsInserted(firstRow, firstRow);
}
}
private static void createAndShowGui() {
BirthdayGui mainPanel = new BirthdayGui();
JFrame frame = new JFrame("ConsoleGuiEg");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}