• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jlist

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having trouble with a college asignment. I need to fill in the blanks to the code below.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Lab37B {
JFrame frame;
JList size;
JLabel message;
String[] sizes = {"Small", "Medium", "Large"};
public static void main(String[] args) {
Lab37B me = new Lab37B();
me.go();
}
public void go() {
frame = new JFrame("Change text size");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
size = new JList(__________);
size.set_____________Index(0);
size.add_________________Listener(new SizeListener());
frame.getContentPane().add(size, BorderLayout.WEST);
message = new JLabel("Hello World!", JLabel.CENTER);
message.setFont(new Font("Serif", Font.PLAIN, 12));
frame.getContentPane().add(message);
frame.setVisible(true);
}
class SizeListener implements ListSelectionListener {
public void ______________(ListSelectionEvent e) {
int i = size.get_________Index();
int ptSize = 12 + (12 * i);
message.setFont(new Font("Serif", Font.PLAIN, ptSize));
}
}
}
This (code below)is how I have filled them in. The program will compile but will not run and I can't figur it out. I think the problem is in the line "size = new JList();" But have done everything I can think of trying.
Thanks

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Lab37B {
JFrame frame;
JList size;
JLabel message;
String[] sizes = {"Small", "Medium", "Large"};
public static void main(String[] args) {
Lab37B me = new Lab37B();
me.go();
}
public void go() {
frame = new JFrame("Change text size");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
size = new JList();
size.setSelectedIndex(0);
size.addListSelectionListener(new SizeListener());
frame.getContentPane().add(size, BorderLayout.WEST);
message = new JLabel("Hello World!", JLabel.CENTER);
message.setFont(new Font("Serif", Font.PLAIN, 12));
frame.getContentPane().add(message);
frame.setVisible(true);
}
class SizeListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent e) {
int i = size.getSelectedIndex();
int ptSize = 12 + (12 * i);
message.setFont(new Font("Serif", Font.PLAIN, ptSize));
}
}
}
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you've missed completing this line

size = new JList(__________);
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeff,

responding here to your email.

It is much better if I point you to where you can find the solution,
which will help when researching future problems.

here's a link to the api docs for JList, scroll down to the 'Constructor Summary' section
there a 4 constructors for JList, and one of them matches your requirements.

http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JList.html

post back if you have a further problem.

might also be worthwhile downloading the api docs, and have them handy on your PC

http://java.sun.com/j2se/1.5.0/download.jsp
(scroll down a bit to "J2SE 5.0 Documentation"), and click the download link
 
reply
    Bookmark Topic Watch Topic
  • New Topic