Adding to a JList with an ActionListener and refresihing GUI frame after adding
I am working on a project (yes it is for school, no I don't want anyone to write it for me. I have too much pride!) using some of the GUI components of java. It is still in a fairly rough stage, and a single thing is keeping me from getting this finished. I try not to ask for help unless I really need it because usually when I do ask it turns out to be a simple mistake, so if that is the case here, take it easy on me. Anyway, so on to the code. This is a group project so some of my comments are to my partner. I would ask them, but it is 4am... Anyway, here it is. Not sure why it is in all these separate boxes. The listener I am messing with is ActionPerformed, near the bottom. I thank you graciously in advance for any help.
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI extends JFrame
{
private JPanel panel;
private JFrame frame;
private JTextArea text;
private MP3List list = new MP3List();
private JList songList;
private JScrollPane scrollList;
private JMenuBar menuBar;
private JMenu menu;
private JMenuItem menuAdd;
private String[] songs;
private static String mp3msg = "Project 2: MP3 Tracker"; // Header for JOptionPane
public GUI()
{
super("mp3");
panel = new JPanel();
createGUI();
add(panel);
}
public void createGUI()
{
//This creates the frame(createGUI)
frame = new JFrame();
//Here, I made an array of the song titles and gave them to a JList
//for display. Do you think we should sort the songs?
songs = new String[list.getSize()];
for (int i = 0; i < list.getSize(); i++开发者_Go百科)
{
songs[i] = list.get(i).getSongTitle();
}
songList = new JList(songs);
//Set the selection mode to single as I want to fill in fields with info on a clicked song. More on
//that to come.
songList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//Made a scroll bar(vertical and horizontal just in case)
scrollList = new JScrollPane(songList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
text = new JTextArea(30, 30);
text.setText("This section will hold information about songs and somesuch.");
menuBar = new JMenuBar();
menu = new JMenu("File");
menuAdd = new JMenuItem("Add Song");
menuAdd.addActionListener(new menuListener());
menu.add(menuAdd);
menuBar.add(menu);
frame.setLayout(new Border());
songList.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
int selectedVar;
selectedVar = songList.getSelectedIndex();
text.setText(("Song Title: " + list.get(selectedVar).getSongTitle())
+ ("\nArtist: " + list.get(selectedVar).getArtistName())
+ ("\nPlayback Time: " + list.get(selectedVar).getPlayBackTime())
+ (" || Cost: " + list.get(selectedVar).getDownloadCost())
+ (" || Size: " + list.get(selectedVar).getFileSize()));
}
});
}
public class Border extends JFrame implements LayoutManager
{
private static final long serialVersionUID = 1L;
private final int WINDOW_WIDTH = 400;
private final int WINDOW_HEIGHT = 300;
public Border()
{
super("MP3 Editor");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(scrollList, BorderLayout.CENTER);
add(text, BorderLayout.SOUTH);
setJMenuBar(menuBar);
setVisible(true);
}
@Override
public void addLayoutComponent(String name, Component comp)
{
// TODO Auto-generated method stub
}
@Override
public void layoutContainer(Container parent)
{
// TODO Auto-generated method stub
}
@Override
public Dimension minimumLayoutSize(Container parent)
{
// TODO Auto-generated method stub
return null;
}
@Override
public Dimension preferredLayoutSize(Container parent)
{
// TODO Auto-generated method stub
return null;
}
@Override
public void removeLayoutComponent(Component comp)
{
// TODO Auto-generated method stub
}
}
public class menuListener extends JMenuItem implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
/*public menuListener()
{
menuItem.addActionListener(this);
}*/
public void actionPerformed(ActionEvent e)
{
MP3 aSong = getInfo();
list.add(aSong);
songs = new String[list.getSize()];
for (int i = 0; i < list.getSize(); i++)
{
songs[i] = list.get(i).getSongTitle();
}
songList = new JList(songs);
scrollList = new JScrollPane(songList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
int index = 0;
songList.setSelectedIndex(index);
songList.ensureIndexIsVisible(index);
}
}
/**
* Input one set of MP3 file information and produce a report <br>
* <p/>
* <hr>
* Date created: Sep 22, 2010 <br>
* Date last modified: Sep 22, 2010 <br>
* <p/>
* <hr>
*/
public static MP3 getInfo()
{
// Gather all information using JOptionPane
String title = JOptionPane.showInputDialog(null,
"Enter Title: ",
mp3msg,
JOptionPane.QUESTION_MESSAGE);
String artist = JOptionPane.showInputDialog(null,
"Enter Artist: ",
mp3msg,
JOptionPane.QUESTION_MESSAGE);
int seconds = Integer.parseInt(JOptionPane.showInputDialog(null,
"Enter playback time in seconds: ",
mp3msg,
JOptionPane.QUESTION_MESSAGE));
double cost = Double.parseDouble(JOptionPane.showInputDialog(null,
"Enter download cost: ",
mp3msg,
JOptionPane.QUESTION_MESSAGE));
double size = Double.parseDouble(JOptionPane.showInputDialog(null,
"Enter file size in megabytes (MB): ",
mp3msg,
JOptionPane.QUESTION_MESSAGE));
MP3 asong = new MP3(title, artist, seconds, cost, size);
return asong;
}
}
There are a number of little issues with your code, but I think the most important one for you is the part where you add a new MP3 in the action listener (the part you said you want help with). Here, you create a brand new JList
and you overwrite the previous JList
, and you create a new scroll pane for it, but you don't add the new JList to your GUI so the old one remains (and so you never see any changes).
Ideally, you should keep the same list throughout, and just update the contents. There are several ways to do that, but one of the easiest is to simply call songList.setListData(songs)
. Or, instead of creating an array of Strings, create a Vector
because JLists work with Vectors too.
Other issues you might want to look at:
private JFrame frame;
is never used.- Your class
Border
is aJFrame
but is also used as aLayoutManager
, which is extremely confusing and wrong. You should get rid of that.
Personally, I'd recommend you rewrite the GUI from scratch. Here's a simple example with a working JList for you to examine:
package examples;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
public class GUI extends JFrame {
private final Vector<String> myVector = new Vector<String>();
private final JList myList = new JList();
public static void main(String... args) {
new GUI().setVisible(true);
}
public GUI() {
super("List");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(new JButton(new AddItemAction()), BorderLayout.NORTH);
add(new JScrollPane(myList), BorderLayout.CENTER);
pack();
}
private class AddItemAction extends AbstractAction {
public AddItemAction() {
super("Add Item");
}
@Override
public void actionPerformed(ActionEvent e) {
String newItem = JOptionPane.showInputDialog("Add a new item:");
if (newItem != null) {
myVector.add(newItem);
myList.setListData(myVector);
}
}
}
}
Read the JList API and follow the link to the secton in the Swing tutorial on "How to Use Lists" which gives a working examples of adding a value to a JList when a button is clicked. Compare you code with the working code to see what the difference is.
精彩评论