我将尝试尽可能彻底地解释我正在尝试做的事情,因为我是 UI 的新手。我有一个字符串列表,我们称之为 user_decide,它包含不同数量的元素。例如
1st Row [192, 495, 393, 399]
2nd Row [384, 493, 945, 559, 485]
这些数字代表一些数组的索引,例如
Name[192] = "John", Name[495] = "Mary"
Surname[192] = "Pap", Surname[495] = "Black"
我正在尝试制作一个具有上一个和下一个按钮以及一个 JTable 的 UI。
第一次,我的 UI 会显示
1st Column 2nd Column 3rd Column 4th Column 5th Column
Name John Mary etc..
Surname Pap Black
当我按下 Next 时,它将显示下一行:
1st Column 2nd Column 3rd Column 4th Column 5th Column 6th Column
Name Name[384] Name[493] Name[945] etc...
Surname Surname[384] Surname[493] Surname[945]
到目前为止,我所做的是一个包含许多 JTable 的框架。
JPanel panel = new JPanel();
int cols = 0;
Iterator<String> iterator = user_decide.iterator();
while (iterator.hasNext())
{
String con = iterator.next();
con = con.replaceAll("\\s", "").replaceAll("\\[", "").replaceAll("\\]", "");
String conv [] = con.split("\\,");
// It can be used for the "for-statement"
cols = conv.length;
List<String> column = new ArrayList<String>();
// Giving the name of the columns
for(int i = 0; i < cols + 1; i ++)
{
if (i == 0) column.add("");
else column.add("Column #" + i);
}
// Setting the jtable with a specific number of columns
String[] column_names = column.toArray(new String[column.size()]);
tableModel = new DefaultTableModel( 17, cols);
tableModel.setColumnIdentifiers(column_names);
JTable table = new JTable(tableModel);
// HERE I AM GOING TO PLACE MY DATA AFTERWARDS
SetData(table, "Name",0,0);
SetData(table, "Surname",1,0);
panel.add(new JScrollPane(table,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS));
}
JFrame frame = new JFrame("Results");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(200, 200, 600, 400);
frame.add(new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS));
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
}
public void SetData(JTable table, Object obj, int row_index, int col_index){
table.getModel().setValueAt(obj,row_index,col_index);
}
当我尝试使用将所有数据存储在一行中的表时,我有这个下一个按钮。
JPanel navigation = new JPanel(
new FlowLayout(FlowLayout.CENTER));
JButton next = new JButton("NEXT");
next.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae) {
int height = table.getRowHeight()*(rows-1);
JScrollBar bar = scrollPane.getVerticalScrollBar();
bar.setValue( bar.getValue()+height );
}
} );
我将不胜感激任何帮助。非常感谢您阅读本文。