Question

public class NewAccountApplet extends JApplet implements ActionListener{
    JPanel jp1, jp2, jp3, jp4, jp5, jp6;
    GridLayout productLO = new GridLayout(10,4,10,10);
    int qty = 5;
    JComboBox<Object>[] selectQty;

if (e.getActionCommand().equals("Login")) {

    if (id.equals(checkID) && pw.equals(checkPW)) {
    JOptionPane.showMessageDialog(null, "Authenticated");
    JPanel content = (JPanel)getContentPane(); 
    GridBagConstraints firstCol = new GridBagConstraints(); 
    firstCol.weightx = 1.0; 
    firstCol.anchor = GridBagConstraints.WEST; 
    firstCol.insets = new Insets(5, 20, 5, 5); 
    GridBagConstraints lastCol = new GridBagConstraints(); 
    lastCol.gridwidth = GridBagConstraints.REMAINDER; 
    lastCol.weightx = 1.0; 
    lastCol.fill = GridBagConstraints.HORIZONTAL; 
    lastCol.insets = new Insets(5, 5, 5, 20); 

    selectQty = new JComboBox[qty];

    jp1.setVisible(false);
    jp2.setVisible(false);
    jp3.setVisible(false);
    jp4.setVisible(true);
    jp5.setVisible(true);
    jp6.setVisible(true);

    String[] itemText = {"1", "2", "3", "4", "5"};
    JLabel[] items = new JLabel[6];
    JLabel purchasePage = new JLabel("Items for Purchase"); 
    jp4.add(purchasePage);
    content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS)); 
    content.add(jp4);

    jp4 = new JPanel();
    jp5 = new JPanel(new GridBagLayout()); //set jp5 as a new jpanel with gridbaglayout
    jp6 = new JPanel(); 

    for(int i=0; (i<items.length); i++) { 
        items[i] = new JLabel(); //adds items[i] as JLabel
        items[i].setText(itemText[i]); //sets text of items as itemText[]
        jp5.add(items[i], firstCol);  //adds items to firstcol of jp5
        selectQty[i] = new JComboBox<Object>(); //JComboBox selectqty[i]
        selectQty[i].setPreferredSize(new Dimension(300, 20)); //sets the size
        jp5.add(selectQty[i], lastCol); //sadsdasd
        }

    }
    else JOptionPane.showMessageDialog(null, "Wrong account information");}

I have some questions regarding adding a JComboBox into a loop to display on my JApplet. the for loop on the bottom adds my JComboBox (selectQty) to the screen. But I get an error message on eclipse where i coded as: items[i].setText(itemText[i]);. It shows up my JPanel jp4 correctly. but the JPanel jp5 is not showing up.. I wonder what is wrong...

So to summarize, the code compiles (with other codes that are not on here), but japplet only shows jp4 jpanel, and error occrs on line: items[i].setText(itemText[i]);.

Was it helpful?

Solution

itemText has 5 elements {"1", "2", "3", "4", "5"} but JLabel[] items = new JLabel[6] items has 6.

OTHER TIPS

You appear to be re-creating jp5 but neither removing the old instance or adding the new instance...

// This suggests that jp5 has already been created
jp5.setVisible(true);
//...
// But you create a new instance here
jp5 = new JPanel(new GridBagLayout());
// Then add stuff to it...

Try using jp5.removeAll() instead of jp5 = new JPanel(new GridBagLayout());

The likely cause of your error is probably IndexOutOfBoundsException caused by the fact the itemText and items have different numbers of elements

