问题描述
由于我是UI的新手,因此我将尝试尽可能全面地解释我要做的事情.我有一个字符串列表,我们称它为user_decide,其中包含不同数量的元素.例如
I will try to explain as thorough as possible what I am trying to do, since I am very new at UIs.I have a String List, let's call it user_decide, which contains different number of elements.For example
1st Row [192, 495, 393, 399]
2nd Row [384, 493, 945, 559, 485]
这些数字表示某些数组的索引,例如
These number represent the index of some arrays, for example
Name[192] = "John", Name[495] = "Mary"
Surname[192] = "Pap", Surname[495] = "Black"
我正在尝试制作一个具有上一个和下一个按钮以及JTable的UI.
I am trying to make a UI which will have a previous and a next button, as well as a JTable.
第一次显示我的用户界面
The first time, my UI will show
1st Column 2nd Column 3rd Column 4th Column 5th Column
Name John Mary etc..
Surname Pap Black
当我按下一步时,它将显示下一行:
and when I press Next, it will display the next line:
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]
到目前为止,我所做的是一个包含许多JTables的框架.
What I have done so far, is a frame with lots of JTables.
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);
}
,当我尝试使用将所有数据存储在一行中的表进行操作时,我有一个下一个按钮.
and I had this next button when I was doing trying to do it with a table which would store all the data in a row.
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 );
}
} );
我将不胜感激.非常感谢您阅读本文.
I would appreciate any help.Thank you a lot for reading this.
推荐答案
public class SwitchTableModel extends AbstractTableModel {
private final String [] name = new String [] {"Alice", "Bob", "John", "Mary"};
private final String [] surname = new String [] {"Green", "Red", "Brown", "Grey"};
private final int [][] data = new int [][] {
new int [] {1, 3, 2},
new int [] {0, 2, 1, 3},
new int [] {0, 3},
new int [] {0, 1, 2, 3}
};
private int position = 0;
@Override
public int getRowCount() {
return 2;
}
@Override
public int getColumnCount() {
return data [position].length + 1;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch (rowIndex)
{
case 0:
return columnIndex == 0 ? "Name" : name [data [position][columnIndex - 1]];
case 1:
return columnIndex == 0 ? "Surname" : surname [data [position][columnIndex - 1]];
default:
throw new Error ();
}
}
public void previous ()
{
position -= 1;
if (position < 0) position = data.length - 1;
fireTableStructureChanged();
}
public void next ()
{
position += 1;
if (position >= data.length) position = 0;
fireTableStructureChanged();
}
public static void main(String[] args) {
final SwitchTableModel model = new SwitchTableModel();
Box toolbar = Box.createHorizontalBox();
toolbar.add (new JButton (new AbstractAction("Previous") {
@Override
public void actionPerformed(ActionEvent e) {
model.previous ();
}
}));
toolbar.add (Box.createHorizontalGlue());
toolbar.add (new JButton (new AbstractAction("Next") {
@Override
public void actionPerformed(ActionEvent e) {
model.next ();
}
}));
JTable table = new JTable(model);
JFrame frame = new JFrame ();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add (toolbar, BorderLayout.NORTH);
frame.getContentPane().add (
new JScrollPane(
table,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
这篇关于在JTable中显示列表的下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!