Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JList display problem

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default JList display problem

    Hello.

    I am having trouble with my JList.

    I cant get it to display a readable text. It is just displaying a object of a table. Anyone know how I can fix this?

    I have googled ListCellRenderer, and I have read something about a method called getDisplayName(), but i dont know how to use it.

    Here is a picture of my problem. It is unreadable text:/

    Java.jpg


    import javax.swing.JList;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTable;
    import javax.swing.DefaultListModel;
    import javax.swing.ListSelectionModel;
    import javax.swing.DefaultListCellRenderer;
    import javax.swing.ListCellRenderer;
    import static javax.swing.JOptionPane.*;
    import javax.swing.event.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.ArrayList;
     
     
     
    class Oving11 extends JFrame{
     
    	private DefaultListModel FraListe = new DefaultListModel();
    	private DefaultListModel TilListe = new DefaultListModel();
     
    	private JList FraValuta = new JList(FraListe);
    	private JList TilValuta = new JList(FraListe);
     
    	private JLabel ledetekst;
     
     
    	public Oving11(String tittel){
     
    		setTitle(tittel);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
     
    		ledetekst = new JLabel("Velg en valuta");
    		add(ledetekst, BorderLayout.NORTH);
     
    		add(new ListePanel(FraValuta, FraListe), BorderLayout.WEST);
    		add(new ListePanel(TilValuta, TilListe), BorderLayout.EAST);
     
    		LocaleRenderer renderer = new LocaleRenderer();
    		FraListe.setCellRenderer(renderer);
     
    		pack();
    	}
     
     
    	private class Valuta{
     
    		private int enhet;
    		private double valutaKurs; // flere slike for hver valuta.
    		private String valutanavn;
     
    		public Valuta(String valutanavn, double valutaKurs, int enhet){
    			this.valutanavn = valutanavn;
    			this.valutaKurs = valutaKurs;
    			this.enhet = enhet;
    		}
     
    		public int getEnhet(){
    			return enhet;
    		}
     
    		public double getKurs(){
    			return valutaKurs;
    		}
     
    		public String getValuta(){
    			return valutanavn;
    		}
     
    	}
     
    	private class ListePanel extends JPanel{ //implements ListCellRenderer
     
    		public Valuta[] valutaliste = {
    		    new Valuta("Euro", 8.10, 1), new Valuta("US Dollar", 6.23, 1),
    		    new Valuta("Britiske pund", 12.27, 1), new Valuta("Svenske kroner", 88.96, 100),
    		    new Valuta("Danske kroner", 108.75, 100), new Valuta("Yen", 5.14, 100),
    		    new Valuta("Islandske kroner", 9.16, 100), new Valuta("Norske kroner", 100, 100)
    		};
     
    		LocaleRenderer render = new LocaleRenderer();
     
    		public ListePanel(JList listen , DefaultListModel DLM){
     
    			DLM.addElement("Ny Valuta");
     
    			for(int i=0; i<valutaliste.length; i++){
    				DLM.addElement(valutaliste[i]);
    				System.out.println(FraListe.getElementAt(i));
    				System.out.println(FraListe.getElementAt(i));
    			}
     
    			listen.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    			JScrollPane rulleListe = new JScrollPane(listen);
    			add(rulleListe);
    			listen.addListSelectionListener(new ListeLytter());
     
     
    		}
    	}
     
    	private class ListeLytter implements ListSelectionListener{
     
    		protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();
    		public void valueChanged(ListSelectionEvent hendelse){
     
    			int valgFra = FraValuta.getSelectedIndex();
    			int valgTil = TilValuta.getSelectedIndex();
     
     
    			ArrayList<Valuta> valutaTab = new ArrayList<Valuta>();
     
     
    			if(valgFra == 0 || valgTil == 0){
    				String valuta = showInputDialog("Navn på valuta du ønsker å registrere.");
    				String kursen = showInputDialog("Kurs på valuta");
    				String enheten = showInputDialog("skriv enhet");
     
    				double kurs = Double.parseDouble(kursen);
    				int enhet = Integer.parseInt(enheten);
     
    				Valuta nyValuta = new Valuta (valuta,kurs,enhet);
    				FraListe.addElement(nyValuta); //getvaluta();
    				TilListe.addElement(nyValuta);
     
     
    				FraValuta.clearSelection();
    				TilValuta.clearSelection();
     
    			}else if(valgFra > 0  && valgTil > 0){
     
     
    				Valuta valgEn = (Valuta)FraListe.get(valgFra);
    				Valuta valgTo = (Valuta)TilListe.get(valgTil);
     
    				//String valgEn = valutaTab.get(valgFra).getValuta();
    				//String valgTo = valutaTab.get(valgTil).getValuta();
     
    				String belopLest = showInputDialog("Fra" + valgEn.getValuta() + "Til" + valgTo.getValuta() + "Oppgi beløpet: ");
    				int belop = Integer.parseInt(belopLest);
     
    				//double enhetskurs1 = valgEn.getKurs() / valgEn.getEnhet();
    				//double enhetskurs2 = valgTo.getKurs() / valgTo.getEnhet();
    				//double sum = ((double)belop / enhetskurs2) * enhetskurs1;
     
    				double sum = (belop * valgTo.getKurs()) / valgEn.getKurs();
     
    				ledetekst.setText(belop + " " + valgEn.getValuta() + " tilsvarer " + sum + " " + valgTo.getValuta());
     
    				FraValuta.clearSelection();
    				TilValuta.clearSelection();
    			}
    		}
    	}
     
    	public static void main(String[]args){
    		Oving11 oving = new Oving11("Valutakalkulator");
    		oving.setVisible(true);
    	}
     
     
    	// I am trying to get the displayname with ListCellRenderer.. but I dont know how it works.
     
    	private class LocaleRenderer extends DefaultListCellRenderer {
     
      		public LocaleRenderer() {
     		}
     
     		public Component getListCellRendererComponent(JList list,Object value,int index, boolean isSelected,boolean cellHasFocus) {
     
        		super.getListCellRendererComponent(list,value,index,isSelected,cellHasFocus);
        		Valuta en = (Valuta)value;
        		setText(en.getValuta()); // getDisplayName();
        		return this;
      		}
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: JList display problem

    Your image shows what is returned by the Object class's toString method for a class: the classname@objectslocation.
    If you want to change what is returned you need to override the toString method in the class that you are printing and return a String that you would like to see instead of the default String from the Object class's method.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Poseidon (February 19th, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JList display problem

    thx man!

    Now it works

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,602
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: JList display problem

    That was an easy one.

Similar Threads

  1. [SOLVED] JList display Hebrew right-to-left
    By ilan in forum AWT / Java Swing
    Replies: 3
    Last Post: October 5th, 2011, 06:43 AM
  2. GridBagLayout display problem
    By mjpam in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 5th, 2011, 04:58 PM
  3. Problem with jList
    By nextdigit in forum AWT / Java Swing
    Replies: 1
    Last Post: April 16th, 2010, 06:19 PM
  4. problem in getting scrollbar in Jlist
    By sandeepben in forum AWT / Java Swing
    Replies: 1
    Last Post: March 27th, 2010, 03:51 AM
  5. [SOLVED] JAVA JList Problem in visual images
    By antitru5t in forum AWT / Java Swing
    Replies: 4
    Last Post: April 15th, 2009, 03:09 PM