views:

141

answers:

2

Hi!

I am a beginner with java and has got a problem that I just cant solve.

I am trying to add strings to my array, I have tested my array so that work. But my problem is that I have created an actionlistener and trying to get the text from another class and then add it to the array.

My Buttonlistener:

public class ButtonListener extends AddToLibrary implements ActionListener {
public void actionPerformed(ActionEvent e) {
    Database dt = new Database();
    dt.add(textType, textTitle, textSort, textDesc);
}   }

I got a friend who told me that I am creating a new database every time I pushes the button, but how do I do if I just want to "load" it? Can clear that database is the classname for my array.

The more "funny" part of this is that when I run it in eclipse it goes to debugger without showing me anything clear that is wrong, and because of my limited knowledge in java this is too much to me.

My buttonlistener is geting the information from AddToLibrary and it looks like this:

public class AddToLibrary extends JPanel{
public String textTitle;
public String textSort;
public String textDesc;
public String textType;

public AddToLibrary() {
    // Förklarande text
    JLabel titel = new JLabel("Titel");
    JLabel sort = new JLabel("Genre");
    JLabel desc = new JLabel("Beskriving");

    // Textrutor
    JTextField textTitel = new JTextField(null, 20);
    textTitel.setToolTipText("ex. Flickan som lekte med elden");
    JTextField textSort = new JTextField(null, 10);
    textSort.setToolTipText("ex. Skräck, Action");
    JTextField textDesc = new JTextField(null, 15);
    textDesc.setToolTipText("ex. Stieg Larsson");

    // Knappar
    JButton addButton = new JButton("Lägg till");
    addButton.addActionListener(new ButtonListener());      //Lyssna på knapp

    // Combobox
    JComboBox comboBox = new JComboBox();
    comboBox.addItem("Film");
    comboBox.addItem("CD");
    comboBox.addItem("Bok");
    comboBox.addItem("Annat");

    // Lägg till i panelen
    add(titel);
    add(textTitel);
    add(sort);
    add(textSort);
    add(desc);
    add(textDesc);
    add(comboBox);
    add(addButton);


}

public String getTitelText(JTextField titelText) {
    textTitle = "" +  titelText.getText();
    return textTitle;
}

public String getDescText(JTextField descText) {
    textDesc = "" + descText.getText();
    return textDesc;
}

public String getSortText(JTextField sortText) {
    textSort = "" + sortText.getText();
    return textSort;
}

public String getTypeText(JComboBox comboBox) {
    return textType = "" + (String) comboBox.getSelectedItem() + ".png";
}
}

But it do not work and I cant understand why it isnt working, so if anyone has some time over to help me I would be pleased.

Thanks!

A: 
public class ButtonListener extends AddToLibrary implements ActionListener {
    private Database dt = new Database();
    public void actionPerformed(ActionEvent e) {
       dt.add(textType, textTitle, textSort, textDesc);
    }   
}

should work. Or better, the database should be created in AddToLibrary and passed to ButtonListener in its constructor. Sorry I do not have the time to check the code for you, but if this does not work, you can notify.

Amit Kumar
Thanks for the try, but it still will not work, but thanks for the try!
+1  A: 

One fault is here:

public class ButtonListener extends AddToLibrary implements ActionListener {

extending AddToLibrary creates a weird inheritance problem.

The simple solution is to define the ButtonListener inline:

final Database dt = new Database();
addButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            dt.add(getTypeText(comboBox), getTitelText(textTitel), getSortText(textSort), getDescText(textDesc));
        }
}); // Lyssna på knapp

One important change is to create one instance of Database to which the strings are added (as Amit Kumar already pointed out).

There were a lot of problems with your code, mostly illegal constructions. My advice is to get a good Java tutorial/book and take notice of how they solve the problems. Also if you use Eclipse (or another modern IDE) it will notify you of any illegal constructions and will try to propose solutions.

One last note, the public Strings and the JTextFields have the same name, this creates a problem for the computer as it does not know which one you are referring to (this is called shadowing). Define unique names for each variable within a class so you do not confuse the compiler or yourself.

=====================================

I have done a little work on your code and I arrived at the following. (It may be improved even further, but this is at least a lot better in terms of legality and readability)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class AddToLibrary extends JPanel {
    private static final long serialVersionUID = 1L;

    private Database database = new Database();

    private JComboBox comboBox = new JComboBox(new String[]{"Film", "CD", "Bok", "Annat"});
    private JButton addButton = new JButton("Lägg till");

    private JTextField textTitel = new JTextField(null, 20);
    private JTextField textSort = new JTextField(null, 10);
    private JTextField textDesc = new JTextField(null, 15);

    private JLabel titel = new JLabel("Titel");
    private JLabel sort = new JLabel("Genre");
    private JLabel desc = new JLabel("Beskriving");

    public AddToLibrary() {
        textTitel.setToolTipText("ex. Flickan som lekte med elden");
        textSort.setToolTipText("ex. Skräck, Action");
        textDesc.setToolTipText("ex. Stieg Larsson");

        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                database.add(comboBox.getSelectedItem() + ".png", 
                             textTitel.getText(), 
                             textSort.getText(), 
                             textDesc.getText()
                )
            }
        }); // Lyssna på knapp

        // Lägg till i panelen
        add(titel);
        add(textTitel);
        add(sort);
        add(textSort);
        add(desc);
        add(textDesc);
        add(comboBox);
        add(addButton);
    }
}
NomeN
Ahh, it is hard to believe that it does almost the same, it is much more beautiful on your way, thanks for taking your time!