I am trying to code in a way that when user clicks a button, a new row of jlabel and jtextfield will be added to the the gridlayout of the jframe. like this:
Let's say the JFrame is 800x600, and the height of each row is 50, after I reach the 13th line, I want to add in a vertical scrolling bar. But now I can't seem to add the components correctly as I wished, I have also tried other layouts and apparently not working out.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
public class Testing extends JFrame implements ActionListener
{
public static int x =0;
JPanel panel;
public Testing()
{
super("Add component on JFrame at runtime");
setLayout(new BorderLayout());
panel=new JPanel();
panel.setPreferredSize(new Dimension(800,600));
panel.setAutoscrolls(true);
panel.setLayout(new GridLayout(0,2,0,20));
add(panel,BorderLayout.CENTER);
JButton button=new JButton("CLICK HERE");
add(button,BorderLayout.SOUTH);
button.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setVisible(true);
pack();
}
public void actionPerformed(ActionEvent evt)
{
JLabel jlbl1 = new JLabel("Row"+x);
JTextField jtf =new JTextField(10);
jtf.setMaximumSize(new Dimension(400,50));
jlbl1.setMaximumSize(new Dimension(400,50));
panel.add(jlbl1);
panel.add(jtf);
panel.revalidate();
x++;
validate();
}
public static void main(String[]args)
{
Testing test=new Testing();
}
}
Edit: Thanks and Props to MadProgrammer for showing guidelines, this is what I wanted, just in case someone else face the same problem as I did:
Thanks to MadProgrammer and Andrew Thompson for showing guidelines. After some tweaks, this is what I wanted.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TableExample {
public static void main(String[] args) {
new TableExample();
}
public TableExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public GridBagConstraints c = new GridBagConstraints();
private JPanel fieldsPanel;
private int row;
public TestPane() {
c.gridx =0;
c.gridy =0;
c.fill = GridBagConstraints.NONE;
setLayout(new BorderLayout());
fieldsPanel = new JPanel(new GridBagLayout());
add(new JScrollPane(fieldsPanel));
JButton btn = new JButton("Add");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fieldsPanel.add(new JLabel("Row " + (++row)),c);
c.gridx++;
fieldsPanel.add(new JTextField(10),c);
fieldsPanel.revalidate();
c.gridy++;
c.gridx--;
}
});
add(btn, BorderLayout.SOUTH);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}