import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.google.gson.Gson;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class Main extends JFrame {
private JPanel contentPane;
private JTableUnit table;
private JPanel panel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1000, 700);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton buttonArray = new JButton("2次元配列");
buttonArray.setBounds(10, 10, 112, 21);
buttonArray.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loadWebJsonArray();
}
});
contentPane.add(buttonArray);
JButton buttonProp = new JButton("JSONプロパティでタイトル");
buttonProp.setBounds(134, 10, 218, 21);
buttonProp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loadWebJson();
}
});
contentPane.add(buttonProp);
JButton buttonFix = new JButton("タイトルを内部で固定");
buttonFix.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loadWebJsonFix();
}
});
buttonFix.setBounds(364, 10, 180, 21);
contentPane.add(buttonFix);
JButton buttonClear = new JButton("行クリア");
buttonClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
table.clear();
}
});
buttonClear.setBounds(574, 10, 150, 21);
contentPane.add(buttonClear);
panel = new JPanel();
panel.setBounds(12, 52, 960, 600);
panel.setLayout(new BorderLayout(0, 0));
contentPane.add(panel);
table = new JTableUnit();
panel.add(table.getScrollPane());
}
// *************************
// 2次元配列
// *************************
private void loadWebJsonArray(){
table.reset();
String url = "https://lightbox.sakura.ne.jp/demo/json/syain_array.php";
String result ="[\"error\"]";
OkHttpClient client = new OkHttpClient();
Request.Builder builder = new Request.Builder();
builder.url(url);
Request request = builder.build();
Response response = null;
try {
response = client.newCall(request).execute();
result = response.body().string();
}
catch (IOException e) {
e.printStackTrace();
}
Gson gson = new Gson();
String[][] data = gson.fromJson(result, String[][].class);
int line_count = -1;
int col_count = 0;
for( String[] row : data ) {
// 初回( タイトル部分 )
if ( line_count == -1 ) {
col_count = 0;
for( String col : row ) {
table.addColumn(String.format("col%d", col_count) );
col_count++;
}
col_count = 0;
for( String col : row ) {
table.setColumnTitle(String.format("col%d", col_count), col );
col_count++;
}
line_count++;
// タイトル部分が一行あるので、次のデータをすぐに読む
continue;
}
// 2回目以降( データ部分 )
// 空行の追加
table.addRow();
col_count = 0;
for( String col : row ) {
// line_count に、
table.setColumn(line_count, col_count, col );
col_count++;
}
line_count++;
}
}
// *************************
// JSONプロパティでタイトル
// *************************
private void loadWebJson(){
table.reset();
String url = "https://lightbox.sakura.ne.jp/demo/json/syain_json.php";
String result ="[\"error\"]";
OkHttpClient client = new OkHttpClient();
Request.Builder builder = new Request.Builder();
builder.url(url);
Request request = builder.build();
Response response = null;
try {
response = client.newCall(request).execute();
result = response.body().string();
}
catch (IOException e) {
e.printStackTrace();
}
Gson gson = new Gson();
// キーの順序を保持する為に、LinkedHashMap を使用
LinkedHashMap<String,String>[] data = gson.fromJson(result, LinkedHashMap[].class);
int line_count = 0;
int col_count = 0;
Set<String> keyset = null;
Collection<String> values = null;
Iterator<String> it;
for( LinkedHashMap<String, String> row_data : data ) {
// 初回( タイトル部分 )
if ( keyset == null ) {
keyset = row_data.keySet();
col_count = 0;
it = keyset.iterator();
while( it.hasNext() ) {
it.next();
table.addColumn(String.format("col%d", col_count) );
col_count++;
}
col_count = 0;
it = keyset.iterator();
while( it.hasNext() ) {
String col_data = it.next();
table.setColumnTitle(String.format("col%d", col_count), col_data );
col_count++;
}
}
// 空行の追加
table.addRow();
// 2回目以降( データ部分 )
col_count = 0;
values = row_data.values();
it = values.iterator();
while( it.hasNext() ) {
String col_data = it.next();
table.setColumn(line_count, col_count, col_data );
col_count++;
}
line_count++;
}
}
// *************************
// タイトルを内部で固定
// *************************
private void loadWebJsonFix(){
table.reset();
for( int i = 0; i < 10; i++ ) {
table.addColumn(String.format("col%d", i) );
}
table.setColumnTitle("col0", "社員コード");
table.setColumnTitle("col1", "氏名");
table.setColumnTitle("col2", "フリガナ");
table.setColumnTitle("col3", "所属");
table.setColumnTitle("col4", "性別");
table.setColumnTitle("col5", "給与");
table.setColumnTitle("col6", "手当");
table.setColumnTitle("col7", "管理者");
table.setColumnTitle("col8", "作成日");
table.setColumnTitle("col9", "更新日");
String url = "https://lightbox.sakura.ne.jp/demo/json/syain_json.php";
String result ="[\"error\"]";
OkHttpClient client = new OkHttpClient();
Request.Builder builder = new Request.Builder();
builder.url(url);
Request request = builder.build();
Response response = null;
try {
response = client.newCall(request).execute();
result = response.body().string();
}
catch (IOException e) {
e.printStackTrace();
}
Gson gson = new Gson();
// データ部分のみ使用
Syain[] data = gson.fromJson(result, Syain[].class);
int line_count = 0;
int col_count = 0;
for( Syain row_data : data ) {
// 空行の追加
table.addRow();
// 行データをセット
table.setColumn(line_count, 0, row_data.社員コード );
table.setColumn(line_count, 1, row_data.氏名 );
table.setColumn(line_count, 2, row_data.フリガナ );
table.setColumn(line_count, 3, row_data.所属 );
table.setColumn(line_count, 4, row_data.性別 );
table.setColumn(line_count, 5, row_data.給与 );
table.setColumn(line_count, 6, row_data.手当 );
table.setColumn(line_count, 7, row_data.管理者 );
table.setColumn(line_count, 8, row_data.作成日 );
table.setColumn(line_count, 9, row_data.更新日 );
line_count++;
}
}
}