2021년 11월 21일 일요일

[Java] Java Swing Application Example

 1. Java Swing Application Example



package com.home.app;

import com.formdev.flatlaf.FlatLightLaf;
import com.home.swing.JS;
import com.home.swing.JSTable;
import com.home.swing.JSText;
import com.home.swing.JSTree;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;

public class SwingListViewer {

    JFrame frame;
    JSplitPane mainSp;
    JSplitPane leftSp;
    JTabbedPane leftBottom;
    JSTable list;
    JSTree detail;
    JSText hexdump;
    JLabel status;

    String[] tableCols = {"index", "time", "info"};
    int[] tableWidth = { 50, 200, 480 };
    int[] tableAlign = { 1, 0, -1 };

    public static void main( String[] args) throws IOException {
        FlatLightLaf.install(); //Must be called first of all Swing code.
        new SwingListViewer().start();
    }

    private void setTestData() {
        list.addRow(new String[] {"1", "2020.01.01 12:00:00", "Information 1" } );
        list.addRow(new String[] {"2", "2020.01.01 12:01:00", "Information 2" } );
        list.addRow(new String[] {"3", "2020.01.01 12:02:00", "Information 3" } );
        list.setRowColor(0, Color.BLUE, Color.LIGHT_GRAY);
        list.setRowColor(1, Color.WHITE, Color.BLACK);

        detail.addTreeToRoot("Tree1");
        detail.addTreeToRoot("Tree2");
    }
    private void start() throws IOException {

        frame = new JFrame("Swing Application Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JS.setWindowIconRes(frame, "res/appicon.ico");
        list = new JSTable(tableCols, tableWidth, tableAlign, this::listHandler );
        detail = new JSTree("Packet Details", this::treeHandler );
        hexdump = new JSText();
        leftBottom = JS.getTabbedPane();
        JS.addTab( leftBottom, "Hexdump", hexdump.ctrl);
        leftSp = JS.getVSplitPane(new JScrollPane(list.ctrl), leftBottom);
        mainSp = JS.getHSplitPane(leftSp, new JScrollPane(detail.ctrl));
        status = new JLabel("Ready");

        frame.getContentPane().add(JS.getMenuBar(), BorderLayout.NORTH);
        frame.getContentPane().add(mainSp, BorderLayout.CENTER);
        frame.getContentPane().add(status, BorderLayout.SOUTH);
        frame.pack();
        frame.setSize( new Dimension(600, 600));
        frame.setLocationRelativeTo(null);

        setTestData();
        frame.setVisible(true);
    }

    public void listHandler() {
        String[] paths = list.getSelectedRow();
        if( paths != null && paths.length > 0 ) {
            for( String path : paths )
                System.out.println( path + " -> " );
        }
    }
    public void treeHandler() {
        String[] paths = detail.getSelectedTreePath();
        if( paths != null && paths.length > 0 ) {
            for( String path : paths )
                System.out.println( path + " -> " );
        }
    }
}

댓글 없음:

댓글 쓰기