0

i'm trying to add a slider to my GUI, but it won't show up, i'm new to java so if you could help it would be much appreciated! I'm not sure how to add the slider to the main grid, at the moment it dosn't appear.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;



public class Grid extends JFrame
{
 public void Slider()
 {
    setLayout(new FlowLayout()); 
    JSlider slider;
    JLabel label;

    slider = new JSlider(JSlider.VERTICAL, 0, 20, 0);
    slider.setMajorTickSpacing(5);
    slider.setPaintTicks(true);
    add(slider);

    label = new JLabel ("Number of lifeforms: 0");
    add(label);

 }  

public static void main (String args[])
   {
  JFrame Grid = new JFrame();
  Grid.setSize(800,600);
  Grid.setTitle("Artificial life simulator");
   Grid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

   String rows = JOptionPane.showInputDialog("How many rows does the grid have?");
   int row = Integer.parseInt(rows);

   String columns = JOptionPane.showInputDialog("How many columns does the grid have?");
   int col = Integer.parseInt(columns);

   JOptionPane.showConfirmDialog(null, "Are these the correct demensions: "
  +row+" x "+col+ "?", 
  "Yes or No", JOptionPane.YES_NO_OPTION);


  Container pane = Grid.getContentPane();
  pane.setLayout(new GridLayout(row,col));

  Color square;

  for (int x = 1; x <=(row*col); x++)
  {
     int altr = 0;
     altr = (x-1) % col;
     altr += (x-1) / col;

     if (altr % 2 == 0)
     {
        square = Color.white;
     }

     else
     {
        square = Color.black;
     }

     JPanel panel = new JPanel();
     panel.setPreferredSize(new Dimension(800/row, 600/col));
     panel.setBackground(square);
     pane.add(panel);
  }
  Grid.setVisible(true);

} }

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3357649
  • 53
  • 1
  • 11

1 Answers1

2

Note several changes to your example:

  • The default layout of JFrame is BorerLayout; a vertical slider goes well in EAST.

  • The slider() method returns a JSlider; the label can go in one of the three remaining BorerLayout areas.

  • Override getPreferredSize() to define the size of the rendering pane.

  • TODO: See also Initial Threads.

image

As tested:

import java.awt.*;
import javax.swing.*;

public class Grid extends JFrame {

    private static JSlider slider() {
        JSlider slider;
        slider = new JSlider(JSlider.VERTICAL, 0, 20, 0);
        slider.setMajorTickSpacing(5);
        slider.setPaintTicks(true);
        return slider;
    }

    public static void main(String args[]) {
        JFrame grid = new JFrame();
        grid.setSize(800, 600);
        grid.setTitle("Artificial life simulator");
        grid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        int row = 3;
        int col = 3;

        JPanel pane = new JPanel(){

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 240);
            }
        };
        pane.setLayout(new GridLayout(row, col));
        Color square;
        for (int x = 1; x <= (row * col); x++) {
            int altr = 0;
            altr = (x - 1) % col;
            altr += (x - 1) / col;

            if (altr % 2 == 0) {
                square = Color.white;
            } else {
                square = Color.black;
            }
            JPanel panel = new JPanel(new GridLayout());
            panel.setBackground(square);
            pane.add(panel);
        }
        grid.add(pane);
        grid.add(slider(), BorderLayout.EAST);
        grid.pack();
        grid.setVisible(true);
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks for that :) If i wanted to add a Jabel to the slider how would i do it? i tried.. private static JSlider slider() { JSlider slider; JLable label; slider = new JSlider(JSlider.VERTICAL, 0, 20, 0); slider.setMajorTickSpacing(5); slider.setPaintTicks(true); int value = slider.getValue(); label = new JLabel("Current value: 0"); add(label); label.setText("Current value: " +_value); return slider; } – user3357649 Jan 22 '14 at 04:12
  • Add the label and the slider to a panel, and then add that panel to the frame's content pane. Update your question to include the [*Minimal, Complete, Valid Example*](http://stackoverflow.com/help/mcve) that shows your current approach. – trashgod Jan 22 '14 at 12:03