0

I'm trying to make a high score system in JPanel. However, I want the data I get to be in a list vertically. This is how it looks like now

public class Highscore extends JPanel {

    Mainpanel mp = new Mainpanel(10);

    HighscoreManager manage = new HighscoreManager();

    public Highscore() {

        setBounds(0, 0, 800, 600);
        setLayout(null);

        JButton btnNewButton = new JButton("New button");
        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                mp = new Mainpanel();

                mp.setVisible(true);

            }
        });

        JLabel lblNewLabel = new JLabel("New label");
        lblNewLabel.setIcon(new ImageIcon("BackgroundHighscore.jpg"));
        lblNewLabel.setBounds(0, 0, 800, 600);
        add(lblNewLabel);

        manage.addScore("Bart",240);
        manage.addScore("Marge",300);
        manage.addScore("Maggie",220);
        manage.addScore("Homer",100);
        manage.addScore("Lisa",270);


        JLabel highscore = new JLabel("HIGHSCORE");
        highscore.setBounds(200,200,400,300);
        highscore.setText(manage.getHighscoreString());
        lblNewLabel.add(highscore);
    }

}

Any help will be appreciated!

asd asd
  • 1
  • 1
  • 1
    You should import it into a JTable not a JLabel – DarkV1 May 01 '16 at 23:27
  • 1
    For [example](http://stackoverflow.com/questions/19148603/resizable-layout-swing-when-disappears-jpanel/19149113?s=1|1.6929#19149113) and [example](http://stackoverflow.com/questions/31738207/trying-to-teach-myself-java-gui-swing-should-be-an-easy-fix/31738292#31738292) – MadProgrammer May 01 '16 at 23:30
  • 1
    Take a look at the [Oracle Documentation - JList](https://docs.oracle.com/javase/tutorial/uiswing/components/list.html). To add elements to the `JList` you might have to create a method in your `HighscoreManager` class that returns a `String` array of the highscores. – MasterBlaster May 01 '16 at 23:37
  • Can't I put the high score sting in a label? – asd asd May 01 '16 at 23:41
  • 1
    If you really want to use a `JLabel` look at the StackOverflow answer to ["Newline in JLabel"](http://stackoverflow.com/a/1090112/4475997). – MasterBlaster May 01 '16 at 23:44
  • If there is an easier to do it, I don't mind :) – asd asd May 01 '16 at 23:45
  • @MasterBlaster: You know, if the list is long, that would be take a long time do make – asd asd May 01 '16 at 23:46
  • 3
    1) `setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. 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). 2) The easiest solution here is to use a `JLabel` as a renderer for the scores in a `JList` (as suggested by @MasterBlaster). – Andrew Thompson May 01 '16 at 23:52
  • You wouldn't have to hardcode it if that's what you're thinking. You could use a for loop with a `String` array to loop through the highscores, add `
    `, then and add them to the `JLabel`.
    – MasterBlaster May 01 '16 at 23:53

0 Answers0