Reputation: 19
I have two splitpanes inside the main window wherein i have two tables on either side. I have left and right arrow buttons just next to the divider, again each on either side of it.
I want that, when i click one of the cells on the left table, the entire row should be selected and then on the right arrow button's click, the selected row be added to the table on the right splitpane and be highlighted indicating the addition of a new row to the table.
Also i want viceversa to be possible, like transferring a row from table in right splitpane to table in left splitpane.
I am relatively new to GUI in java.
The only code that i have is this...
JPanel jsp1 = new JPanel();
jsp1.setPreferredSize(new Dimension(460,600));
jsp1.setBackground(Color.WHITE);
JPanel jsp2 = new JPanel();
jsp2.setBackground(Color.WHITE);
jsp1.setLayout(null);
jsp2.setLayout(null);
JLabel j1 = new JLabel("Left Splitpane");
j1.setBounds(150,10,100,20);
JLabel j2 = new JLabel("Right Splitpane");
j2.setBounds(200,10,100,20);
jsp1.add(j1);
jsp2.add(j2);
JButton left = new JButton ();
left.setBorderPainted(false);
left.setBackground(Color.WHITE);
JButton right = new JButton ();
right.setBorderPainted(false);
right.setBackground(Color.WHITE);
left.add(new BasicArrowButton(BasicArrowButton.WEST));
right.add(new BasicArrowButton(BasicArrowButton.EAST));
left.setBounds(397, 220, 80, 45);
right.setBounds(-15, 220, 80, 45);
jsp1.add(left);
jsp2.add(right);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
true, jsp1, jsp2);
splitPane.setOneTouchExpandable(false);
getContentPane().add(splitPane);
Object [] columns = {"Name", "Number", "City"};
Object [][] data = {{"Adam", 1, "London"},{"Charlie", 3, "New York"},{"Phil", 5, "Madrid"},{"Megan", 2, "Amstredam"},
{"Wayne",11, "Paris"},{"Mary", 8, "Venice"},{"Jones", 7, "Sydney"},};
JTable mytable = new JTable(data,columns);
mytable.setRowHeight(60);
JTable mynewtable = new JTable(data,columns);
mynewtable.setRowHeight(60);
mynewtable.setAutoCreateRowSorter(true);
JScrollPane scroll = new JScrollPane(mytable);
scroll.setBounds(30,80,350,455);
JScrollPane scrollone = new JScrollPane(mynewtable);
scrollone.setBounds(80,80,350,455);
jsp1.add(scroll);
jsp2.add(scrollone);
Upvotes: 0
Views: 590
Reputation: 347204
Use:
JTable#setRowSelectionAllowed
to allow the row to be highlighted when selectedActionListener
s to your buttons so you know when they are triggered. See How to write an Action Listener and How to Use Buttons, Check Boxes, and Radio Buttons
JTable
is backed by a DefaultTableModel
. You can use DefaultTableModel#getDataVector
to get the row data buffer and JTable.addRow
to move data between the tables.You'll also want to take a look at Laying out components within a container to fix issues with your layout
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTable left;
private JTable right;
public TestPane() {
Object[] columns = {"Name", "Number", "City"};
Object[][] data = {{"Adam", 1, "London"}, {"Charlie", 3, "New York"}, {"Phil", 5, "Madrid"}, {"Megan", 2, "Amstredam"},
{"Wayne", 11, "Paris"}, {"Mary", 8, "Venice"}, {"Jones", 7, "Sydney"},};
DefaultTableModel leftModel = new DefaultTableModel(data, columns);
DefaultTableModel rightModel = new DefaultTableModel(columns, 0);
left = new JTable(leftModel);
right = new JTable(rightModel);
JButton moveRight = new JButton("-->");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
add(new JScrollPane(left), gbc);
gbc.gridx += 2;
add(new JScrollPane(right), gbc);
gbc.gridx = 1;
gbc.fill = GridBagConstraints.NONE;
add(moveRight, gbc);
moveRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int index = left.getSelectedRow();
if (index != -1) {
index = left.convertRowIndexToModel(index);
DefaultTableModel leftModel = (DefaultTableModel) left.getModel();
DefaultTableModel rightModel = (DefaultTableModel) right.getModel();
Vector rowData = (Vector) leftModel.getDataVector().get(index);
leftModel.removeRow(index);
rightModel.addRow(rowData);
}
}
});
}
}
}
Upvotes: 1