Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > What's wrong with JList in JScrollPanel

Reply
Thread Tools

What's wrong with JList in JScrollPanel

 
 
fancyerii
Guest
Posts: n/a
 
      09-21-2008
I want to design a Frame which cotains 2 buttons. When I click
button1, the frame display a JPanel(Panel1) and when I click

button2, it displays another JPanel(Panel2).

codes in Frame1:
jButton1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (selected != null)
Frame1.this.remove(selected);
selected = new Panel1();

selected.setBounds(0, 0, 400, 300);
Frame1.this.add(selected);
Frame1.this.repaint();

}
});


There is a JLabel and a JScorllPane which contains a JList in Panel1
There is a JList and a JScorllPane which contains a JList in Panel1
In Panel1, JLabel can display correctly. But JScrollPanel display
nothing.
In Panel2, JList can display correctly. But JScrollPanel display
nothing.

What's wrong with JList in JScrollPane?


Panel1 and Panel2:

public class Panel1 extends JPanel {
public Panel1() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
this.setLayout(null);
DefaultListModel model=new DefaultListModel();
model.addElement("1");
model.addElement("2");
jList1.setModel(model);
jScrollPane1.setBounds(new Rectangle(59, 62, 285, 131));
jLabel1.setText("jLabel1");
jLabel1.setBounds(new Rectangle(62, 25, 151, 23));
this.add(jScrollPane1);
this.add(jLabel1);
jScrollPane1.getViewport().add(jList1);
}

JScrollPane jScrollPane1 = new JScrollPane();
JList jList1 = new JList();
JLabel jLabel1 = new JLabel();
}

public class Panel2 extends JPanel {
public Panel2() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
this.setLayout(null);
DefaultListModel model=new DefaultListModel();
model.addElement("1");
model.addElement("2");
jList1.setModel(model);
jList2.setModel(model);
jScrollPane1.setBounds(new Rectangle(86, 61, 236, 161));
jList2.setBounds(new Rectangle(73, 11, 65, 29));
this.add(jScrollPane1);
this.add(jList2);
jScrollPane1.getViewport().add(jList1);
}

JScrollPane jScrollPane1 = new JScrollPane();
JList jList1 = new JList();
JList jList2 = new JList();
}
 
Reply With Quote
 
 
 
 
softwarepearls_com
Guest
Posts: n/a
 
      09-21-2008
My suggestion is to have a look at CardLayout.. personally, I wouldn't
use the remove-old-and-replace-by-other approach you're using.
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      09-22-2008
fancyerii wrote:
> I want to design a Frame which cotains 2 buttons. When I click
> button1, the frame display a JPanel(Panel1) and when I click
>
> button2, it displays another JPanel(Panel2).
>
> codes in Frame1:
> jButton1.addActionListener(new ActionListener(){
> public void actionPerformed(ActionEvent e) {
> if (selected != null)
> Frame1.this.remove(selected);
> selected = new Panel1();
>
> selected.setBounds(0, 0, 400, 300);
> Frame1.this.add(selected);
> Frame1.this.repaint();
>
> }
> });
>
>
> There is a JLabel and a JScorllPane which contains a JList in Panel1
> There is a JList and a JScorllPane which contains a JList in Panel1
> In Panel1, JLabel can display correctly. But JScrollPanel display
> nothing.
> In Panel2, JList can display correctly. But JScrollPanel display
> nothing.
>
> What's wrong with JList in JScrollPane?
>
>
> Panel1 and Panel2:
>
> public class Panel1 extends JPanel {
> public Panel1() {
> try {
> jbInit();
> } catch (Exception exception) {
> exception.printStackTrace();
> }
> }
>
> private void jbInit() throws Exception {
> this.setLayout(null);
> DefaultListModel model=new DefaultListModel();
> model.addElement("1");
> model.addElement("2");
> jList1.setModel(model);
> jScrollPane1.setBounds(new Rectangle(59, 62, 285, 131));
> jLabel1.setText("jLabel1");
> jLabel1.setBounds(new Rectangle(62, 25, 151, 23));
> this.add(jScrollPane1);
> this.add(jLabel1);
> jScrollPane1.getViewport().add(jList1);
> }
>
> JScrollPane jScrollPane1 = new JScrollPane();
> JList jList1 = new JList();
> JLabel jLabel1 = new JLabel();
> }
>
> public class Panel2 extends JPanel {
> public Panel2() {
> try {
> jbInit();
> } catch (Exception exception) {
> exception.printStackTrace();
> }
> }
>
> private void jbInit() throws Exception {
> this.setLayout(null);
> DefaultListModel model=new DefaultListModel();
> model.addElement("1");
> model.addElement("2");
> jList1.setModel(model);
> jList2.setModel(model);
> jScrollPane1.setBounds(new Rectangle(86, 61, 236, 161));
> jList2.setBounds(new Rectangle(73, 11, 65, 29));
> this.add(jScrollPane1);
> this.add(jList2);
> jScrollPane1.getViewport().add(jList1);
> }
>
> JScrollPane jScrollPane1 = new JScrollPane();
> JList jList1 = new JList();
> JList jList2 = new JList();
> }


Your code doesn't make any sense. I can only suggest that you use a
layout manager and forget the setBounds() method. Every Java
installation uses different fonts and different screen resolutions. A
layout manager will allow your code to work not only on your machine but
on everybody else's too.

Please give us a little better description and we could probably give
you some simple code to layout your design without much difficulty.

--

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting the previously selected index for JList Jared Java 0 07-31-2003 07:39 AM
JList Problems Tom Java 1 07-30-2003 03:15 PM
JList problem. Tom Java 1 07-30-2003 12:53 PM
JList question - how to fire a change event when nothing has changed? Sam Java 0 07-17-2003 10:40 PM
JList question Passero Java 1 06-28-2003 12:17 PM



Advertisments
 



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