Toon posts:

[Java] JList - geeft geen scrollbar weer

Pagina: 1
Acties:

Verwijderd

Topicstarter
Ik heb op het moment de volgende probleem.

In java gebruik makend van swing (duh JList ;) ) krijg ik straks invoer binnen via een array list. Dit kan ik kan lekker makkie weer omtoverne in een JList. Nou ik heb dus wat layout prbleempjes.

Ik maak gebruik van een cardlayout en de verschillende card maken zelf van een XYLayout gebruik. Nou heb ik dus een JList gemaakt met meerdere inputs nu komt het probleem dat hij de list laat zien keurig maar dat hij dit afkapt op de grote waarop ik het heb gezet. Dus er zijn nog meerdere selectie's mogelijk alleen de selectie gaat onder de achtergrond door. Normaliteir zou je denken dat ie er een scrollbar van maakt maar nu niks dus

Hier is de broncode (let op deze word zelf aangeroepen door een andere klasse, dus ik laat alleen deze klasse zien)

Java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
 public CenterMenu(InfoMenu infoMenu) {
        addFocusListener(this);
        this.infoMenu = infoMenu;
        fontButton = new Font("ARIAL", Font.BOLD, 24);
        fontLabel = new Font("ARIAL", Font.PLAIN, 18);
        fontText = new Font("ARIAL", Font.PLAIN, 18);
        fontText = new Font("ARIAL", Font.BOLD, 15);
        this.setLayout(xYLayout1);
        cl = new JPanel();
        cl.setLayout(cardLayout);
        xYLayout1.setHeight(550);
        xYLayout1.setWidth(750);
        try {
            requestFocus();
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    private void jbInit() throws Exception {
        pMenu = new JPanel(xYLayout1);
        pMenu.setBackground(Color.yellow);

        bRoute = new JButton("Route");
        bRoute.setFont(fontButton);
        bRoute.addActionListener(this);
        tRoute = new JTextPane();
        tRoute.setText("Via dit Menu Item Kunt u Rit Informatie invoeren en/of wijzigen.");
        tRoute.setFont(fontText);
        tRoute.setBackground(Color.yellow);
        tRoute.setForeground(Color.blue);

        lRoute = new JLabel("Invoer Route Informatie");
        lRoute.setFont(fontButton);
        pRoute = new JPanel(xYLayout1);
        pRoute.setBackground(Color.yellow);
        lTreinNummer = new JLabel("TreinNummer:");
        lTreinNummer.setFont(fontLabel);
        tTreinNummer = new JTextField();
        tTreinNummer.setFont(fontLabel);
        tTreinNummer.addFocusListener(this);
        ok1 = new JButton("OK");
        ok1.setFont(fontOk);
        ok1.addActionListener(this);
        
        String[] entries = { "Entry 1", "Entry 2", "Entry 3",
                         "Entry 4", "Entry 5","Entry 6","Entry 6","Entry 6","Entry 6","Entry 6","Entry 6"};

        
      cBestemming = new JList(entries);
      cBestemming.addFocusListener(this);
      cBestemming.setFont(fontLabel);
      cBestemming.setVisibleRowCount(0);
      JScrollPane listPane = new JScrollPane(cBestemming);
        
        

        cl.add("kaart0", pMenu);
        cl.add("kaart1", pRoute);

        pMenu.add(bRoute, new XYConstraints(25, 15, 130, 50));
        pMenu.add(tRoute, new XYConstraints(160,15,600,50));

        pRoute.add(lRoute, new XYConstraints(15, 15, 600, 25));
        pRoute.add(lTreinNummer, new XYConstraints(15, 67, 120, 25));
        pRoute.add(tTreinNummer, new XYConstraints(140, 67,80,25));
        pRoute.add(ok1, new XYConstraints(250, 55, 50, 50));
        pRoute.add(cBestemming, new XYConstraints(15, 125, 300 ,200));


        this.add(cl);

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == bRoute) {
            cardLayout.show(cl, "kaart1");
            tTreinNummer.requestFocusInWindow();
        }
        if(e.getSource() == ok1) {
            infoMenu.SetTrein(tTreinNummer.getText());
        }
    }

    public void SetCard(String kaart) {
        cardLayout.show(cl, kaart);
        if(kaart=="kaart1") {
            tTreinNummer.requestFocusInWindow();
        }
    }

    public void SetText(String text) {
        if(focusPart == "JTextField"){
            JTextField textField = (JTextField) focus;
            if (text == "DEL" || text == "<--") {
                Document document = textField.getDocument();
                try {
                    document.remove(document.getLength() - 1, 1);
                } catch (BadLocationException ex) {
                }
            } else if (text == "Clear") {
                textField.setText("");
            } else {
                textField.setText(tTreinNummer.getText() + text);
            }
        }
        if(focusPart =="JComboBox") {
            if (text.length() >= 1) {
                char cText = text.charAt(0);
                JComboBox comboBox = (JComboBox) focus;
                comboBox.selectWithKeyChar(cText);
            }
            else {
                System.out.println("error");
            }                

        }
    }
    

   
    public void focusGained(FocusEvent e) {
        if(e.getSource() instanceof JTextField) {
            focus = e.getSource();
            focusPart = "JTextField";
        }
        if(e.getSource() instanceof JComboBox) {
            focus = e.getSource();
            focusPart = "JComboBox";
        }
        this.focus = focus;
        this.focusPart = focusPart;
    }
    
    public void focusLost(FocusEvent e) {
    }



Alvast bedankt

- Baasie

  • Aloys
  • Registratie: Juni 2005
  • Niet online
tis lang geleden dat ik java heb gebruikt, maar volgens mij moet je de scrollpane adden aan je layout ipv de list zelf, de scrollpane zal de list meepakken

  • sig69
  • Registratie: Mei 2002
  • Laatst online: 14:00
Ooit wel eens in de Api docs gekeken?
JList doesn't support scrolling directly. To create a scrolling list you make the JList the viewport view of a JScrollPane. For example:

JScrollPane scrollPane = new JScrollPane(dataList);
// Or in two steps:
JScrollPane scrollPane = new JScrollPane();
scrollPane.getViewport().setView(dataList);
Edit: ah, je hebt 'm al in een scrollpane. Dan moet je idd de scollpane aan je panel toevoegen en niet je jlist

[ Voor 20% gewijzigd door sig69 op 29-09-2006 15:18 ]

Roomba E5 te koop


Verwijderd

Topicstarter
Haha bedankt voor de info, ik zie me fout ik had de JScrollPane all aangemaakt maar ik had nog steeds de list staan bij de add...... zo dat was mijn probleem iig bedankt :D

Verwijderd

Topicstarter
Okey nu hebk een volgend probleem ik wil dat die scrolllbar meegaat als ik een toets op me keyboard intoetst (ik kon het wel in combobox maar nog niet gevonden in deze... :D)

  • Robtimus
  • Registratie: November 2002
  • Laatst online: 12-02 15:05

Robtimus

me Robtimus no like you

Dan moet je een ListSelectionListener gebruiken om events af te vangen wanneer je selectie veranderd. Vervolgens heeft JList een method genaamd "ensureIndexIsVisible(int index)". Daarmee kun je ervoor zorgen dat element index zichtbaar is.

JTree heeft trouwens iets soortgelijks met "scrollRowToVisible(int row)", voor JTable moet je zelf iets verzinnen.

Edit: je kan trouwens ipv een ListSelectionListener ook "gewoon" een KeyListener gebruiken, dat maakt in dit geval niet zo heel erg veel uit.

[ Voor 16% gewijzigd door Robtimus op 29-09-2006 20:39 ]

More than meets the eye
There is no I in TEAM... but there is ME
system specs

Pagina: 1