patternjavaMinor
Music list program
Viewed 0 times
musiclistprogram
Problem
I need a little help with improving my first Java program. I have programmed for about a month, so my code is quite messy. The program is a "Musiclist" program, with which you can add songs, edit songs, and remove songs from the list.
The program uses 2
What I need help with is:
If you have any new suggestions for my program, I would love to include them.
```
public class MusicList extends JFrame{
public JTextField af;
private JList jl;
private JButton add;
private JButton edit;
private JButton test;
private JPanel jp;
private JScrollPane sp;
private JTextField artist;
private JButton save;
private JButton listb;
private JPopupMenu jpo;
private JMenuItem ite;
private JButton editsave;
private JButton editlist;
int g;
//creates a DefaultListModel.. actions at the JList can be made through here. e.g if adding to jlist is m.addElement();
DefaultListModel m = new DefaultListModel();
//creates arraylists
List fl = new ArrayList();
List art = new ArrayList();
public MusicList(){
super("Musiclist");
setLayout(null);
jl = new JList(m);
add(jl);
//creates a scrollpane, "implements jlist"
sp = new JScrollPane(jl);
sp.setBounds(30,20,150,140);
add(sp);
//creates the textfield to contain songname
af = new JTextField(12);
String afs = af.getText();
af.setBounds(20,20,170,20);
add(af);
af.setVisible(false);
The program uses 2
ArrayLists, to store the information about the song (Song name, Artist), which will be stored to the arraylist(indexnumber).What I need help with is:
- Make it less messy. Maybe add
ActionListenersto another class?
- The song- and artist
ArrayListthat shares index seems like a bad idea. I always have to perform 2 actions like song.add(index) and artist.add(index). Maybe a 2DArrayList?
- I need to add the songs to a database I can call it from, when I open the program next time.
If you have any new suggestions for my program, I would love to include them.
```
public class MusicList extends JFrame{
public JTextField af;
private JList jl;
private JButton add;
private JButton edit;
private JButton test;
private JPanel jp;
private JScrollPane sp;
private JTextField artist;
private JButton save;
private JButton listb;
private JPopupMenu jpo;
private JMenuItem ite;
private JButton editsave;
private JButton editlist;
int g;
//creates a DefaultListModel.. actions at the JList can be made through here. e.g if adding to jlist is m.addElement();
DefaultListModel m = new DefaultListModel();
//creates arraylists
List fl = new ArrayList();
List art = new ArrayList();
public MusicList(){
super("Musiclist");
setLayout(null);
jl = new JList(m);
add(jl);
//creates a scrollpane, "implements jlist"
sp = new JScrollPane(jl);
sp.setBounds(30,20,150,140);
add(sp);
//creates the textfield to contain songname
af = new JTextField(12);
String afs = af.getText();
af.setBounds(20,20,170,20);
add(af);
af.setVisible(false);
Solution
For cleaning up your code a bit,
-
Try to create methods for repeating code. For example, your adding lots of JButtons repeatedly. I'm not a UI expert but I think you can try something like this...
Then your code will look like this, which is much more readable, and will cut out about 3 lines of code per JButton (Note: some JButtons your setting visibility, you could overload a method for those as well, or include a boolean in above method for handling that in ALL JButtons)
As far as seperating your Action listeners, I agree with Roberts above comment. If you decide not to place them in a seperate file, I would at least seperate them into a private method, something like private void setActionListeners.
In general I like to modularize my code so it reads as close to this as possible...
- Avoid using abbreviations for your variables. If some other coder comes along and looks halfway through your massive class file, they will think what is a 'js'?
-
Try to create methods for repeating code. For example, your adding lots of JButtons repeatedly. I'm not a UI expert but I think you can try something like this...
private void addJButton(JButton button, String name, Rectangle rectangle){
button = new JButton(name);
button.setBounds(rectangle.getBounds());
add(button);
}Then your code will look like this, which is much more readable, and will cut out about 3 lines of code per JButton (Note: some JButtons your setting visibility, you could overload a method for those as well, or include a boolean in above method for handling that in ALL JButtons)
addJButton(add, "add", new Rectangle(20, 180, 80, 20));
addJButton(edit, "edit", new Rectangle(110, 180, 80, 20));As far as seperating your Action listeners, I agree with Roberts above comment. If you decide not to place them in a seperate file, I would at least seperate them into a private method, something like private void setActionListeners.
In general I like to modularize my code so it reads as close to this as possible...
public void someMainMethod(){
initialize();
calculations();
someMoreCalculations();
iReadLikeABook();
}Code Snippets
private void addJButton(JButton button, String name, Rectangle rectangle){
button = new JButton(name);
button.setBounds(rectangle.getBounds());
add(button);
}addJButton(add, "add", new Rectangle(20, 180, 80, 20));
addJButton(edit, "edit", new Rectangle(110, 180, 80, 20));public void someMainMethod(){
initialize();
calculations();
someMoreCalculations();
iReadLikeABook();
}Context
StackExchange Code Review Q#39498, answer score: 6
Revisions (0)
No revisions yet.