1

I'm creating a mp3Tagger app in Java, using JAudioTagger http://www.jthink.net/jaudiotagger/ The app actually shows the title of the track, the number, the album, the artist, the year, the genre and the comments included in the Metadatas. It should show the artwork of the album on a JPanel in a corner of the main JFrame... But it doesn't. I think that the Jpanel doesn't work: it doesn't show neither JTextField or Button... The code:

class JPanelArt extends JComponent {

    private static final long serialVersionUID = 1L;
    private Image image;

    public JPanelArt() {
        try {
            image = ImageIO.read(new File("res/download.jpg"));
        } catch (IOException e) {}
    }

    protected void paintComponent(Graphics g) {
        if (image == null)
            return;
        g.drawImage(image, 0, 0, null);
    }

}

Frame Class:

public class Mp3TaggerFrame extends JFrame {


private static final long serialVersionUID = 1L;

    static AudioFile mp3;

    private JPanel contentPane;
    private JPanelArt albumArt;
    private JLabel imageArt;
    private JTextField titleTrack;
    private JTextField artist;
    private JTextField album;
    private JTextField genre;
    private JTextField trackNumber;
    private JTextField lenght;
    private JTextField year;
    private JTextField comment;
    private JTextField bitRate;

    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Mp3 File",
            "mp3");

    public void setAlbumArt(BufferedImage _image) {


    }

    public String getTitleTrack() {
        return titleTrack.getText();
    }

    public void setTitleTrack(String title) {
        this.titleTrack.setText(title);
    }

    public String getArtist() {
        return artist.getText();
    }

    public void setArtist(String artist) {
        this.artist.setText(artist);
    }

    public String getAlbum() {
        return album.getText();
    }

    public void setAlbum(String album) {
        this.album.setText(album);
    }

    public String getGenre() {
        return genre.getText();
    }

    public void setGenre(String genre) {
        this.genre.setText(genre);
    }

    public int getTrackNumber() {
        return Integer.valueOf(trackNumber.getText());
    }

    public void setTrackNumber(String string) {
        this.trackNumber.setText(string);
    }

    public void setLenght(int lenght) {
        int min = 0;
        int sec = lenght;
        while (sec >= 60) {
            sec = sec - 60;
            min++;
        }
        this.lenght.setText(String.valueOf(min) + ":" + String.valueOf(sec));
    }

    public int getYear() {
        return Integer.valueOf(year.getText());
    }

    public void setYear(String year) {
        this.year.setText(year);
    }

    public String getComment() {
        return comment.getText();
    }

    public void setComment(String comment) {
        this.comment.setText(comment);
    }

    public String getBitRate() {
        return bitRate.getText();
    }

    public void setBitRate(String bitRate) {
        this.bitRate.setText(bitRate);
    }

    /**
     * Create the frame.
     */
    public Mp3TaggerFrame() {
        try {
            UIManager.setLookAndFeel(new SyntheticaBlackStarLookAndFeel());
        } catch (Exception e) {
        }

        chooser.setFileFilter(filter);

        setFont(new Font("Century Schoolbook", Font.PLAIN, 14));
        setResizable(false);
        setTitle("Mp3Tagger");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 562, 423);

        setIconImage(new ImageIcon("res/Icon.png").getImage());

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu mnFile = new JMenu("File");
        menuBar.add(mnFile);

        JMenuItem openMp3File = new JMenuItem("Open Mp3 File");
        mnFile.add(openMp3File);
        openMp3File.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                openFile();
            }
        });

        JMenuItem saveWithNewTag = new JMenuItem("Save With New Tag");
        mnFile.add(saveWithNewTag);
        saveWithNewTag.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                saveFile();
            }
        });

        JMenuItem mntmExit = new JMenuItem("Exit");
        mnFile.add(mntmExit);

        JMenu mnAbout = new JMenu("About");
        menuBar.add(mnAbout);

        JMenuItem mntmAboutTheProgram = new JMenuItem("About The Program");
        mnAbout.add(mntmAboutTheProgram);

        JMenuItem mntmContacts = new JMenuItem("Contacts");
        mnAbout.add(mntmContacts);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblTitle = new JLabel("Title:");
        lblTitle.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblTitle.setBounds(6, 6, 122, 28);
        contentPane.add(lblTitle);

        titleTrack = new JTextField();
        titleTrack.setToolTipText("Title Of The Track");
        titleTrack.setBounds(6, 32, 122, 28);
        contentPane.add(titleTrack);
        titleTrack.setColumns(10);

        JLabel lblArtist = new JLabel("Artist:");
        lblArtist.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblArtist.setBounds(6, 72, 122, 28);
        contentPane.add(lblArtist);

        artist = new JTextField();
        artist.setToolTipText("Artist");
        artist.setColumns(10);
        artist.setBounds(6, 98, 122, 28);
        contentPane.add(artist);

        JLabel lblAlbum = new JLabel("Album:");
        lblAlbum.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblAlbum.setBounds(6, 138, 122, 28);
        contentPane.add(lblAlbum);

        album = new JTextField();
        album.setToolTipText("Album");
        album.setColumns(10);
        album.setBounds(6, 164, 122, 28);
        contentPane.add(album);

        JLabel lblGenre = new JLabel("Genre:");
        lblGenre.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblGenre.setBounds(6, 270, 122, 28);
        contentPane.add(lblGenre);

        genre = new JTextField();
        genre.setToolTipText("Genre Of The Track");
        genre.setColumns(10);
        genre.setBounds(6, 296, 122, 28);
        contentPane.add(genre);

        JLabel lblTrackNumber = new JLabel("Track Number:");
        lblTrackNumber.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblTrackNumber.setBounds(6, 204, 122, 28);
        contentPane.add(lblTrackNumber);

        trackNumber = new JTextField();
        trackNumber.setToolTipText("Number of the track in the album");
        trackNumber.setColumns(10);
        trackNumber.setBounds(6, 230, 122, 28);
        contentPane.add(trackNumber);

        JLabel lblAlbumArt = new JLabel("Album Art");
        lblAlbumArt.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblAlbumArt.setBounds(375, 6, 73, 28);
        contentPane.add(lblAlbumArt);

        albumArt = new JPanelArt();
        albumArt.setToolTipText("The Album Cover");
        albumArt.setBounds(375, 192, 150, -150);
        contentPane.add(albumArt);

        JLabel lblLenght = new JLabel("Lenght");
        lblLenght.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblLenght.setBounds(375, 204, 122, 28);
        contentPane.add(lblLenght);

        lenght = new JTextField();
        lenght.setEditable(false);
        lenght.setToolTipText("Lenght of the song");
        lenght.setColumns(10);
        lenght.setBounds(375, 230, 122, 28);
        contentPane.add(lenght);

        JButton editAlbumArt = new JButton("Edit");
        editAlbumArt.setBounds(460, 6, 55, 28);
        contentPane.add(editAlbumArt);

        JLabel lblYear = new JLabel("Year:");
        lblYear.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblYear.setBounds(181, 6, 122, 28);
        contentPane.add(lblYear);

        year = new JTextField();
        year.setToolTipText("Year Of The Track");
        year.setColumns(10);
        year.setBounds(181, 32, 122, 28);
        contentPane.add(year);

        JLabel lblComments = new JLabel("Comments:");
        lblComments.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblComments.setBounds(181, 72, 122, 28);
        contentPane.add(lblComments);

        comment = new JTextField();
        comment.setToolTipText("Comments Of The Track");
        comment.setColumns(10);
        comment.setBounds(181, 98, 122, 160);
        contentPane.add(comment);

        JLabel lblBitrate = new JLabel("Bit Rate");
        lblBitrate.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblBitrate.setBounds(375, 270, 122, 28);
        contentPane.add(lblBitrate);

        bitRate = new JTextField();
        bitRate.setToolTipText("BitRate of the song");
        bitRate.setEditable(false);
        bitRate.setColumns(10);
        bitRate.setBounds(375, 296, 122, 28);
        contentPane.add(bitRate);
    }

    private void openFile() {
        if (chooser.showOpenDialog(chooser) == JFileChooser.APPROVE_OPTION) {
            try {
                mp3 = AudioFileIO.read((new File(chooser.getSelectedFile()
                        .getAbsolutePath())));
            } catch (CannotReadException | IOException | TagException
                    | ReadOnlyFileException | InvalidAudioFrameException e) {
                e.printStackTrace();
            }
        } else
            System.out.println("Nessun File Selezionato");

        setTextFields();

    }

    private void setTextFields() {
        Tag tag = mp3.getTag();
        AudioHeader ah = mp3.getAudioHeader();

        setTitleTrack(tag.getFirst(FieldKey.TITLE));
        setAlbum(tag.getFirst(FieldKey.ALBUM));
        setArtist(tag.getFirst(FieldKey.ARTIST));
        setGenre(tag.getFirst(FieldKey.GENRE));
        setTrackNumber(tag.getFirst(FieldKey.TRACK));
        setLenght(ah.getTrackLength());
        setYear(tag.getFirst(FieldKey.YEAR));
        setComment(tag.getFirst(FieldKey.COMMENT));
        setBitRate(ah.getBitRate() + "Kbs");
        try {
            setAlbumArt((BufferedImage) tag.getFirstArtwork().getImage());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    private void saveFile() {


    }
}
Syrinxos
  • 137
  • 1
  • 2
  • 8
  • 3
    You don't need `JComponent#setVisible()`; call `JFrame#pack()` _then_ `JFrame#setVisible()`. For additional guidance, please edit your question to include a [complete example](http://stackoverflow.com/help/mcve) that exhibits the problem you outline. – trashgod Nov 21 '14 at 12:07
  • Still doesn't work, i've added all the Frame Class. I've used Window Builder Plug-in for eclipse to build the GUI – Syrinxos Nov 21 '14 at 12:38
  • `contentPane.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). It is probably also why the panel is not appearing.. But post an MCVE as advised by @trashgod for more definite answers. – Andrew Thompson Nov 21 '14 at 13:21
  • Are you saying you don't see something like [this](http://i.stack.imgur.com/fDI4R.png)? BTW - I shifted the album art up 50 pixels, because it was overlapping the last two labels.. – Andrew Thompson Nov 21 '14 at 13:36
  • Wow D: How do you make it shows? I'm still unable to see the Panel... – Syrinxos Nov 21 '14 at 14:15

2 Answers2

3

How do you make it shows? I'm still unable to see the Panel..

enter image description here

I made some changes, but in the course of (beginning) to make an MCVE, I got this code to make the image above.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Mp3TaggerFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    private JPanel contentPane;
    private JPanel albumArt;
    private JLabel imageArt;
    private JTextField titleTrack;
    private JTextField artist;
    private JTextField album;
    private JTextField genre;
    private JTextField trackNumber;
    private JTextField lenght;
    private JTextField year;
    private JTextField comment;
    private JTextField bitRate;

    public static void main(String[] args) {
        new Mp3TaggerFrame().setVisible(true);
    }

    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Mp3 File",
            "mp3");

    public void setAlbumArt(BufferedImage _image) {

    }

    public String getTitleTrack() {
        return titleTrack.getText();
    }

    public void setTitleTrack(String title) {
        this.titleTrack.setText(title);
    }

    public String getArtist() {
        return artist.getText();
    }

    public void setArtist(String artist) {
        this.artist.setText(artist);
    }

    public String getAlbum() {
        return album.getText();
    }

    public void setAlbum(String album) {
        this.album.setText(album);
    }

    public String getGenre() {
        return genre.getText();
    }

    public void setGenre(String genre) {
        this.genre.setText(genre);
    }

    public int getTrackNumber() {
        return Integer.valueOf(trackNumber.getText());
    }

    public void setTrackNumber(String string) {
        this.trackNumber.setText(string);
    }

    public void setLenght(int lenght) {
        int min = 0;
        int sec = lenght;
        while (sec >= 60) {
            sec = sec - 60;
            min++;
        }
        this.lenght.setText(String.valueOf(min) + ":" + String.valueOf(sec));
    }

    public int getYear() {
        return Integer.valueOf(year.getText());
    }

    public void setYear(String year) {
        this.year.setText(year);
    }

    public String getComment() {
        return comment.getText();
    }

    public void setComment(String comment) {
        this.comment.setText(comment);
    }

    public String getBitRate() {
        return bitRate.getText();
    }

    public void setBitRate(String bitRate) {
        this.bitRate.setText(bitRate);
    }

    /**
     * Create the frame.
     */
    public Mp3TaggerFrame() {

        chooser.setFileFilter(filter);

        setFont(new Font("Century Schoolbook", Font.PLAIN, 14));
        setResizable(false);
        setTitle("Mp3Tagger");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 562, 423);

        setIconImage(new ImageIcon("res/Icon.png").getImage());

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu mnFile = new JMenu("File");
        menuBar.add(mnFile);

        JMenuItem openMp3File = new JMenuItem("Open Mp3 File");
        mnFile.add(openMp3File);
        openMp3File.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                openFile();
            }
        });

        JMenuItem saveWithNewTag = new JMenuItem("Save With New Tag");
        mnFile.add(saveWithNewTag);
        saveWithNewTag.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                saveFile();
            }
        });

        JMenuItem mntmExit = new JMenuItem("Exit");
        mnFile.add(mntmExit);

        JMenu mnAbout = new JMenu("About");
        menuBar.add(mnAbout);

        JMenuItem mntmAboutTheProgram = new JMenuItem("About The Program");
        mnAbout.add(mntmAboutTheProgram);

        JMenuItem mntmContacts = new JMenuItem("Contacts");
        mnAbout.add(mntmContacts);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblTitle = new JLabel("Title:");
        lblTitle.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblTitle.setBounds(6, 6, 122, 28);
        contentPane.add(lblTitle);

        titleTrack = new JTextField();
        titleTrack.setToolTipText("Title Of The Track");
        titleTrack.setBounds(6, 32, 122, 28);
        contentPane.add(titleTrack);
        titleTrack.setColumns(10);

        JLabel lblArtist = new JLabel("Artist:");
        lblArtist.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblArtist.setBounds(6, 72, 122, 28);
        contentPane.add(lblArtist);

        artist = new JTextField();
        artist.setToolTipText("Artist");
        artist.setColumns(10);
        artist.setBounds(6, 98, 122, 28);
        contentPane.add(artist);

        JLabel lblAlbum = new JLabel("Album:");
        lblAlbum.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblAlbum.setBounds(6, 138, 122, 28);
        contentPane.add(lblAlbum);

        album = new JTextField();
        album.setToolTipText("Album");
        album.setColumns(10);
        album.setBounds(6, 164, 122, 28);
        contentPane.add(album);

        JLabel lblGenre = new JLabel("Genre:");
        lblGenre.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblGenre.setBounds(6, 270, 122, 28);
        contentPane.add(lblGenre);

        genre = new JTextField();
        genre.setToolTipText("Genre Of The Track");
        genre.setColumns(10);
        genre.setBounds(6, 296, 122, 28);
        contentPane.add(genre);

        JLabel lblTrackNumber = new JLabel("Track Number:");
        lblTrackNumber.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblTrackNumber.setBounds(6, 204, 122, 28);
        contentPane.add(lblTrackNumber);

        trackNumber = new JTextField();
        trackNumber.setToolTipText("Number of the track in the album");
        trackNumber.setColumns(10);
        trackNumber.setBounds(6, 230, 122, 28);
        contentPane.add(trackNumber);

        JLabel lblAlbumArt = new JLabel("Album Art");
        lblAlbumArt.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblAlbumArt.setBounds(375, 6, 73, 28);
        contentPane.add(lblAlbumArt);

        albumArt = new JPanel();
        BufferedImage bi = new BufferedImage(150,150,BufferedImage.TYPE_INT_RGB);
        albumArt.add(new JLabel(new ImageIcon(bi)));
        albumArt.setToolTipText("The Album Cover");
        albumArt.setBounds(375, 42, 150, 150);
        contentPane.add(albumArt);

        JLabel lblLenght = new JLabel("Lenght");
        lblLenght.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblLenght.setBounds(375, 204, 122, 28);
        contentPane.add(lblLenght);

        lenght = new JTextField();
        lenght.setEditable(false);
        lenght.setToolTipText("Lenght of the song");
        lenght.setColumns(10);
        lenght.setBounds(375, 230, 122, 28);
        contentPane.add(lenght);

        JButton editAlbumArt = new JButton("Edit");
        editAlbumArt.setBounds(460, 6, 55, 28);
        contentPane.add(editAlbumArt);

        JLabel lblYear = new JLabel("Year:");
        lblYear.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblYear.setBounds(181, 6, 122, 28);
        contentPane.add(lblYear);

        year = new JTextField();
        year.setToolTipText("Year Of The Track");
        year.setColumns(10);
        year.setBounds(181, 32, 122, 28);
        contentPane.add(year);

        JLabel lblComments = new JLabel("Comments:");
        lblComments.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblComments.setBounds(181, 72, 122, 28);
        contentPane.add(lblComments);

        comment = new JTextField();
        comment.setToolTipText("Comments Of The Track");
        comment.setColumns(10);
        comment.setBounds(181, 98, 122, 160);
        contentPane.add(comment);

        JLabel lblBitrate = new JLabel("Bit Rate");
        lblBitrate.setFont(new Font("DokChampa", Font.PLAIN, 15));
        lblBitrate.setBounds(375, 270, 122, 28);
        contentPane.add(lblBitrate);

        bitRate = new JTextField();
        bitRate.setToolTipText("BitRate of the song");
        bitRate.setEditable(false);
        bitRate.setColumns(10);
        bitRate.setBounds(375, 296, 122, 28);
        contentPane.add(bitRate);
    }

    private void openFile() {
    }

    private void setTextFields() {
    }

    private void saveFile() {
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Note that it is still rubbish code. When something is moved in front of the window then moved away, it leaves 'paint artifacts'. Instead of using `null` layouts, it should be laid out with a layout manager (or several). – Andrew Thompson Nov 21 '14 at 14:51
  • http://imgur.com/SeL8Nsn made it! Thank you for the help! i'll finish this one and the i'll start another to create the GUI with layout manager! – Syrinxos Nov 21 '14 at 16:11
0

Please notice that JPanelArt is a JComponent and not a JFrame. A the definition states "op-level Swing containers -- such as JFrame, JDialog, and JApplet -- are specialized components that provide a place for other Swing components to paint themselves". There are two solutions that you could try:

  1. Make JPanelArt extend JPanel, instead of JComponent.
  2. Include a JPanel in your Mp3TaggerFrame class, include the creation code in the Mp3TaggerFrame constructor and the drawing part into the paint() method of Mp3TaggerFrame (in this case, you wold have to have some getGraphics included in order to obtain the panel graphics).

I hope it helps.

rlinden
  • 2,053
  • 1
  • 12
  • 13
  • did you try the second approach? Also, there is no size definition in the JPanelArt. I do not know the defaults, but could it be that it is not invisible, just too small to be seen? – rlinden Nov 21 '14 at 13:03
  • i setted the size of the panel by the setBounds() – Syrinxos Nov 21 '14 at 14:34