我在主窗口内有两个分割窗格,其中两边各有两张桌子。我在分隔符旁边有左右箭头按钮,每个按钮位于分隔符的两侧。
我想要的是,当我单击左表中的一个单元格时,应选择整行,然后在右箭头按钮的单击上,将所选行添加到右侧分隔页的表中并突出显示表示向表中添加了新行。
此外,我希望反之亦然,例如将右侧分隔页中的行转移到左侧分隔页中的表。
我对java中的GUI相对较新。
我唯一拥有的代码是......
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);
答案 0 :(得分:1)
使用:
JTable#setRowSelectionAllowed
允许在选中时突出显示行extern crate num_cpus;
let num = num_cpus::get();
添加到按钮,以便了解它们何时被触发。请参阅How to write an Action Listener和How to Use Buttons, Check Boxes, and Radio Buttons
ActionListener
由JTable
支持。您可以使用DefaultTableModel#getDataVector
获取行数据缓冲区,并使用JTable.addRow
在表之间移动数据。您还需要查看Laying out components within a container以解决布局问题
DefaultTableModel