So I'm making a playlist software like itunes or spotify and I'm having trouble with my JTextfields. When I first read the songs the text that is too long over flows to the right like I want. But when I reorder the category by clicking on the detail labels at the top, the over flowing text overflows to the left. I can see the text first overflow to the right and then quickly readjust itself to the left.
Here's where I add each jtextfield to my jpanel, the bug might have something to do with my mouse listener but I'm not sure.
for (final List<JTextField> inner : songTextFields) {
if (inner.equals(songTextFields.get(songTextFields.size() - 1))) {
bottom = true;
}
for (int i = 0; i < details.size(); i++) {
final JTextField textField = inner.get(details.get(i));
textField.setPreferredSize(detailDimensions.get(i));
textField.setHorizontalAlignment(JTextField.LEFT);
if (leftSide) {
if (bottom) {
textField.setBorder(BorderFactory.createCompoundBorder(bottomLeftBorder, emptyBorder));
} else {
textField.setBorder(BorderFactory.createCompoundBorder(leftBorder, emptyBorder));
}
leftSide = false;
} else if (bottom) {
textField.setBorder(BorderFactory.createCompoundBorder(bottomBorder, emptyBorder));
} else {
textField.setBorder(emptyBorder);
}
textField.setFont(playlistFont);
textField.setBackground(textFieldBackground);
textField.setForeground(Color.WHITE);
textField.setOpaque(true);
textField.setEditable(false);
// textField listeners
final int usedI = i;
textField.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
unEditLastTextField(textField);
textField.setEditable(true);
textField.setSelectionStart(0);
textField.setSelectionEnd(textField.getText().length());
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
songs.get(songTextFields.indexOf(inner)).setByInt(details.get(usedI), textField.getText());
textField.setText(textField.getText());
textField.setEditable(false);
}
}
});
}
}
});
textField.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
// TODO: play song
}
}
});
if (i == details.size() - 1) {
gc.anchor = GridBagConstraints.LINE_START;
if (bottom) {
textField.setBorder(BorderFactory.createCompoundBorder(bottomRightBorder, emptyBorder));
} else {
textField.setBorder(BorderFactory.createCompoundBorder(rightBorder, emptyBorder));
}
//textField.setPreferredSize(detailDimensions.get(i));
//textField.setHorizontalAlignment(JTextField.LEFT);
panel.add(textField, gc);
} else {
//textField.setPreferredSize(detailDimensions.get(i));
//textField.setHorizontalAlignment(JTextField.LEFT);
panel.add(textField, gc);
gc.gridx++;
}
System.out.println(textField.getText() + " : " + textField.getPreferredSize() + " " + textField.getHorizontalAlignment());
}
leftSide = true;
bottom = false;
textFieldBackground = (textFieldBackground == lightColor ? darkColor : lightColor);
gc.gridx = 0;
gc.gridy++;
gc.anchor = GridBagConstraints.LINE_END;
}
Here's where I'm creating each textfield. The labels will sort if a detail label is clicked and the songsortby string isn't already equal to that label.
private List<List<JTextField>> getSongTextFields(List<Song> songs) {
switch (songSortBy) {
case "Title":
songs = sortSongs(0, songs);
break;
case "Artist":
songs = sortSongs(1, songs);
break;
case "Album":
songs = sortSongs(2, songs);
break;
case "Year":
songs = sortSongs(3, songs);
break;
case "Genre":
songs = sortSongs(4, songs);
break;
case "Features":
songs = sortSongs(5, songs);
break;
case "Producers":
songs = sortSongs(6, songs);
break;
default:
break;
}
List<List<JTextField>> textFields = new ArrayList<>();
for (Song song : songs) {
List<JTextField> innerLabels = new ArrayList<>();
innerLabels.add(new JTextField(song.getTitle()));
innerLabels.add(new JTextField(song.getArtist()));
innerLabels.add(new JTextField(song.getAlbum()));
innerLabels.add(new JTextField(song.getYear()));
innerLabels.add(new JTextField(song.getGenre()));
innerLabels.add(new JTextField(song.getFeatures()));
innerLabels.add(new JTextField(song.getProducers()));
textFields.add(innerLabels);
}
return textFields;
}
And finally this is where I'm sorting my songs, I don't think the bug is occurring in this method but you never know.
private List<Song> sortSongs(int order, List<Song> songs) {
List<Song> sortedList = new ArrayList<>();
int j;
for (int i = 0; i < songs.size(); i++) {
j = 0;
while (j < sortedList.size() && songs.get(i).getByInt(order).toUpperCase().compareTo(
sortedList.get(j).getByInt(order).toUpperCase()) >= 0) {
j++;
}
sortedList.add(j, songs.get(i));
}
return sortedList;
}
If you guys want to see any other parts of my code just to let me know, thanks for the help.