hello..
uuhhmm ... i have a problem here..
i want to know how can i do that
if the tuition paid "yes/no": "N" .. then message_error exist that "you need to pay first.." ,. and dont print the result,,
but if the answer is "Y" then .. it proceed to result..
its a running program .. please debug it..
please help me..
need it ASAP .. thanks..
/**panchitz trial program.. still error**/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class Panchita extends JPanel implements ActionListener,
FocusListener {
JTextField studentField, addressField, paidField;
JFormattedTextField stu_idField;
JSpinner courseSpinner;
boolean addressSet = false;
Font regularFont, italicFont;
JLabel addressDisplay;
final static int GAP = 10;
// do { // loop sa do while (still error)
public Panchita() {
setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
JPanel leftHalf = new JPanel() {
public Dimension getMaximumSize() {
Dimension pref = getPreferredSize();
return new Dimension(Integer.MAX_VALUE,
pref.height);
}
};
leftHalf.setLayout(new BoxLayout(leftHalf,
BoxLayout.PAGE_AXIS));
leftHalf.add(createEntryFields());
leftHalf.add(createButtons());
add(leftHalf);
add(createAddressDisplay());
}
protected JComponent createButtons() {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
JButton button = new JButton("Print");
button.addActionListener(this);
panel.add(button);
button = new JButton("Clear");
button.addActionListener(this);
button.setActionCommand("clear");
panel.add(button);
panel.setBorder(BorderFactory.createEmptyBorder(0, 0,
GAP-5, GAP-5));
return panel;
}
public void actionPerformed(ActionEvent e) {
if ("clear".equals(e.getActionCommand())) {
addressSet = false;
studentField.setText("");
addressField.setText("");
paidField.setText(""); //paid
//set ng text
stu_idField.setValue(null);
} else {
addressSet = true;
}
updateDisplays();
}
protected void updateDisplays() {
addressDisplay.setText(formatAddress());
if (addressSet) {
addressDisplay.setFont(regularFont);
} else {
addressDisplay.setFont(italicFont);
}
}
protected JComponent createAddressDisplay() {
JPanel panel = new JPanel(new BorderLayout());
addressDisplay = new JLabel();
addressDisplay.setHorizontalAlignment(JLabel.CENTER);
regularFont = addressDisplay.getFont().deriveFont(Font.PLAIN,
16.0f);
italicFont = regularFont.deriveFont(Font.ITALIC);
updateDisplays();
//Lay out the panel.
panel.setBorder(BorderFactory.createEmptyBorder(
GAP/2, //top
0, //left
GAP/2, //bottom
0)); //right
panel.add(new JSeparator(JSeparator.VERTICAL),
BorderLayout.LINE_START);
panel.add(addressDisplay,
BorderLayout.CENTER);
panel.setPreferredSize(new Dimension(200, 150));
return panel;
}
protected String formatAddress() {
if (!addressSet) return "Preview.";
String student = studentField.getText();
String address = addressField.getText();
String course = (String)courseSpinner.getValue();
String stud_id = stu_idField.getText();
String paid = paidField.getText();
String empty = "";
if ((student == null) || empty.equals(student)) {
student = "<em>(no student specified)</em>";
}
if ((address == null) || empty.equals(address)) {
address = "<em>(no address specified)</em>";
}
if ((course == null) || empty.equals(course)) {
course = "<em>(no course specified)</em>";
} else {
int abbrevIndex = course.indexOf('(') + 1;
course = course.substring(abbrevIndex,
abbrevIndex + 2);
}
if ((stud_id == null) || empty.equals(stud_id)) {
stud_id = "";
}
if ((paid == null) || empty.equals(paid)) {
paid = "";
}
StringBuffer sb = new StringBuffer();
sb.append("<html><p align=Left>");
sb.append(student);
sb.append("<br>");
sb.append(address);
sb.append("<br> ");
sb.append(course); //should format
sb.append("<br>");
sb.append(stud_id);
sb.append(paid); //paid
sb.append("<br>"); //paid
sb.append("</p></html><br><br><br><br>");
return sb.toString();
}
// MaskFormatter.
protected MaskFormatter createFormatter(String s) {
MaskFormatter formatter = null;
try {
formatter = new MaskFormatter(s);
} catch (java.text.ParseException exc) {
System.err.println("formatter is bad: " + exc.getMessage());
System.exit(-1);
}
return formatter;
}
public void focusGained(FocusEvent e) {
Component c = e.getComponent();
if (c instanceof JFormattedTextField) {
selectItLater(c);
} else if (c instanceof JTextField) {
((JTextField)c).selectAll();
}
}
//side
protected void selectItLater(Component c) {
if (c instanceof JFormattedTextField) {
final JFormattedTextField ftf = (JFormattedTextField)c;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ftf.selectAll();
}
});
}
}
//FocusListener
public void focusLost(FocusEvent e) { } //ignore
protected JComponent createEntryFields() {
JPanel panel = new JPanel(new SpringLayout());
String[] labelStrings = {
"Student Name: ",
"address: ",
"Course: ",
"Student ID: ",
"Tuition paid [Y/N]"
};
JLabel[] labels = new JLabel[labelStrings.length];
JComponent[] fields = new JComponent[labelStrings.length];
int fieldNum = 0;
//Create the text field
studentField = new JTextField();
studentField.setColumns(30);
fields[fieldNum++] = studentField;
addressField = new JTextField();
addressField.setColumns(20);
fields[fieldNum++] = addressField;
String[] courseStrings = getcourseStrings();
courseSpinner = new JSpinner(new SpinnerListModel(courseStrings));
fields[fieldNum++] = courseSpinner;
stu_idField = new JFormattedTextField(
createFormatter("###########"));
fields[fieldNum++] = stu_idField;
paidField = new JTextField(); //paid
paidField.setColumns(2); //paid
fields[fieldNum++] = paidField; //paid
//Labeling lng i2
for (int i = 0; i < labelStrings.length; i++) {
labels[i] = new JLabel(labelStrings[i],
JLabel.TRAILING);
labels[i].setLabelFor(fields[i]);
panel.add(labels[i]);
panel.add(fields[i]);
//Add ng listeners
JTextField tf = null;
if (fields[i] instanceof JSpinner) {
tf = getTextField((JSpinner)fields[i]);
} else {
tf = (JTextField)fields[i];
}
tf.addActionListener(this);
tf.addFocusListener(this);
}
SpringUtilities.makeCompactGrid(panel,
labelStrings.length, 2,
GAP, GAP, //init x,y
GAP, GAP/2);//xpad, ypad
return panel;
}
public String[] getcourseStrings() {
String[] courseStrings = {
"Computer Studies (CS)",
"Information Technology (IT)",
"Secondary Education (SE)",
"Mechanical Engineering (ME)",
"Electrical Engineering (EE)",
"Civil Engineering (CE)",
"Computer Engineering (CE)",
"Industrial Engineering (IE)",
};
return courseStrings;
}
public JFormattedTextField getTextField(JSpinner spinner) {
JComponent editor = spinner.getEditor();
if (editor instanceof JSpinner.DefaultEditor) {
return ((JSpinner.DefaultEditor)editor).getTextField();
} else {
System.err.println("Unexpected editor type: "
+ spinner.getEditor().getClass()
+ " isn't a descendant of DefaultEditor");
return null;
}
}
private static void createAndShowGUI() {
//Decoration
JFrame.setDefaultLookAndFeelDecorated(true);
//window.
JFrame frame = new JFrame("Panchita");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//content pane.
JComponent newContentPane = new Panchita();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
class SpringUtilities {
public static void printSizes(Component c) {
System.out.println("minimumSize = " + c.getMinimumSize());
System.out.println("preferredSize = " + c.getPreferredSize());
System.out.println("maximumSize = " + c.getMaximumSize());
}
public static void makeGrid(Container parent,
int rows, int cols,
int initialX, int initialY,
int xPad, int yPad) {
SpringLayout layout;
try {
layout = (SpringLayout)parent.getLayout();
} catch (ClassCastException exc) {
System.err.println("The first argument to makeGrid must use SpringLayout.");
return;
}
Spring xPadSpring = Spring.constant(xPad);
Spring yPadSpring = Spring.constant(yPad);
Spring initialXSpring = Spring.constant(initialX);
Spring initialYSpring = Spring.constant(initialY);
int max = rows * cols;
//height and width ng preview
Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)).
getWidth();
Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)).
getWidth();
for (int i = 1; i < max; i++) {
SpringLayout.Constraints cons = layout.getConstraints(
parent.getComponent(i));
maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
}
//new width/height
for (int i = 0; i < max; i++) {
SpringLayout.Constraints cons = layout.getConstraints(
parent.getComponent(i));
cons.setWidth(maxWidthSpring);
cons.setHeight(maxHeightSpring);
}
// adjust the x/y constraints
SpringLayout.Constraints lastCons = null;
SpringLayout.Constraints lastRowCons = null;
for (int i = 0; i < max; i++) {
SpringLayout.Constraints cons = layout.getConstraints(
parent.getComponent(i));
if (i % cols == 0) { //start of new row
lastRowCons = lastCons;
cons.setX(initialXSpring);
} else { //x component
cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST),
xPadSpring));
}
if (i / cols == 0) { //first row
cons.setY(initialYSpring);
} else { //y previous row
cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH),
yPadSpring));
}
lastCons = cons;
}
//Set the parent's size.
SpringLayout.Constraints pCons = layout.getConstraints(parent);
pCons.setConstraint(SpringLayout.SOUTH,
Spring.sum(
Spring.constant(yPad),
lastCons.getConstraint(SpringLayout.SOUTH)));
pCons.setConstraint(SpringLayout.EAST,
Spring.sum(
Spring.constant(xPad),
lastCons.getConstraint(SpringLayout.EAST)));
}
private static SpringLayout.Constraints getConstraintsForCell(
int row, int col,
Container parent,
int cols) {
SpringLayout layout = (SpringLayout) parent.getLayout();
Component c = parent.getComponent(row * cols + col);
return layout.getConstraints(c);
}
public static void makeCompactGrid(Container parent,
int rows, int cols,
int initialX, int initialY,
int xPad, int yPad) {
SpringLayout layout;
try {
layout = (SpringLayout)parent.getLayout();
} catch (ClassCastException exc) {
System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
return;
}
//Align ng cells
Spring x = Spring.constant(initialX);
for (int c = 0; c < cols; c++) {
Spring width = Spring.constant(0);
for (int r = 0; r < rows; r++) {
width = Spring.max(width,
getConstraintsForCell(r, c, parent, cols).
getWidth());
}
for (int r = 0; r < rows; r++) {
SpringLayout.Constraints constraints =
getConstraintsForCell(r, c, parent, cols);
constraints.setX(x);
constraints.setWidth(width);
}
x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
}
//Align ng cells row/heights
Spring y = Spring.constant(initialY);
for (int r = 0; r < rows; r++) {
Spring height = Spring.constant(0);
for (int c = 0; c < cols; c++) {
height = Spring.max(height,
getConstraintsForCell(r, c, parent, cols).
getHeight());
}
for (int c = 0; c < cols; c++) {
SpringLayout.Constraints constraints =
getConstraintsForCell(r, c, parent, cols);
constraints.setY(y);
constraints.setHeight(height);
}
y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
}
//Set the parent's size.
SpringLayout.Constraints pCons = layout.getConstraints(parent);
pCons.setConstraint(SpringLayout.SOUTH, y);
pCons.setConstraint(SpringLayout.EAST, x);
}
//}//do
}