Help with JButton to open JPanel

So I have this code so far:

[code]
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.awt.*;

import javax.swing.*;

public class GUI2 extends JFrame {

private JButton first;
private JButton prev;
private JButton next;
private JButton last;
private JButton add;
private JButton delete;
private JButton save;
private JButton search;
public int index = 0;
private List lines;
private int currentIndex;
private JFrame textFrame;
private JLabel label;
private JPanel panel;
private JTextField brandtext;
private JTextField unitstext;
private JTextField pricetext;
public GUI2() {
super("Inventory Program Part 5");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lines = new ArrayList();
setLayout(new FlowLayout());

first = new JButton("First");
add(first);

prev = new JButton("Previous");
add(prev);

next = new JButton("Next");
add(next);

last = new JButton("Last");
add(last);

add = new JButton("Add");
add(add);

delete = new JButton("Delete");
add(delete);

save = new JButton("Save");
add(save);

search= new JButton ("Search");
add(search);

ImageIcon icon = new ImageIcon(getClass().getResource("logoImage.JPG"));
label = new JLabel(icon);
add(label);

HandlerClass handler = new HandlerClass();
first.addActionListener(handler);
prev.addActionListener(handler);
next.addActionListener(handler);
last.addActionListener(handler);

addHandlerClass addHandler = new addHandlerClass();
add.addActionListener(addHandler);
delete.addActionListener(handler);
save.addActionListener(handler);
search.addActionListener(handler);
}

private ImageIcon createImageIcon(String string) {
// TODO Auto-generated method stub
return null;
}

public GUI2(String arrayRecord) {
this();

try {
BufferedReader reader = new BufferedReader(new FileReader(arrayRecord));
String line = "";

while ((line = reader.readLine()) != null) {
lines.add(line);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

public void showFile() {
for (int i = 0; i < lines.size(); i++) {
System.out.println(lines.get(i));
}
}

public static void main(final String[] args) {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
GUI2 f = new GUI2(args[0]);
f.setSize(200, 900);
f.setVisible(true);
}
});
} catch (Exception e) {
System.out.println("Error");
}
}

private class HandlerClass implements ActionListener {
public void actionPerformed(ActionEvent event) {
textFrame = new JFrame("Inventory");
textFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
textFrame.getContentPane();

JTextArea textArea = new JTextArea();
textFrame.add(textArea);

Object button = event.getSource();

if (first == button) {
currentIndex = 0;
} else if ((prev == button) && (currentIndex > 0)) {
--currentIndex;
} else if (next == button) {
++currentIndex;
}

else if (delete == button){

}

// Wrap
if (currentIndex == lines.size()) {
currentIndex = 0;

} else if (last == button) {
currentIndex = lines.size()-1;
}

textArea.setText(lines.get(currentIndex));
textFrame.setSize(900, 150);
textFrame.setVisible(true);
}
}
private class addHandlerClass implements ActionListener{
public void actionPerformed(ActionEvent event){
panel = new JPanel();
brandtext = new JTextField(10);
unitstext = new JTextField(10);
pricetext = new JTextField(10);
panel.add(new JLabel("Enter Brand: "));
panel.add(brandtext);
panel.add(new JLabel("Enter Units:"));
panel.add(unitstext);
panel.add(new JLabel("Enter Price: "));
panel.add(pricetext);
panel.setVisible(true);
panel.setSize(200,200);

Object button = event.getSource();

if (add == button){
<-<-<-probelem

brandtext.setText("");
unitstext.setText("");
pricetext.setText("");
}

}

}
}

[/code]


and I'm trying to make the "add" button open a new window with text fields so they can enter new product information. I can't seem to figure out how to even make the JPanel open at all when you click on the button, less likely how to make it take in user information. I'm just trying to take this one step at a time. First: make the window open. Thanks for any help :)

