0

Hi this is my concrete problem. I was tried to add one button to one panel with for loop.

This is for loop for creating JButtons.

nizButtona=new JButton[22];
for(int i=0;i<nizButtona.length;i++){

    nizButtona[i] = new JButton();
    if(i==0){
    nizButtona[i].setText("Započni kviz"); //Start quiz
    nizButtona[i].addActionListener(new ActionListener(){
         @Override 
        public void actionPerformed(ActionEvent e){
            cl.next(nizPanela[1]);
        }
    });
    }else if(i==1){
        nizButtona[i].setText("Izlaz"); //Quit
        nizButtona[i].addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                System.exit(0);
            }
        });
        }else if(i<12){
        nizButtona[i].setText("Sledeće pitanje"); //Next question, on next panel
        nizButtona[i].addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e){
                    cl.next(nizPanela[1]);
                }
            });
    }

This is new loop for adding buttons on panels. Here nizButtona[i-1] is i-1 because first button for next question have for 1 argument than JPanel where it's need to be add, and you GridBagLayout for all components so i will put all on the same location for each panel. Without it the problem is the same.

     for(int i=3;i<=11;i++){
        nizPanela[i].add(nizButtona[i-1]);
    }

Here is how i was create array for JPanels.

nizPanela = new JPanel[13];

    for (int i=0;i<nizPanela.length;i++ ){

        nizPanela[i] = new JPanel();

        if(i<=1){
        okvir.getContentPane().add(nizPanela[i]);//Does i real need this getContentPane?
        }else{
            nizPanela[i].setLayout(new GridBagLayout());
            nizPanela[1].add(nizPanela[i], String.valueOf(i));
        }

    }
    cl=new CardLayout();

    nizPanela[1].setLayout(cl); 

    cl.show(nizPanela[1],"2");

This is how program look photo Button Sledeće pitanje visible on this panel but it don't should be. It's only visible if i move mouse pointer through place of this button.

