0

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);
        }
    }
}
arooney88
  • 195
  • 1
  • 7
  • 21
  • So, is your question "how do I add a component to the bottom of a container"? If so, then think layout managers such as BorderLayout and adding your progBar BorderLayout.PAGE_END (also known as BorderLayout.SOUTH). Also progBar needs to have an adequate preferredSize. For my money though I'd use a real JProgressBar for this. – Hovercraft Full Of Eels Dec 10 '17 at 20:20
  • Possible duplicate of [How to align JPanel to bottom of JFrame (java swing)](https://stackoverflow.com/questions/29907307/how-to-align-jpanel-to-bottom-of-jframe-java-swing) – Hovercraft Full Of Eels Dec 10 '17 at 20:21
  • @HovercraftFullOfEels I'm not using a layout manager just yet. I am using the default for JPanel, so my thinking was, since I'm adding it last to panel, that would be at the bottom by default. My issue is that I can't even get the rectangle to draw in the first place and I'm not sure how to draw a component on a frame. – arooney88 Dec 10 '17 at 20:35
  • `"I'm not using a layout manager just yet..."` -- and **THAT'S** your problem. No, it does not "go to the bottom by default". You in fact **are** using a layout manager without realizing it -- JPanel uses FlowLayout by default. Please read the layout manager tutorial to learn how to use them properly. And no your rectangle won't draw because of the second issue I mentioned -- your JComponent has a default preferred size of [0, 0]. – Hovercraft Full Of Eels Dec 10 '17 at 20:37
  • @HovercraftFullOfEels I meant that by default, you are using the FlowLayout for JPanel if you don't specify one, my fault for making that confusing. Do I set the preferred size of my component after I construct it? – arooney88 Dec 10 '17 at 21:16
  • See: https://stackoverflow.com/questions/1783793/java-difference-between-the-setpreferredsize-and-setsize-methods-in-compone – Hovercraft Full Of Eels Dec 10 '17 at 21:20

0 Answers0