0

To start with -- I'm not sure, that I have properly formulated the question (I'm new in Java and in making programs with GUI).

It is the following thing, I'm trying to do. I have a window with several similar parameters (numbers are just for distinction between lines and it ist just very simplified example, of what should my GUI be): Initial Window

Then, by clicking on the "+"-button I would like to add an new line, like here: Line 35 is added

It should be also possible to delete lines, like here: Line 30 was deleted, by pressing "-"-Button.

As I wrote at the beginning, it is possible, that there was such a question, but I couldn't find anything (probably, because I do not now the keywords or I was looking with a wrong ones).

How such window can be done? The only idea I have is to draw a new window after every +/-.

Addition: Code (not working in the part of changing the number of rows).

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


public class Test extends JFrame {
    public Test() {
        
        setSize(200, 600);
        JButton plusButton[] = new JButton[100];
        JButton minusButton[] = new JButton[100];
        JTextField fields[] = new JTextField[100];
        JPanel panel1 = new JPanel();
                
        for (int i=0; i<plusButton.length; i++) {
            plusButton[i]=new JButton("+");
            minusButton[i]=new JButton("-");
            fields[i] = new JTextField("Text "+ i);
        }
        
        for (int i=1; i<4; i++) {
            panel1.add(plusButton[i*10]);
            plusButton[i*10].setActionCommand("add after " +String.valueOf(i));
            panel1.add(minusButton[i*10]);
            minusButton[i*10].setActionCommand("remove " +String.valueOf(i));
            panel1.add(fields[i*10]);
        }
        

        panel1.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
      
        this.getContentPane().add(panel1);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    
    public void actionPerformed(ActionEvent e) {
        for (int i=0; i<100; i++) {
            String stand1 = "add after "+String.valueOf(i);
            String stand2 = "remove "+String.valueOf(i);
                 if (stand1.equals(e.getActionCommand())) {
                //add "row" of elements
                panel1.add(plusButton[i]);
                plusButton[i+1].setActionCommand("add");
                panel1.add(minusButton[i+1]);
                minusButton[i+1].setActionCommand("remove");
                panel1.add(fields[i+1]);

            } else if (stand2.equals(e.getActionCommand())) {
                 //delete "row" of elements
            }
        }
    } 

    public static void main(String[] args) {
        
       Test a = new Test();
    }
}

The Problem, that is obvious -- when I want to add 2 rows (i think it is proper definition) of buttons after button 20, there will be an doubling of numbers. As a solution I see here a creation of a new panel for each new row. But it is sounds wrong for me.

P.S. Unfortunately I do not have time to end this topic or to post a working example. I actually found some kind of solution, beginning from the Question here, on Stack Overflow: Adding JButton to JTable as cell. So, in case somebody will be looking for such topic, it should sounds like "jButton in jTable".

divega
  • 9
  • 2
  • 2
    This is an “I want a pony” type of question that’s difficult to answer concisely and is off-topic on Stack Overflow. If you can, make an attempt and then show us your code we can understand what you’re trying to do on a technical level. Even a small amount of code, however incomplete or broken, can give us context and illustrate your intentions. Instead of leaving us to speculate on what you might need, give us something to build so we can focus help in those areas where it’s most needed. – tadman May 30 '21 at 15:38
  • 1
    May I suggest that you decide on the gui framework you want to use (I suggest JavaFX, but that is an opinion), then take a tutorial on that framework, doing all the exercises. – NomadMaker May 30 '21 at 16:18
  • I thought about using GUI-Builder, but I want to understand, what actually happens. GUI-Builder Code is to complicated for me to understand (for now), how something works an why. I added my not working code. – divega May 30 '21 at 18:45

1 Answers1

0

There are multiple GUI frameworks for Java. First decide which one you wanna use.

As for your particular query

Add functionality to the + and - such that it will create an instance of a field object (that line with parameters as you call them) or destroy that particular instance of the object.

+ is clicked -> Create new object on consecutive line and increase the pointer-count(?) of the following fields.

- is clicked -> Call destructor for the particular object and decrease the pointer-count of the following fields.

SuperTC
  • 1
  • 1