1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Test_DG extends JFrame {
private JPanel contentPane;
static int nbrows;
JPanel columnPanel = new JPanel();
/**
* Launch the application.
*/
public static void main(String[] args) {
nbrows=0;
Test_DG frame = new Test_DG();
frame.setVisible(true);
}
/**
* Create the frame.
*/
public Test_DG() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 532, 362);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 11, 210, 302);
contentPane.add(scrollPane);
scrollPane.setViewportView(columnPanel);
columnPanel.setBackground(Color.ORANGE);
columnPanel.setLayout(new GridLayout(0, 1, 0, 5));
JButton button = new JButton("New button");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
addRow();
}
});
button.setBounds(288, 44, 89, 23);
contentPane.add(button);
}
void addRow() {
JPanel rowPanel = new JPanel();
rowPanel.setLayout(null);
JLabel lbl = new JLabel("");
lbl.setText("N°"+nbrows);
///////////////////
nbrows++;
////////////////////
lbl.setBounds(10, 18, 77, 14);
rowPanel.add(lbl);
JButton btnAdd = new JButton("Add");
btnAdd.setBounds(124, 14, 66, 23);
rowPanel.add(btnAdd);
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
addRow(); //////// ISNT RECURSION HERE ??
}
});
rowPanel.add(btnAdd);
columnPanel.add(rowPanel);
columnPanel.validate();
columnPanel.repaint();
}
} |
Partager