How to Align This Perfectly

Blueberry Muffin 1.45
Strawberry Bagel 0.80
Lite Yogurt 0.75
Vanilla Ice Cream 2.75
Hash Browns 2.50
Toast 2.00
French Fries 1.50
Onion Soup 3.00
Coffee 0.90
Iced Tea 1.00
Hot Chocolate 1.75


Here is my code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener.*;

public class TeamBa extends JFrame implements ActionListener
{
    static String[] yourChoicesItems = 
                        {"Blueberry Muffin      1.45",
                         "Strawberry Bagel      0.80",
                         "Lite Yogurt           0.75",
                         "Vanilla Ice Cream     2.75",
                         "Hash Browns           2.50",
                         "Toast                 2.00",
                         "French Fries          1.50",
                         "Onion Soup            3.00",
                         "Coffee                0.90",
                         "Iced Tea              1.00",
                         "Hot Chocolate         1.75"};
    
    static double[] yourChoicesPrices = {1.45, 0.80, 0.75, 2.75, 2.50, 2.00, 1.50, 3.00, 0.90, 1.00, 1.75};
    
    private JList yourChoices;
    private JTextArea bill;
    private Container pane;
    
    public TeamBa()
    {
        
        
        pane = getContentPane();
        pane.setBackground(new Color(0, 200, 200));
        pane.setLayout(new BorderLayout(5, 5));
        
        JLabel yourChoicesLabel = new JLabel("A LA CARTE MENU");
        pane.add(yourChoicesLabel, BorderLayout.NORTH);
        yourChoicesLabel.setFont(new Font("Dialog",Font.BOLD,20));
        
        yourChoices = new JList(yourChoicesItems);
        pane.add(new JScrollPane(yourChoices), BorderLayout.WEST);
        yourChoices.setFont(new Font("Courier",Font.BOLD,14));
        
        bill = new JTextArea();
        pane.add(bill,BorderLayout.EAST);
        bill.setFont(new Font("Courier",Font.PLAIN,12));        
        
        JButton button = new JButton("Selection Completed");
        pane.add(button,BorderLayout.SOUTH);
        button.addActionListener(this);
        
        setTitle("Welcome to Java Kiosk");
        setSize(500, 360);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    
    private void displayBill()
    {
        int[] listArray = yourChoices.getSelectedIndices();
        double localTax = 0.065;
        double tax;
        double subTotal = 0;
        double total;
        
        bill.setEditable(false);
        bill.setText("");
        
        for(int index = 0; index < listArray.length; index++)
            subTotal = subTotal + yourChoicesPrices[listArray[index]];
        
        tax = localTax * subTotal;
        total = subTotal + tax;
        
        bill.append("       JAVA KIOSK A LA CARTE\n\n");
        bill.append("----------------- Welcome ----------------------\n\n");
        
        for(int index = 0; index < listArray.length; index++)
        {
            bill.append(yourChoicesItems[listArray[index]] + "\n");
        }
        
        bill.append("\n");
        bill.append("SUB TOTAL\t\t$" + String.format("%.2f", subTotal) + "\n");
        bill.append("TAX      \t\t$" + String.format("%.2f", tax) + "\n");
        bill.append("TOTAL    \t\t$" + String.format("%.2f", total) + "\n");
        bill.append("\n         Thank You!! - Have a nice day!!");
        
        yourChoices.clearSelection();
        
        repaint();
              
    }
    
    public void actionPerformed(ActionEvent event)
    {
        if (event.getActionCommand().equals("Selection Completed"))
            displayBill();
    }
    
    public static void main(String[] args)
    {
        TeamBa alc = new TeamBa();
    }
}

Try...

Font Bill_Font = new Font ("Courier", Font.PLAIN, 12);
Bill.setFont (Bill_Font);

...in your displayBill() method.

Since most fonts are proportional, things don't line up.
The Courier font is not proportional (fixed width).

Edited 5 Years Ago by hfx642: n/a

This article has been dead for over six months. Start a new discussion instead.