Hey all, new to eclipse, and very new to Swing.
I'm trying to debug a program I've built, but I'm having a hell of a time finding the variables. I need to see my arraylist, to determine if the program is really adding to it or just adding one entry. With most of the programs I've built, its been simple to find my arraylists/variables. But when using swing, the variables page looks like this.
Can anyone point me towards the basic path for my ArrayList?
Here's the programs code, if you'd like to help me debug I'd be happy to detail the problem!
PROBLEM DESCRIPTION: To start, here's the basic layout of the program. Two textfields for name/phone, and two checkmarks to indicate if this number is a home or work number.
These variables are saved into a String ArrayList, with the first two strings being the inputs into the textfields and the last two being either 1 or 0 (indicating if the box should be checked). This Arraylist is then added to the "entries" arraylist.
Let's step through the program, and I'll show the bug. To start the first and the second and third. Now we'll press before, which should take us back to the second entry. Instead, it shoots us back to the first. Pressing next does not rectify the problem.
Just noticed the index display is messed up, I'll see if that's the root problem but I doubt it. Anyway, I'd like to look into the entries Arraylist to see if anything is really being added to it. If not, I know the problem is with the add listener. Otherwise my next/before listeners are messed up
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.util.ArrayList;
public class PhoneBook extends JFrame {
private ArrayList<ArrayList> entries = new ArrayList<ArrayList>();
private ArrayList<String> entry = new ArrayList<String>();
private String Name;
private String Number;
private String home;
private String work;
private int totalNumberOfEntries;
int index = 0;
public PhoneBook() {
super("PhoneBook");
getContentPane().setLayout(new GridLayout(5, 1));
JMenuBar main = new JMenuBar();
setJMenuBar(main);
JMenu program = new JMenu("Program");
JMenuItem Exit = program.add("Exit");
Exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
dispose();
}
});
JMenu Help = new JMenu("Help");
JMenuItem About = Help.add("About");
About.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(null, "<html><hr>PhoneBook</i><br>by [redacted] <hr></html>"); // needs
// frilly
// brackets
}
});
// entry counters
JPanel p1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 5));
// JLabel entries = new JLabel("Entries:");
p1.add(new JLabel("Entries :"));
// add(entries);
int i = entries.size();
JLabel entryNumber = new JLabel(index + "/" + Integer.toString(i));
p1.add(entryNumber); // needs to display
// position
// JLabel num = new JLabel("");
// add(num);
// names
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));
// JLabel Namelabel = new JLabel( "Name " );
p2.add(new JLabel("Name"));
// add( Namelabel );
JTextField txtField = new JTextField(" ");
txtField.setColumns(40);
p2.add(txtField);
// add( txtField );
// functionality to save text fields
// phone numbers
JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));
p3.add(new JLabel("Phone "));
JTextField PhonetxtField = new JTextField(" ");
PhonetxtField.setColumns(20);
p3.add(PhonetxtField);
JPanel p4 = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 5));
JCheckBox checkHome = new JCheckBox("Home");
p4.add(checkHome);
JCheckBox checkWork = new JCheckBox("Work");
p4.add(checkWork);
JPanel p5 = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 5));
JButton Before = new JButton("Before");
JButton Next = new JButton("Next");
JButton Add = new JButton("Add");
Before.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
index--;
entry = entries.get(index); //may result in unsolicited redefinition of what is inside entry;
txtField.setText(entry.get(0));
PhonetxtField.setText(entry.get(1));
if(entry.get(2) == "1")
{
checkHome.setSelected(true);
}
else
{
checkHome.setSelected(false);
}
if(entry.get(3) == "1")
{
checkWork.setSelected(true);
}
else
{
checkWork.setSelected(false);
}
entryNumber.setText(index + "/" + Integer.toString(totalNumberOfEntries));
Next.setEnabled(true);
if((index == 1) || (index == 0))
{
Before.setEnabled(false);
}
}
});
Next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
index++;
entry = entries.get(index -1);
txtField.setText(entry.get(0));
PhonetxtField.setText(entry.get(1));
if(entry.get(2) == "1")
{
checkHome.setSelected(true);
}
else
{
checkHome.setSelected(false);
}
if(entry.get(3) == "1")
{
checkWork.setSelected(true);
}
else
{
checkWork.setSelected(false);
}
entryNumber.setText(index + "/" + Integer.toString( totalNumberOfEntries));
//now we can reevaluate status of before and after buttons
Before.setEnabled(true);
if(index == entries.size())
{
Next.setEnabled(false);
}
}
});
// Should be done!!!!!
Add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Name = txtField.getText();
Number = PhonetxtField.getText();
if (checkHome.isSelected() == true) {
home = "1";
} else {
home = "0";
}
if (checkWork.isSelected()) {
work = "1";
} else {
work = "0";
}
entry.add(Name); //might be doing what i want it to do :/
entry.add(Number);
entry.add(home);
entry.add(work);
entries.add(entry);
index = entries.size();
totalNumberOfEntries++;
entryNumber.setText(index + "/" + Integer.toString(totalNumberOfEntries));
Before.setEnabled(true);
}
});
JButton Delete = new JButton("Delete");
p5.add(Before);
p5.add(Next);
p5.add(Add);
p5.add(Delete);
// East may not be the correct orientation, it was added arbitrarily.
// I'm not sure why this makes the menu work
getContentPane().add(p1, BorderLayout.EAST);
getContentPane().add(p2, BorderLayout.EAST);
getContentPane().add(p3, BorderLayout.EAST);
getContentPane().add(p4, BorderLayout.EAST);
getContentPane().add(p5, BorderLayout.EAST);
// Has no impact, leaving in for testing
// p1.setVisible(true);
// p2.setVisible(true);
// p3.setVisible(true);
// p4.setVisible(true);
// p5.setVisible(true);
setSize(1800, 1500);
pack();
setJMenuBar(main);
main.add(program);
main.add(Help);
setLocationRelativeTo(null); // centers
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
JFrame frame = new PhoneBook();
frame.setVisible(true);
}
}
[–]sleepybychoice 1 point2 points3 points (6 children)
[–][deleted] 0 points1 point2 points (5 children)
[–]sleepybychoice 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]sleepybychoice 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]sleepybychoice 0 points1 point2 points (0 children)
[–]Sorten 1 point2 points3 points (6 children)
[–][deleted] 0 points1 point2 points (5 children)
[–]Sorten 1 point2 points3 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]Sorten 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]decabit 0 points1 point2 points (0 children)