String[] itemText = {"1", "2", "3", "4", "5"};
JLabel[] items = new JLabel[6];
//...
for(int i=0; (i<items.length); i++) { 
    items[i] = new JLabel(); //adds items[i] as JLabel
    // Error here on the last element...
    items[i].setText(itemText[i]); //sets text of items as itemText[]

Instead, consider using something like...

String[] itemText = {"1", "2", "3", "4", "5"};
JLabel[] items = new JLabel[itemText.length];

And remember, magic numbers are a bad idea

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.*;

public class NewAccountApplet extends JApplet implements ActionListener{
   /**
     * 
     */
    private static final long serialVersionUID = 1L;
        JLabel titlePage; 
        JLabel[] txt; 
        JTextField[] jtf; 
        JButton accept, decline;
        JPanel jp1, jp2, jp3, jp4, jp5, jp6;
        String[] accountlist = {"Select Account Type.", "Customer", "Admin"};
        JComboBox<Object> textAlignment = new JComboBox<Object>(accountlist);
        GridLayout productLO = new GridLayout(10,4,10,10);
        int qty = 5;
        JComboBox<Object>[] selectQty;


public void init(){
    setSize(400,400);

    JPanel content = (JPanel)getContentPane(); 
    GridBagConstraints firstCol = new GridBagConstraints(); 
    firstCol.weightx = 1.0; 
    firstCol.anchor = GridBagConstraints.WEST; 
    firstCol.insets = new Insets(5, 20, 5, 5); 
    GridBagConstraints lastCol = new GridBagConstraints(); 
    lastCol.gridwidth = GridBagConstraints.REMAINDER; 
    lastCol.weightx = 1.0; 
    lastCol.fill = GridBagConstraints.HORIZONTAL; 
    lastCol.insets = new Insets(5, 5, 5, 20); 

    String[] labeltxt = {"Name","Account ID","Password","E-Mail","Phone","Address","","","Account Type"}; 
    titlePage = new JLabel("Create New Account"); 
    txt = new JLabel[9]; 
    jtf = new JTextField[9]; 
    accept = new JButton("Create"); 
    decline = new JButton("Decline"); 

    jp1 = new JPanel(); 
    jp2 = new JPanel(new GridBagLayout()); 
    jp3 = new JPanel(); 
    jp4 = new JPanel();
    jp5 = new JPanel();
    jp6 = new JPanel();

    for(int i=0; (i<9); i++) { 
        txt[i] = new JLabel(); 
        txt[i].setText(labeltxt[i]); 
        jp2.add(txt[i], firstCol); 
        jtf[i] = new JTextField(); 
        jtf[i].setPreferredSize(new Dimension(300, 20)); 
        jp2.add(jtf[i], lastCol); 

        }



    jp1.add(titlePage); 
    jp3.add(accept); 
    jp3.add(decline); 
    content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS)); 
    content.add(jp1); 
    content.add(jp2); 
    content.add(jp3); 
    String id = this.jtf[1].getText();
    String pw = this.jtf[2].getText();
    jtf[6].setText(id);
    jtf[7].setText(pw);

    jtf[6].setVisible(false);
    jtf[7].setVisible(false);
    jtf[8].setVisible(false);

    jp2.add(textAlignment, lastCol);

    decline.addActionListener(this);
    accept.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {
    String id = jtf[1].getText();
    String pw = jtf[2].getText();
    String checkID = jtf[6].getText();
    String checkPW = jtf[7].getText();
    String accountType = "";
    String correctType = "Customer";
    String chosenType = (String) textAlignment.getSelectedItem();
    JPasswordField pField = new JPasswordField(10);
    JPanel pPanel = new JPanel();
    pPanel.add(new JLabel("Please Enter Password: "));
    pPanel.add(pField);

    if (e.getActionCommand().equals("Create") && (chosenType.equals("Customer"))){
        JOptionPane.showMessageDialog(null, "Thank you for Joining!");
        id = jtf[1].getText();
        pw = jtf[2].getText();
        titlePage.setText("Welcome to Final Sales!");
        accept.setText("Login");
        decline.setText("Cancel");
        txt[6].setText("UserName");
        txt[7].setText("Password");
        jtf[0].setText("");
        jtf[3].setText("");
        jtf[4].setText("");
        jtf[5].setText("");

        txt[0].setVisible(false);
        txt[1].setVisible(false);
        txt[2].setVisible(false);
        txt[3].setVisible(false);
        txt[4].setVisible(false);
        txt[5].setVisible(false);

        textAlignment.setVisible(false);
        txt[8].setVisible(false);

        jtf[0].setVisible(false);
        jtf[1].setVisible(false);
        jtf[2].setVisible(false);
        jtf[3].setVisible(false);
        jtf[4].setVisible(false);
        jtf[5].setVisible(false);
        jtf[6].setVisible(true);
        jtf[7].setVisible(true);

        }

    if (e.getActionCommand().equals("Create") && (chosenType.equals("Admin"))) {
        JOptionPane.showMessageDialog(null, pPanel);
        JOptionPane.showMessageDialog(null, "Wrong Admin Password"); 
    }

    if (e.getActionCommand().equals("Create") && (chosenType.equals("Select Account Type."))) {
        JOptionPane.showMessageDialog(null, "You have selected wrong account type.");
    }

    if (e.getActionCommand().equals("Decline"))
        System.exit(0);

    if (e.getActionCommand().equals("Login")) { 
            if (id.equals(checkID) && pw.equals(checkPW)) {
            JOptionPane.showMessageDialog(null, "Authenticated");

            JPanel content = (JPanel)getContentPane(); 
            GridBagConstraints firstCol = new GridBagConstraints(); 
            firstCol.weightx = 1.0; 
            firstCol.anchor = GridBagConstraints.WEST; 
            firstCol.insets = new Insets(5, 20, 5, 5); 
            GridBagConstraints lastCol = new GridBagConstraints(); 
            lastCol.gridwidth = GridBagConstraints.REMAINDER; 
            lastCol.weightx = 1.0; 
            lastCol.fill = GridBagConstraints.HORIZONTAL; 
            lastCol.insets = new Insets(5, 5, 5, 20); 

            selectQty = new JComboBox[qty];

            jp1.setVisible(false);
            jp2.setVisible(false);
            jp3.setVisible(false);
            jp4.setVisible(true);
            jp5.setVisible(true);
            jp6.setVisible(true);


            String[] itemText = {"White Snapback", "Silver Necklace", "Black T Shirt", "", "5"};
            JLabel[] items = new JLabel[5];
            JLabel purchasePage = new JLabel("Items for Purchase"); 
            jp4.add(purchasePage);
            content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS)); 
            content.add(jp4);

            jp4 = new JPanel();
            jp5.setLayout(new GridBagLayout());
            jp6 = new JPanel();

            for(int i=0; (i<items.length); i++) { 
                items[i] = new JLabel(); 
                items[i].setText(itemText[i]); 
                jp5.add(items[i], firstCol); 
                selectQty[i] = new JComboBox<Object>(); 
                selectQty[i].setPreferredSize(new Dimension(300, 20)); 
                jp5.add(selectQty[i], lastCol); 
            }







            }
            else JOptionPane.showMessageDialog(null, "Wrong account information");}

        if (e.getActionCommand().equals("Cancel")) {
            System.exit(0);}
        }


    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top