0

How can I add multiple Strings in JLabel without create a new Object with mouseListener and I want it to display the list of Strinsg in block but it always display inline.

My question is everytime I have an String I need to create a new object of JLabel? BTW I have a database with a lot of names and creating a lot of JLabel will be hard.

HERE is the image Click HERE

HERE is my current code with objects I have a database that gets the String of names and then add to JPanel.

 x = new JLabel("name1");
      x1 = new JLabel("name2");
     x.setFont(new Font("calibri",Font.BOLD,20));
    x.addMouseListener(new MouseListener(){

        @Override
        public void mouseClicked(MouseEvent arg0) {         
        }

        @Override
        public void mouseEntered(MouseEvent arg0) { 
            x.setForeground(Color.blue);
        }

        @Override
        public void mouseExited(MouseEvent arg0) {
            x.setForeground(Color.BLACK);

        }

        @Override
        public void mousePressed(MouseEvent arg0) {
            x.setForeground(Color.RED);             
        }

        @Override
        public void mouseReleased(MouseEvent a) {
            x.setForeground(Color.blue);

        }});
    add(x);
    add(x1);
mKorbel
  • 108,320
  • 17
  • 126
  • 296
Poldz Kerk
  • 59
  • 2
  • 14
  • look at JList, OR set the JLabel to html, and append
    +your new name
    – Randy May 01 '13 at 20:06
  • can you get all names first, and concat them with linebreak, set to one jlabel? – Kent May 01 '13 at 20:06
  • but when I click them with mouselistener I should get a different name. – Poldz Kerk May 01 '13 at 20:11
  • *"but when I click them with mouselistener I should get a different name."* unless you want to spent a a lot of time fumbling around with font metrics and line breaks, I'd recommend using either a JList or JTable instead, the will do everything you want and lot more simply – MadProgrammer May 01 '13 at 20:24
  • I am using JList now HOw can I disable the blue box when I clicked the list of Strings? – Poldz Kerk May 01 '13 at 20:27
  • @Poldz Kerk override key in UIManager, [search here](http://tips4java.wordpress.com/2008/10/09/uimanager-defaults/) – mKorbel May 01 '13 at 21:54

2 Answers2

3

It sounds like you are looking for a better control. What about JList?

http://docs.oracle.com/javase/tutorial/uiswing/components/list.html

cneller
  • 1,422
  • 1
  • 14
  • 22
1

You can use HTML formatting inside Swing buttons, menu buttons, labels, etc. Just use an html tag when you're setting the text.

Nick
  • 11
  • 1
  • See [this example](http://stackoverflow.com/questions/7861724/is-there-some-word-wrap-property-of-jlabel-exist/7861833#7861833) for instance. – Andrew Thompson May 02 '13 at 04:15