Results 1 to 6 of 6
  1. #1
    Zeo7 is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default Creating Jradio Buttons from a given array

    hi, i have an array whit an unkown number of elements and i wnat to create a JRadioButton for each element of the array

    what i have this far
    PHP Code:
    public class OpcionMesa extends javax.swing.JFrame {
    
        /** Creates new form OpcionMesa */
        public OpcionMesa() {
            initComponents();
            Bottones();
    
           
    
        }
    
        private void Bottones(){
            int i;
            int f=0;
           for (i= 0; i < Mesas.tables.length; i++)
               Multiboton[i] = new JRadioButton("Mesa "+Mesas.tables[i].getNumero() );
           this.add(Multiboton[f]);
           f++;
    
        }
    
    
        
    
        /** 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() {
    
            Selecione = new javax.swing.JLabel();
            jButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    
            Selecione.setText("Selecione una mesa");
    
            jButton1.setText("OK");
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(74, 74, 74)
                            .addComponent(jButton1))
                        .addComponent(Selecione))
                    .addContainerGap(55, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(Selecione)
                    .addGap(360, 360, 360)
                    .addComponent(jButton1)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
    
                    new OpcionMesa().setVisible(true);
    
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JLabel Selecione;
        private javax.swing.JButton jButton1;
        public JRadioButton[] Multiboton = new JRadioButton[Mesas.tables.length];
        // End of variables declaration


    how ever the JRadio Buttons dont apear in the JFrame what should i do, any help is apreciated
    Last edited by Zeo7; 12-17-2010 at 02:54 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Please edit your post above to change your quote tags to code tags so that your code is readable.

    To solve your problem, put your JRadioButtons in a JPanel that uses a GridLayout, perhaps a GridLayout that has one column and variable number of rows: new GridLayout(0, 1);

    Don't add your JRadioButtons to a JPanel that uses GroupLayout. You will probably want to read up more on the layout managers at the Swing tutorial site to find out more.

  3. #3
    Zeo7 is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    i tired reading the grid layout tutorial but i dont get it

    PHP Code:
    public class GridLayoutMesas {
    
        JFrame frame = new JFrame("Mesas");
        
        GridLayout mesas =  new GridLayout(0,1);
        frame.setLayout(mesas);

    i liked the gridlayout idea but i cant get it to work

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Zeo7 View Post
    i tired reading the grid layout tutorial but i dont get it

    PHP Code:
    public class GridLayoutMesas {
    
        JFrame frame = new JFrame("Mesas");
        
        GridLayout mesas =  new GridLayout(0,1);
        frame.setLayout(mesas);

    i liked the gridlayout idea but i cant get it to work
    "i cant get it to work" doesn't tell us much, and in fact I don't know how to help you based on this information. Please tell us specifically what problems you are having. You may wish to post compilable runnable code. Also, while NetBeans is a wonderful IDE, you should avoid using its drag-and-drop Swing code generation and instead learn to code Swing by hand. It's the best way to understand all of this.

  5. #5
    Zeo7 is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    ok so i got the grid layout but how do i add the listeners??? plx

    PHP Code:
    import java.awt.*;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.BoxLayout;
    import javax.swing.WindowConstants;
    
    /**
     *
     * @author Stephan
     */
    public class GridLayoutMesas {
    
    
        JFrame frame;
        JPanel panelSuperior, panelInferior;
        JButton[] mesas = new JButton[Mesas.tables.length];
        JTextField pantalla;
    
        public GridLayoutMesas(){
     
            construyePanelInferior();
            construyeVentana();}
    
        void construyePanelInferior(){
            panelInferior= new JPanel();
            panelInferior.setLayout(new GridLayout(0,1));
            for (int i = 0; i<mesas.length;i++ ){
              mesas[i]= new JButton("Mesa " + Mesas.tables[i].getNumero() );
              panelInferior.add(mesas[i]);
            }
    
    
            }
    
        void construyeVentana(){
            frame =new JFrame("Calculadora ");
            frame.setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS));
            frame.add(panelInferior);
            frame.pack();
            frame.setVisible(true);
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
    
        public static void main(String [] inforux){
            new GridLayoutMesas();}
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The layout has nothing to do with the listeners. If you need to add listeners to your buttons, be they JButtons, JRadioButtons, or anything that derives from AbstractButton, then you add them by calling addActionListener(ActionListener al) on the button object. Since you're creating them in a for loop, you can add the listener in that same loop. I strongly suggest, no urge, that you read the Swing tutorials to get a better and deeper understanding of this.

Similar Threads

  1. Non functional radio buttons and messed up array
    By flameofSuzaku in forum New To Java
    Replies: 2
    Last Post: 01-10-2011, 05:38 PM
  2. Multidemensional array of Buttons
    By macwadu in forum AWT / Swing
    Replies: 32
    Last Post: 08-29-2010, 12:25 AM
  3. Replies: 2
    Last Post: 04-30-2010, 03:06 AM
  4. Creating Clear and Exit Buttons
    By flambookey in forum New To Java
    Replies: 2
    Last Post: 03-24-2010, 05:08 PM
  5. creating buttons
    By defactor in forum New To Java
    Replies: 5
    Last Post: 01-02-2010, 02:06 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •