I have a song library, and I would like this selection list to only be on the left hand side of the window because I want to put other information about the song on the righthand side. I'm not sure how to change the size of JScrollPane
, which is inside the JFrame
.
In this library, I want to be able to import the songs stored in a file to my song library. Right now, I have an array within my code, but I want to be able to read from a text file instead of using this approach. In the file, I want to be able to store artist and album information about the song, but I don't want it to display in the song list.
String songs[] = {"Song1", "Song2", "Song3", "Song4", "Song5"};
JList list = new JList(songs);
public SongLib(){
JFrame songLibrary = new JFrame("Song Library");
songLibrary.setLocationRelativeTo(null);
songLibrary.setResizable(true);
songLibrary.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
list.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent evt){
int i = list.getSelectedIndex();
if (i != -1)
System.out.println("Selected: " + songs[i]);
else
System.out.println("Choose a song");
}
});
JScrollPane JSPane = new JScrollPane(list);
JSPane.setPreferredSize(new Dimension(100,100));
songLibrary.add(JSPane);
songLibrary.setSize(400,400);
songLibrary.setVisible(true);
}
public static void main(String[] args){
new SongLib();
}
setPreferredSize()
. Meaning that this call: JSPane.setPreferredSize(new Dimension(100,100));
should definitely be removed. JSplitPane
. If you don't want the draggable divider, use a JPanel
with an appropriate LayoutManager
( GridBagLayout
may be a good choice) In addition to @Guillaume Polet's good advice , setVisibleRowCount()
may be useful to you going forward.
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/** @see https://stackoverflow.com/a/14801908/230513 */
public class SongLib {
String songs[] = {"Song1", "Song2", "Song3", "Song4", "Song5"};
JList list = new JList(songs);
public SongLib() {
JFrame songLibrary = new JFrame("Song Library");
songLibrary.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
list.setVisibleRowCount(4);
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent evt) {
int i = list.getSelectedIndex();
if (i != -1) {
System.out.println("Selected: " + songs[i]);
} else {
System.out.println("Choose a song");
}
}
});
JScrollPane JSPane = new JScrollPane(list);
songLibrary.add(JSPane);
songLibrary.pack();
songLibrary.setLocationRelativeTo(null);
songLibrary.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new SongLib();
}
});
}
}
For 1., take a look at layouting in Java, especially the GridBagLayout. http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
For 2: Reading and writing from / into files is made by using File-Objects and Inout/Output-Streams. But this is a very low level way to do this. I think you should consider using an XML file. Take a look at JAXB
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.