私は Google 検索を行い、JTable と JScrollPane に関するさまざまなソリューションを実装しようとしました。以下は、私が抱えている問題の自己完結型の例です。これらのテーブルで水平スクロール バーを使用できるようにしたいと考えています。どうぞよろしくお願いいたします。
package org.examples;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
public class TableTest extends JFrame {
private JTable tableTest_1;
private ArrayListStringTableModel tableTestModel_1;
private JTable tableTest_2;
private ArrayListStringTableModel tableTestModel_2;
private JScrollPane tableTestScrollPane_1;
private JScrollPane tableTestScrollPane_2;
private class ArrayListStringTableModel extends AbstractTableModel {
/**
*
*/
private static final long serialVersionUID = 3071398398073956893L;
private List <String> listOfStrings;
private String[] columnNames = {""};
public ArrayListStringTableModel (String[] lColumnNames) {
columnNames = lColumnNames;
listOfStrings = new ArrayList<String>();
}
public void addString (String lString) {
String localString = lString;
listOfStrings.add(localString);
}
public int getColumnCount () {
return columnNames.length;
}
public int getRowCount () {
return listOfStrings.size();
}
public String getColumnName (int col) {
return columnNames[col];
}
public Object getValueAt (int row, int column) {
Object returnObject = null;
String localString = listOfStrings.get(row);
switch (column) {
case 0 :
returnObject = (Object)localString;
break;
}
return returnObject;
}
}
/**
* Initialize the contents of the frame.
*/
public TableTest() {
String[] rows = {"Damage Resistance (only vs. Head Shots) Physical Damage Reduction, Resistant, 75% ; Only Works Against Head Shots Rare attack , Only Amyklai can use Energy Damage Reduction, Resistant, 75% ; Only Works Against Head Shots Rare attack , Only Amyklai can use",
"Mental Defense (10 points total),Only Amyklai can use",
"Sight Group Flash Defense (5 points),Only Amyklai can use",
"Resistant Protection (10 PD/10 ED),STR Min 18 , Costs Half Endurance",
"Banded Mail Resistant Protection (8 PD/8 ED)"
};
String[] columns = {"Damage"};
tableTestModel_1 = new ArrayListStringTableModel(columns);
tableTestModel_2 = new ArrayListStringTableModel(columns);
// Set up the main window frame
setTitle("Table Test");
setSize(435, 375);
getContentPane().setLayout(null);
for (int i=0; i <= 4; i++) {
tableTestModel_1.addString(rows[i]);
tableTestModel_2.addString(rows[i]);
}
tableTest_1 = new JTable(tableTestModel_1);
tableTest_1.setBounds(new Rectangle(10, 10, 395, 250));
tableTest_1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tableTestScrollPane_1 = new JScrollPane(tableTest_1);
tableTestScrollPane_1.setBounds(10, 10, 405, 80);
tableTestScrollPane_1.setColumnHeaderView(tableTest_1);
tableTest_2 = new JTable(tableTestModel_2);
tableTest_2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tableTest_2.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tableTestScrollPane_2 = new JScrollPane(tableTest_2);
tableTestScrollPane_2.setBounds(10, 100, 200, 80);
JViewport viewport = new JViewport();
viewport.setView(tableTest_2);
viewport.setPreferredSize(tableTest_2.getPreferredSize());
tableTestScrollPane_2.setRowHeaderView(viewport);
tableTestScrollPane_2.setCorner(JScrollPane.UPPER_LEFT_CORNER, tableTest_2.getTableHeader());
getContentPane().add(tableTestScrollPane_1);
getContentPane().add(tableTestScrollPane_2);
}
/**
* @param args
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TableTest testtableWindow = new TableTest();
testtableWindow.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}