ADDING AN OBJECT INTO A JPANEL!

i want to add a Jslider into a panel(year_panel) and then add that year_panel into the GridWindow.

I have tried various ways but all in vain.Could somebody help me to put the Slider in the panel and the panel into the main Grid!

thanks a lot.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

import java.text.SimpleDateFormat;

public class Grid extends JFrame /*implements ActionListener*/

{

static final int FPS_INIT = 15;//initial frames per second

int frameNumber = 0;

int delay;

//Timer timer;

boolean frozen = false;

private JPanel date_panel,year_panel, month_panel,day_panel;

private String time=new String("");

private Date date=new Date();

//private Jslider framesPerSecond ;

public Grid() {

time=time + new Date().toString();

SimpleDateFormat formatter = new SimpleDateFormat("E, MMMM d, yyyy") ;

time = formatter.format(date) ;

date_panel=new JPanel();

year_panel=new JPanel();

month_panel=new JPanel();

day_panel=new JPanel();

date_panel.setLayout(new FlowLayout());

date_panel.add(new JLabel(time));

year_panel.setLayout(new FlowLayout());

/*these two lines*/

year_panel.add(new JLabel(framesPerSecond));

//year_panel.add(framesPerSecond);

month_panel.setLayout(new FlowLayout());

day_panel.setLayout(new FlowLayout());

JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL,

0, 31, FPS_INIT);

//framesPerSecond.addChangeListener(new SliderListener());

//Turn on labels at major tick marks.

framesPerSecond.setMajorTickSpacing(10);

framesPerSecond.setMinorTickSpacing(1);

framesPerSecond.setPaintTicks(true);

framesPerSecond.setPaintLabels(true);

framesPerSecond.setBorder(

BorderFactory.createEmptyBorder(0,0,10,0));

//Create the label for the animation.

//picture = new JLabel(new ImageIcon("images/doggy/T"

//+ frameNumber

//+ ".gif"),

// JLabel.CENTER);

Container contentPane = getContentPane();

contentPane.setLayout(new GridLayout(1,4));

contentPane.add(date_panel);

contentPane.add(year_panel);

//contentPane.add(framesPerSecond);

//contentPane.add(year_panel);

//contentPane.add(month_panel);

//contentPane.add(day_panel);

}

public static void main(String args[]) {

Grid gw = new Grid();

gw.setTitle("Date Slider");

gw.setSize(400,200);

gw.setVisible(true);

}

}

[2690 byte] By [shahzad5] at [2007-9-26 1:18:56]
# 1

Here you go...import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

import java.text.SimpleDateFormat;

public class Grid extends JFrame /*implements ActionListener*/

{

static final int FPS_INIT = 15; //initial frames per second

//SHOULD BE INT:

int intframeNumber = 0;

int delay;

// Timer timer;

boolean frozen = false;

private JPanel date_panel,year_panel, month_panel,day_panel;

private String time=new String("");

private Date date=new Date();

//private Jslider framesPerSecond ;

public Grid() {

time=time + new Date().toString();

SimpleDateFormat formatter = new SimpleDateFormat("E, MMMM d, yyyy") ;

time = formatter.format(date) ;

date_panel=new JPanel();

year_panel=new JPanel();

month_panel=new JPanel();

day_panel=new JPanel();

//MOVE THIS TO HERE:

JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL,

0, 31, FPS_INIT);

date_panel.setLayout(new FlowLayout());

date_panel.add(new JLabel(time));

year_panel.setLayout(new FlowLayout());

/*these two lines*/

//year_panel.add(new JLabel(framesPerSecond));

//DON'T ADD A JLABEL^^, DO THIS INSTEAD:

year_panel.add(framesPerSecond);

//year_panel.add(framesPerSecond);

month_panel.setLayout(new FlowLayout());

day_panel.setLayout(new FlowLayout());

// framesPerSecond.addChangeListener(new SliderListener());

//Turn on labels at major tick marks.

framesPerSecond.setMajorTickSpacing(10);

framesPerSecond.setMinorTickSpacing(1);

framesPerSecond.setPaintTicks(true);

framesPerSecond.setPaintLabels(true);

framesPerSecond.setBorder(

BorderFactory.createEmptyBorder(0,0,10,0));

//Create the label for the animation.

// picture = new JLabel(new ImageIcon("images/doggy/T"

// + frameNumber

// + ".gif"),

// JLabel.CENTER);

Container contentPane = getContentPane();

contentPane.setLayout(new GridLayout(1,4));

contentPane.add(date_panel);

contentPane.add(year_panel);

//contentPane.add(framesPerSecond);

// contentPane.add(year_panel);

//contentPane.add(month_panel);

//contentPane.add(day_panel);

}

public static void main(String args[]) {

Grid gw = new Grid();

gw.setTitle("Date Slider");

gw.setSize(400,200);

gw.setVisible(true);

}

}

artntek at 2007-6-29 0:50:41 > top of Java-index,Archived Forums,Java Programming...