How to transfer data from Jlist of one frame to JTable of another frame using GUI.
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame tableFrame = new JFrame("Table");
JPanel tablePanel = new JPanel();
tablePanel.setLayout(new BorderLayout());
DefaultTableModel defaultTableModel = new DefaultTableModel();
defaultTableModel.addColumn("Integers");
JTable table = new JTable(defaultTableModel);
JScrollPane scrollPane = new JScrollPane(table);
tablePanel.add(scrollPane, BorderLayout.CENTER);
tableFrame.add(tablePanel);
JFrame listFrame = new JFrame("List");
JPanel listPanel = new JPanel();
listPanel.setLayout(new BorderLayout());
JList<String> list = new JList<>(new String[]{"1", "2", "3", "4"});
JButton button = new JButton("To table");
button.addActionListener(event -> {
for (int i = 0; i < list.getModel().getSize(); i++) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(new String[]{list.getModel().getElementAt(i)});
}
});
listPanel.add(new JScrollPane(list), BorderLayout.CENTER);
listPanel.add(button, BorderLayout.SOUTH);
listFrame.add(listPanel);
tableFrame.setSize(200, 250);
tableFrame.setResizable(false);
tableFrame.setVisible(true);
tableFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
listFrame.setSize(200, 250);
listFrame.setResizable(false);
listFrame.setVisible(true);
listFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
Comments
Leave a comment