3

im trying to make my slider appear on my gui once i run my java project, here is what i >have, any help would be nice :)

so the menu bar appears, the menu shapes appears, and the 3 shapes appear too, i just cant >get the slider to appear on at the bottom of the box


class assignment


package assignment;

public class Assignment {

public static void main(String[] args) {
    
MyFrame pane = new MyFrame();  
   
pane.setVisible(true);
pane.setSize(400,400);

class MyFrame


package assignment;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MyFrame extends javax.swing.JFrame {

/**
 * Creates new form MyFrame
 */
public MyFrame() {
initComponents();
    
// Create the menu
JMenuBar topMenu = new JMenuBar();
this.setJMenuBar(topMenu);
    
//create the menu button "shapes"
JMenu shapes = new JMenu("Shapes");
topMenu.add(shapes);
//Create the 3 shapes for the menu
JMenuItem square = new JMenuItem("Square");
JMenuItem circle = new JMenuItem("Circle");        
JMenuItem triangle = new JMenuItem("Triangle");
    
//add shapes to menu
shapes.add(circle);
shapes.add(triangle);
shapes.add(square);
    
//add the menu
setJMenuBar(topMenu);
    
    
MyControlPanel pane = new MyControlPanel();
this.add(pane);
    
    
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

    try {
        for (javax.swing.UIManager.LookAndFeelInfo info :          javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
        new MyFrame().setVisible(true);
        }
    });
    
    
}
// Variables declaration - do not modify
// End of variables declaration

}


class MyControlPanel


package assignment;

import javax.swing.JSlider;
import javax.swing.JTextField;


public class MyControlPanel extends javax.swing.JPanel {

/**
 * Creates new form MyControlPanel
 */
public MyControlPanel() {
initComponents();

JSlider slider = new JSlider();
slider.setMajorTickSpacing(10); 
slider.setPaintLabels(true); 
slider.setPaintTicks(true); 
 
JTextField boundary_length = new JTextField("Boundary Length");
JTextField area = new JTextField("Area");
    
 
this.add(slider);
this.add(area);
this.add(boundary_length);
    
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration

}

Community
  • 1
  • 1
john
  • 65
  • 1
  • 9
  • 2
    1. Avoid form designers until you understand what the various layout managers are doing. 2. Use a different layout manager, try `FormLayout` as a test. Have a look at [A Visual Guide to Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) and [Using Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html) for more information – MadProgrammer Oct 20 '12 at 01:10
  • i understand that but i'm sure i'm missing some code some where? – john Oct 20 '12 at 01:24
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Oct 20 '12 at 01:24
  • @Mad Why `FormLayout` as opposed to (e.g.) `FlowLayout`? Not that I have anything against `FormLayout`, it is very useful, just not a J2SE inbuilt layout. – Andrew Thompson Oct 20 '12 at 01:27
  • @AndrewThompson *doh*, yes, that should be `FlowLayout`, it's being a fun 3 days of sick wife :P – MadProgrammer Oct 20 '12 at 01:28
  • @MadProgrammer I could still use a bit of help, i Don't understnad why my slider isnt appearing – john Oct 20 '12 at 01:29
  • 1
    @StevenMcilhone Have you tried using `FlowLayout` to confirm the component appears? – Andrew Thompson Oct 20 '12 at 01:31
  • @AndrewThompson im still new to java, im unsure what i have to change for that :( – john Oct 20 '12 at 01:33
  • 1
    @Steven: you're just adding the JSlider to the GUI without respecting the layout managers at all. I second what everyone else is saying. Go to the [layout manager tutorial](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) and read through it, and get rid of your generated code. You need to understand what layouts do and how to use them before anyone can help you. – Hovercraft Full Of Eels Oct 20 '12 at 01:33
  • Remove the entire content of `initCoponent()` and replace it with `this.setLayout(new FlowLayout());` or `this.setLayout(new GridLayout(1,0));`. In fact, try both. – Andrew Thompson Oct 20 '12 at 01:35
  • I have removed the generated code, and it works, thanks for that help, i'l look into that tut. thanks everyone – john Oct 20 '12 at 01:36

1 Answers1

2

What you're missing is an understanding of the GroupLayout

If I do this

public MyControlPanel() {
    initComponents();

    JSlider slider = new JSlider();
    slider.setMajorTickSpacing(10);
    slider.setPaintLabels(true);
    slider.setPaintTicks(true);

    JTextField boundary_length = new JTextField("Boundary Length");
    JTextField area = new JTextField("Area");

    setLayout(new FlowLayout());

    this.add(slider);
    this.add(area);
    this.add(boundary_length);

}

I get this...

enter image description here

All I did was change the layout manager to FlowLayout. No offense, but GroupLayout isn't suppose to be created by hand as is really suppose to be used with form editors.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366