GlacialMan
  • 579
  • 2
  • 5
  • 20
  • Don't just paste your code. What steps have you taken to debug this? Can you identify exactly which part of your code isn't working, and then just post the relevant snippets? – Luke Taylor Mar 27 '16 at 00:36
  • i don't have syntax error so the problem is logical, so i think the better is to post all lines from my code. the problem is: i can't see JButton on Frame. – GlacialMan Mar 27 '16 at 00:53
  • Still, best practice is to only post any code that you think might be relevant. Even if you don't know which code is important, you should break your code into different blocks, and between blocks of code explain what each one is for. – Luke Taylor Mar 27 '16 at 00:56
  • ok but my code isn't too big so anyone who know little about programming and java will know to read my code :) – GlacialMan Mar 27 '16 at 00:58
  • As this is your first post, why don't you look at http://stackoverflow.com/help/how-to-ask – Luke Taylor Mar 27 '16 at 01:01
  • Huh? What do you mean "why you add me" – Luke Taylor Mar 27 '16 at 01:03
  • please if you don't know answer on my question go away. i don't have time to lose – GlacialMan Mar 27 '16 at 01:05
  • I don't know the answer, but this isn't a good question under stackoverflow's guidelines. You probably won't get an answer. – Luke Taylor Mar 27 '16 at 01:06
  • `okvir.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). **Fix that first, and you will likely find this problem disappears.** – Andrew Thompson Mar 27 '16 at 01:14
  • 1
    BTW @LukeTaylor What the OP provided is a [mcve]. At less than 100 lines of code it is (for all intents and purposes) minimal. It is complete (can compile without any changes or extra libraries), is verifiable and an example of the problem (we can run the code to see the problem). – Andrew Thompson Mar 27 '16 at 01:18
  • 1
    @AndrewThompson Thanks. I really just objected to posting the code with no comments about what each section was meant to do. I see your point about how <100 lines isn't really that bad. – Luke Taylor Mar 27 '16 at 01:20

1 Answers1

2

Instead of setLayout(null), learn to use layouts to your advantage. The example below uses a series of nested layouts to add a one grid inside another.

image

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 * @see https://stackoverflow.com/a/36243395/230513
 */
public class Test {

    private static final int ROW = 2;
    private static final int COL = 5;

    private void display() {
        JFrame f = new JFrame("Test");
        f.setLayout(new GridLayout(0, 1));
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel top = new JPanel(new GridBagLayout());
        top.setBackground(Color.darkGray);
        JLabel label = new JLabel("Post no bills.");
        label.setForeground(Color.yellow);
        top.add(label);
        f.add(top);
        f.add(createGridPanel());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private JPanel createGridPanel() {
        JPanel p = new JPanel(new GridLayout(ROW, COL, 5, 5));
        p.setBorder(BorderFactory.createLineBorder(Color.yellow,5));
        p.setBackground(Color.yellow);
        for (int r = 0; r < ROW; r++) {
            for (int c = 0; c < COL; c++) {
                p.add(createSubPanel());
            }
        }
        return p;
    }

    private JPanel createSubPanel() {
        JPanel p = new JPanel(new GridLayout(0, 1));
        JPanel top = new JPanel();
        top.add(new JButton("One"));
        top.add(new JButton("Two"));
        JPanel bot = new JPanel();
        bot.add(new JRadioButton("A"));
        bot.add(new JRadioButton("B"));
        bot.add(new JRadioButton("C"));
        bot.add(new JRadioButton("D"));
        p.add(top);
        p.add(bot);
        return p;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test()::display);
    }
}

Addendum: "I want … two JButtons for next and back."

To permit navigation by button from one panel to another, use CardLayout, shown here and revised below.

image

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 * @see https://stackoverflow.com/a/36243395/230513
 */
public class CardPanel extends JPanel {

    private static final JPanel cards = new JPanel(new CardLayout());
    private final String name;

    public CardPanel(String name) {
        super(new GridLayout(0, 1));
        this.name = name;
        JPanel top = new JPanel(new GridBagLayout());
        top.setBackground(Color.darkGray);
        JLabel label = new JLabel(name);
        label.setForeground(Color.yellow);
        top.add(label);
        JPanel bot = new JPanel();
        bot.setBorder(BorderFactory.createLineBorder(Color.yellow, 5));
        bot.add(new JRadioButton("A"));
        bot.add(new JRadioButton("B"));
        bot.add(new JRadioButton("C"));
        bot.add(new JRadioButton("D"));
        this.add(top);
        this.add(bot);
    }

    @Override
    public String toString() {
        return name;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                create();
            }
        });
    }

    private static void create() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        for (int i = 1; i < 9; i++) {
            CardPanel p = new CardPanel("Panel " + String.valueOf(i));
            cards.add(p, p.toString());
        }
        JPanel control = new JPanel();
        control.add(new JButton(new AbstractAction("\u22b2Prev") {

            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cl = (CardLayout) cards.getLayout();
                cl.previous(cards);
            }
        }));
        control.add(new JButton(new AbstractAction("Next\u22b3") {

            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cl = (CardLayout) cards.getLayout();
                cl.next(cards);
            }
        }));
        f.add(cards, BorderLayout.CENTER);
        f.add(control, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • See also [`GridButtonPanel`](http://stackoverflow.com/a/7706684/230513). – trashgod Mar 27 '16 at 06:13
  • Oh you didn't understand me :) I want to show only one panel and the end (south panel) so exp One label - for question, 4 Radio Button for answers and 2 JButton for next and back. So when someone click on next it will show another panel which have another label for question, another answers and next and back button. I want to have 10panels which will show up one by one when some click on text. I hope that you understand me :) – GlacialMan Mar 27 '16 at 09:14
  • Thanks maybe it will help me :) – GlacialMan Mar 27 '16 at 09:23
  • Like [this](http://stackoverflow.com/a/5655843/230513)? – trashgod Mar 27 '16 at 21:59
  • yeah like that thank you so much :) – GlacialMan Mar 28 '16 at 11:35
  • let's check question again i was updated it. the problem is now with one components which are is on place where is not need to be – GlacialMan Mar 28 '16 at 22:25
  • Sounds like a new question; include a [mcve] and reference this questions appropriate. – trashgod Mar 28 '16 at 22:38
  • yeah it's new question so can you help me now so i don't want to see button - Sledeće pitanje on that JPanel – GlacialMan Mar 28 '16 at 22:46
  • Absent your [mcve], I've shown an approach above. – trashgod Mar 29 '16 at 01:29
  • sorry but i don't know how to fill your request. I was explain what the problem is and i post my code. I know to do that like you do it bud can you tell me where i made mistake why i see this button on that panel ?does i need to set it unvisible and then when i click on button "Započni kviz - Start quiz", by actionPerformed method to set it visible ? – GlacialMan Mar 29 '16 at 07:36
  • Please edit your question to include a [mcve] that shows your current approach. – trashgod Mar 29 '16 at 08:42
  • you want all parts of code? – GlacialMan Mar 29 '16 at 18:51
  • Never mind i was found solution for my problem thank you for all your helps. – GlacialMan Mar 29 '16 at 21:32
  • I accepted your answer. So now i have another question :) How with some of loop to every time when someone click on button next that next of it will set visible true. So exp. we have 5 panels with 4 JButtons which have action to go to next panel and another JButton set visible true like that. first panel: first button is visible all other JButtons are unvisilble, when someone click on that button he will go to next (Second) panel and 2 button on that panel will set visible, but now the button from first panel will set unvisible.... – GlacialMan Mar 30 '16 at 21:34
  • You might look at the *Card Layout Actions* cited [here](http://stackoverflow.com/a/5657106/230513), which _disables_ the inactive button. Also, consider [asking](http://stackoverflow.com/questions/ask) a new question that focuses on your chosen approach. – trashgod Mar 30 '16 at 22:13
  • Here is my new question thank you http://stackoverflow.com/questions/36339852/jbutton-doesnt-recognize-does-jradiobutton-is-select – GlacialMan Mar 31 '16 at 17:27
  • The accepted answer appears correct. – trashgod Mar 31 '16 at 20:30