I know the below code does not work for what I'm trying to do, but I haven't found a fix for it. I am trying to add a blank rectangle to the bottom of the frame when the program runs. When I run the following code, the frame populates correctly with my labels, button, and TextFieldArea but it isn't creating the blank rectangle I am trying to add to the bottom (The end goal will be to fill the rectangle as the program works to completion aka a progress bar).
My thoughts were that you could create a subClass within my TicketCounterFrame class that would be able to extend JComponent so I could create my rectangle that way. I have a companion method called createComponents
where I am creating the rest of my components, and then I create a panel in the constructor, add all my components to the panel, and then add the panel to the frame. I haven't worked with creating shapes and adding them to a frame at all so I'm confused on if you would need to make a private instance variable progBar of type ProgressBar and how exactly you create that component and add it to the frame along with the rest of the components created in createComponents
method.
I'm positive I'm not thinking about this correctly but haven't been able to find helpful examples. Can someone show me how/why I'm doing this incorrectly? Any help would be greatly appreciated.
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TicketCounterFrame extends JFrame{
TicketCounter tickets;
private JLabel headerLbl;
private JLabel promptLbl;
private JLabel soldLbl;
private JTextField ticketAmount;
private JButton purchaseBtn;
private ProgressBar progBar;
public TicketCounterFrame(TicketCounter tkts){
this.tickets = tkts;
createComponents();
submitClickListener s = new submitClickListener();
purchaseBtn.addActionListener(s);
JPanel panel = new JPanel();
panel.add(headerLbl);
panel.add(ticketAmount);
panel.add(purchaseBtn);
panel.add(promptLbl);
panel.add(soldLbl);
panel.add(progBar);
add(panel);
setSize(600,400);
}
class submitClickListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String t = ticketAmount.getText();
try
{
int ticketAmt = Integer.parseInt(t);
if(ticketAmt <= 6 && ticketAmt > 0)
{
if (ticketAmt < tickets.getRemaining())
{
tickets.purchaseTickets(ticketAmt);
ticketAmount.setText("");
promptLbl.setText(ticketAmt + " ticket(s) purchased. There are " + tickets.getRemaining() + " tickets remaining for sale");
}
else if(ticketAmt > tickets.getRemaining())
{
ticketAmount.setText("");
promptLbl.setText("Invalid entry. There are only " + tickets.getRemaining() + " tickets left. Try again.");
}
else if (ticketAmt == tickets.getRemaining())
{
tickets.purchaseTickets(ticketAmt);
ticketAmount.setText("");
promptLbl.setText("There are " + tickets.getRemaining() + " tickets left.");
soldLbl.setText("SOLD OUT!");
}
}
else
{
ticketAmount.setText("");
promptLbl.setText("Invalid entry. Try again. There are " + tickets.getRemaining() + " tickets remaining for sale");
}
}
catch (NumberFormatException err){
ticketAmount.setText("");
promptLbl.setText("Invalid entry. Try again. There are " + tickets.getRemaining() + " tickets remaining");
}
}
}
private void createComponents(){
headerLbl = new JLabel("Enter how many tickets for purchase (1 - 6)");
ticketAmount = new JTextField(10);
promptLbl = new JLabel("There are " + tickets.getRemaining() + " tickets for sale");
soldLbl = new JLabel();
purchaseBtn = new JButton("Buy tickets");
progBar = new ProgressBar();
}
public class ProgressBar extends JComponent{
@Override
public void paintComponent(Graphics g){
g.drawRect(100, 200, 300, 50);
}
}
}