Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

JAVA: Panels Changes

Options
  • 04-04-2005 12:30pm
    #1
    Registered Users Posts: 1,987 ✭✭✭


    The below code is the code im haveing problems with, when i click on the 'Tender' button nothin happens, the 'td' class is called and it runs through the whole class and comes back to this when finished like it should but the panel in the 'td' class will not appear and the current panel stays! Any ideas?


    public void actionPerformed(ActionEvent e)
    {
    JButton clickedButton = (JButton)e.getSource();
    String buttonText = clickedButton.getText();

    if(buttonText.equals("TENDER"))
    {
    TenderPanel td = new TenderPanel();
    }
    else if(buttonText.equals("DISCOUNT"))
    {
    DiscountScreen d = new DiscountScreen();
    }
    ....etc


Comments

  • Closed Accounts Posts: 7 MDMA


    first thing to do is check to see if the code is entering ur "Method"
    its a dump as **** trick but it helps put in the following code

    public void actionPerformed(ActionEvent e)
    System.out.println("step1");//this will tell u if it gets here
    {
    JButton clickedButton = (JButton)e.getSource();
    String buttonText = clickedButton.getText();

    if(buttonText.equals("TENDER")){
    System.out.println("step2"); see if ur switch works

    TenderPanel td = new TenderPanel();
    }
    else if(buttonText.equals("DISCOUNT"))

    {
    System.out.println("step3");//same again

    DiscountScreen d = new DiscountScreen();
    }


    now run the java prog and look at the dos screen for output

    now if u click the button and see nothing then ur action command if wrong
    which we will need more than the above code to fix

    then if the other messages do not appear then ur JButton = new JButton("is not named the same as the txt in the switch); if none of these problems arise we eill need more code

    so there thats my dumb ass trick for debugging.....


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    i used that trink already and its running through the method f but its not creating the new panle and adding it to the content page!??


  • Registered Users Posts: 4,188 ✭✭✭pH


    I'm not sure that you're showing all the code but :
     if(buttonText.equals("TENDER"))
    {
    TenderPanel td = new TenderPanel();
    }
    
    Will just create a new TenderPanel object and throw it away. You need to add it to the existing panel/form to see it


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    I tried the below but still nothin!


    if(buttonText.equals("TENDER"))
    {
    TenderPanel();
    cPane.add(tenderPanelEast, BorderLayout.EAST);
    }


  • Closed Accounts Posts: 7 MDMA


    Could u post more/all of ur code and we'll have a stare at it...


  • Advertisement
  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    I've posted the code dat is used for the ActionListener and the two panels below!!???

    public MainScreen()
    {
    tender = new JButton("TENDER");
    tender.addActionListener(this);
    discount = new JButton("DISCOUNT");
    discount.addActionListener(this);
    search = new JButton("SEARCH");
    search.addActionListener(this);
    returns = new JButton("RETURNS");
    returns.addActionListener(this);
    adminTools = new JButton("ADMIN TOOLS");
    adminTools.addActionListener(this);
    logoff = new JButton("LOG OFF");
    logoff.addActionListener(this);
    //End of east panel items

    eastPanel.add(tender);
    eastPanel.add(discount);
    eastPanel.add(search);
    eastPanel.add(returns);
    eastPanel.add(adminTools);
    eastPanel.add(logoff);

    cPane.add(eastPanel, BorderLayout.EAST);
    }



    private void TenderPanel()
    {
    JButton cash = new JButton("CASH");
    cash.addActionListener(this);
    tenderPanelEast.add(cash);
    JButton card = new JButton("CARDS");
    tenderPanelEast.add(card);
    JButton cheque = new JButton("CHEQUE");
    tenderPanelEast.add(cheque);
    JButton logoff = new JButton("LOG OFF");
    tenderPanelEast.add(logoff);
    }

    public void actionPerformed(ActionEvent e)
    {
    JButton clickedButton = (JButton)e.getSource();
    String buttonText = clickedButton.getText();

    if(buttonText.equals("TENDER"))
    {
    cPane.remove(southPanel);
    TenderPanel();
    cPane.add(tenderPanelEast, BorderLayout.EAST);
    }
    else if(buttonText.equals("DISCOUNT"))
    {
    DiscountScreen d = new DiscountScreen();
    }
    else if(buttonText.equals("SEARCH"))
    {

    }
    else if(buttonText.equals("RETURNS"))
    {

    }
    else if(buttonText.equals("ADMIN TOOLS"))
    {

    }
    else if(buttonText.equals("LOG OFF"))
    {
    dbconn.closeConnection();
    System.exit(0);
    }
    }


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Well for a start everything you define in TenderPanel is going to be thrown away after the method reaches the end brace. Define all your buttons outside of the method and then instansiate them inside in it.
    /*Before any methods*/
    
    JButton cash, card, cheque, logoff;
    
    /*Then, when the time comes*/
    
    private void TenderPanel()
    {
        cash = new JButton("CASH");
        cash.addActionListener(this);
        tenderPanelEast.add(cash);
        card = new JButton("CARDS");
        tenderPanelEast.add(card);
        cheque = new JButton("CHEQUE");
        tenderPanelEast.add(cheque);
        logoff = new JButton("LOG OFF");
        tenderPanelEast.add(logoff);
    }
    

    Secondly, you're going about actionPerformed in a strange way. You don't need to define a button and pull the text from it, you can just do a direct object comparison.
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == tender)
        {
            cPane.remove(southPanel);
            TenderPanel();
            cPane.add(tenderPanelEast, BorderLayout.EAST);
        }
    }
    


Advertisement