Comments

  • tia,
    I think what you want is a separate dialog to capture the information related to the Add button. A Jpanel needs to reside within a window I believe.
    I made minor changes to the code and it displays the dialog you've designed.
    Also when an instance of GUI2 is instantiated, f, there is no constructor that takes a parameter.
    Overall the program seems to be coming along pretty well.
    [code]

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import java.util.ArrayList;
    import java.util.List;
    import java.awt.*;

    import javax.swing.*;

    public class GUI2 extends JFrame {

    private JButton first;
    private JButton prev;
    private JButton next;
    private JButton last;
    private JButton add;
    private JButton delete;
    private JButton save;
    private JButton search;
    public int index = 0;
    private List lines;
    private int currentIndex;
    private JFrame textFrame;
    private JLabel label;
    // private JPanel panel;
    private javax.swing.JDialog panel;
    private JTextField brandtext;
    private JTextField unitstext;
    private JTextField pricetext;

    public GUI2() {
    super("Inventory Program Part 5");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    lines = new ArrayList();
    setLayout(new FlowLayout());

    first = new JButton("First");
    add(first);

    prev = new JButton("Previous");
    add(prev);

    next = new JButton("Next");
    add(next);

    last = new JButton("Last");
    add(last);

    add = new JButton("Add");
    add(add);

    delete = new JButton("Delete");
    add(delete);

    save = new JButton("Save");
    add(save);

    search = new JButton("Search");
    add(search);

    ImageIcon icon = new ImageIcon(getClass().getResource("logoImage.JPG"));
    // ImageIcon icon = new ImageIcon(getClass().getResource("JK.JPG"));
    label = new JLabel(icon);
    add(label);

    HandlerClass handler = new HandlerClass();
    first.addActionListener(handler);
    prev.addActionListener(handler);
    next.addActionListener(handler);
    last.addActionListener(handler);

    addHandlerClass addHandler = new addHandlerClass();
    add.addActionListener(addHandler);
    delete.addActionListener(handler);
    save.addActionListener(handler);
    search.addActionListener(handler);
    }

    private ImageIcon createImageIcon(String string) {
    // TODO Auto-generated method stub
    return null;
    }

    public GUI2(String arrayRecord) {
    this();

    try {
    BufferedReader reader = new BufferedReader(new FileReader(arrayRecord));
    String line = "";

    while ((line = reader.readLine()) != null) {
    lines.add(line);
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }

    public void showFile() {
    for (int i = 0; i < lines.size(); i++) {
    System.out.println(lines.get(i));
    }
    }

    public static void main(final String[] args) {
    try {
    SwingUtilities.invokeAndWait(new Runnable() {

    public void run() {
    // GUI2 f = new GUI2(args[0]);
    GUI2 f = new GUI2();
    f.setSize(200, 900);
    f.setVisible(true);
    }
    });
    } catch (Exception e) {
    System.out.println("Error ar main run: " + e);
    System.out.println("Error ar main run: " + e.getCause());
    }
    }

    private class HandlerClass implements ActionListener {

    public void actionPerformed(ActionEvent event) {
    textFrame = new JFrame("Inventory");
    textFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    textFrame.getContentPane();

    JTextArea textArea = new JTextArea();
    textFrame.add(textArea);

    Object button = event.getSource();

    if (first == button) {
    currentIndex = 0;
    } else if ((prev == button) && (currentIndex > 0)) {
    --currentIndex;
    } else if (next == button) {
    ++currentIndex;
    } else if (delete == button) {
    }

    // Wrap
    if (currentIndex == lines.size()) {
    currentIndex = 0;

    } else if (last == button) {
    currentIndex = lines.size() - 1;
    }

    textArea.setText(lines.get(currentIndex));
    textFrame.setSize(900, 150);
    textFrame.setVisible(true);
    }
    }

    private class addHandlerClass implements ActionListener {

    public void actionPerformed(ActionEvent event) {
    // panel = new JPanel();
    panel = new javax.swing.JDialog(); //new
    panel.setModal(true);//new
    panel.setLayout(new FlowLayout()); //new
    brandtext = new JTextField(10);
    unitstext = new JTextField(10);
    pricetext = new JTextField(10);
    panel.add(new JLabel("Enter Brand: "));
    panel.add(brandtext);
    panel.add(new JLabel("Enter Units:"));
    panel.add(unitstext);
    panel.add(new JLabel("Enter Price: "));
    panel.add(pricetext);
    // panel.setVisible(true);
    panel.setSize(200, 200);

    Object button = event.getSource();

    if (add == button) {
    panel.getContentPane(); //new
    panel.setVisible(true); //new
    // <-<- <-probelem


    brandtext.setText("");
    unitstext.setText("");
    pricetext.setText("");
    }

    }
    }
    }


    [/code]

    gl, se52
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion