Reputation: 880
For some reason the Jlist
will not show up on my applet.
It shows up just right of the slider but only when you click on the individual elements.
I tried this.validate()
and this.repaint()
with no luck. Can anyone help me out?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import java.awt.Graphics;
import javax.swing.JList;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.ListSelectionModel;
public class HeatingHome extends JApplet implements ActionListener
{
// declare variables here
JRadioButton switchIt = new JRadioButton();
JSlider tempControl = new JSlider(JSlider.VERTICAL, 10, 15, 11);
String[] theRooms = {"Porch", "Kitchen", "Living Room", "Hall", "Bedroom 1", "Bathroom", "Bedroom 2"};
JList roomsList = new JList(theRooms);
public void init()
{
setSize(1000,600);
}
public void paint(Graphics g)
{
super.paint(g);
roomsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
roomsList.setBounds(700, 200, 150, 150);
roomsList.setVisible(true);
roomsList.setEnabled(true);
add(roomsList);
//tempControl.addChangeListener(e);
tempControl.setMajorTickSpacing(10);
tempControl.setPaintLabels(true);
tempControl.setMinorTickSpacing(1);
tempControl.setPaintTicks(true);
tempControl.setBounds(600, 200, 100, 200);
tempControl.setEnabled(true);
add(tempControl);
}
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Upvotes: 1
Views: 1261
Reputation: 168825
Whereas the list & slider are added to the applet every time it is painted, the radiobutton is never added.
This might get you started:
// <applet code='HeatingHome' width=400 height=200></applet>
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class HeatingHome extends JApplet
{
// declare variables here
JRadioButton switchIt = new JRadioButton();
JSlider tempControl = new JSlider(JSlider.VERTICAL, 10, 15, 11);
String[] theRooms = {"Porch", "Kitchen", "Living Room", "Hall", "Bedroom 1", "Bathroom", "Bedroom 2"};
JList roomsList = new JList(theRooms);
public void init()
{
// applet size is set by the HTML
//setSize(1000,600);
setLayout(new BorderLayout());
JPanel gui = new JPanel(new BorderLayout(5,5));
gui.setBackground(Color.ORANGE);
gui.setBorder(new EmptyBorder(20,20,20,20));
roomsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Use LAYOUTS!!!
//roomsList.setBounds(700, 200, 150, 150);
// Becomes visible when added to something!
//roomsList.setVisible(true);
// Enabled by default!
//roomsList.setEnabled(true);
gui.add(roomsList, BorderLayout.EAST);
//tempControl.addChangeListener(e);
tempControl.setMajorTickSpacing(10);
tempControl.setPaintLabels(true);
tempControl.setMinorTickSpacing(1);
tempControl.setPaintTicks(true);
//tempControl.setBounds(600, 200, 100, 200);
//tempControl.setEnabled(true);
//add(tempControl);
gui.add(tempControl, BorderLayout.WEST);
gui.add(switchIt, BorderLayout.NORTH);
add(gui);
validate();
}
}
Upvotes: 1
Reputation: 354536
You're adding plenty of controls every time the applet is painted. Which means every new control you add will trigger at least another paint operation.
Move all the code from the paint
method out into a constructor or similar.
paint
is for painting the control's visuals yourself. I.e. you take the supplied Graphics
object and do stuff on it until you're happy. You're usually not modifying anything else as painting is a bit unpredictable in general.
Upvotes: 1