开发者

Writing something in JList

hey i have another problem. I created JList in my main window and now i want to add something to it. I do it this way...

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) 

{          

           Dodaj_Przedmiot dodaj_przedmiot = new Dodaj_Przedmiot(null, true);
           dodaj_przedmiot.setVisible(true);
           SterowanieBazy instance = SterowanieBazy.getInstance();       
           Zmienne_pomocnicze zp = new Zmienne_pomocnicze();
           String przedmiot = zp.getPrzechowaj();
           instance.dodajPrzedmiot(przedmiot);
           String przedm[] = instance.zwrocPrzedmioty();
           jList1.setListData(przedm);
}

what i want to write in that list is what i collect from my jDialogForm: dodaj_przedmiot

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 

{

        String sciezka = jTextField1.getText();
        if (sciezka.length() > 0)
        {
          Zmienne_pomocnicze zp = new Zmienne_pomocnicze();
          zp.setPrzechowaj(sciezka);

        }
        this.setVisible(false);
    }                            

and i collect try to copy that date using this class

public class Zmienne_pomocnicze {

public String n;
public int a;

public void setPrzechowaj (String neew)

{

    n = neew;
}

public String getPrzechowaj ()

{

    return n;
}

}

i would be grateful f开发者_如何学Cor any ideas how to make it work.


This is somewhat difficult to follow, but from what I gather, you are using your Zmienne_pomocnicze class in two places, and both of them seem to do nothing.

First, in jButton2ActionPerformed you instantiate a new Zmienne_pomocnicze and try to get the data from it using the getPrzechowaj method. This will return n, but as you have just instantiated the instance, n is null. As I cant infer from the method names of the following code, I cant figure out what you want to do with that data, but this action is most certainly not what you want to do.

In the second case, jButton1ActionPerformed takes the value from the text field and then test for validity (legnth is greater than 0). If the validation passes, you then create a new Zmienne_pomocnicze, call setPrezechowaj with the text field value and then let the new object fall out of scope. Again, this is certainly not the desired effect.

It would be interesting to see what the flow of your program is supposed to be, ie what button triggers which jButton[12]ActionPerformed methods and how you expect them to interact.


Here's a simple example of adding entries to a JList.

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

public class JListTest {

    private static final Random random = new Random();

    public static final void main(String args[]) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        final JFrame frame = new JFrame("Test");
        final DefaultListModel dlm = new DefaultListModel();
        final JList list = new JList(dlm);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(new JScrollPane(list));
        frame.add(new JButton("Add") {
            {
                addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        dlm.addElement("A" + (random.nextInt(9000) + 1000));
                    }
                });
            }
        }, BorderLayout.SOUTH);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }
}


I always recommend reading the API for basic information.

If you read the JList API you will find a link to the Swing tutorial on "How to Use Lists". The example there shows how to dynamically add and remove entries from the ListModel.

Tutorials are a good place to start because you find working examples as well as explanations as to how the code works. Then, if required you can ask a specific question about a specific piece of code.

Not only that you now have a reference that might come in handy for other problems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