Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package clientapp;
- import java.awt.BorderLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTable;
- public class Okno extends JFrame implements ActionListener {
- // переменная
- JTable table;
- //
- public Okno() {
- setTitle("Поступление товаров");
- setBounds(10, 10, 800, 600);
- // добавляем созданную панель в главное окно!
- getContentPane().add(createPanel());
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- }
- //
- public JPanel createPanel() {
- JPanel p = new JPanel();
- // задаем компоновку для панели
- p.setLayout(new BorderLayout());
- // создаем таблицу
- // колонки нашей таблицы
- String[] columns = {"№", "Наименование товара",
- "Остаток на складе", "Количество поступления"};
- // данные для таблицы
- String[][] data = {
- {"1", "Мед", "", ""},
- {"2", "Прополис", "", ""},
- {"3", "Воск пчелиный", "", ""},
- {"4", "Цветочная пыльца", "", ""},
- {"5", "Перга", "", ""},
- {"6", "Маточное молочко", "", ""},
- {"7", "Трутневое молочко", "", ""},
- {"8", "Пчелиный яд", "", ""},
- {"9", "Пчелиная огневка", "", ""},
- {"10", "Пчелиный подмор", "", ""}
- };
- // data - данные для таблицы, columns - колонки
- table = new JTable(data, columns);
- // создаем объект для скроллирования данных таблицы
- JScrollPane scrollPane = new JScrollPane(table);
- // помещаем таблицу по центру экрана
- p.add(scrollPane, BorderLayout.CENTER);
- // панель содержит кнопки Выполнить, Выход
- JPanel bottomPanel = new JPanel();
- JButton invokeB = new JButton("Выполнить");
- invokeB.addActionListener(this);
- JButton exitB = new JButton("Выход");
- exitB.addActionListener(this);
- bottomPanel.add(invokeB);
- bottomPanel.add(exitB);
- // добавляем панел bottomPanel - помещаем ее вниз нашей панели
- p.add(bottomPanel, BorderLayout.SOUTH);
- return p;
- }
- //1) По нажатию на кнопку Выполнить
- //получить данные из колонки с индексом columnNum в виде массива строк
- public String[] getTableData(int columnNum) {
- //
- System.out.println("getTableData_columnNum=" + columnNum);
- // для проверки метода getValueAt
- // String value = table.getValueAt(0, 3).toString();
- // System.out.println("value=" + value);
- String[] columnData = new String[10];
- for (int i = 0; i < columnData.length; i++) {
- columnData[i] = table.getValueAt(i, columnNum).toString();
- }
- return columnData;
- }
- // 2) По нажатию на кнопку Выполнить
- //установить данные в колонку columnNum
- //Данный метод позволяет установить (заполнить)
- //данные в колонку таблицы
- public void setTableData(int columnNum, String[] values) {
- // для проверки метода setValueAt
- //table.setValueAt("999", 0, 2);
- for(int i = 0; i < values.length; i++) {
- table.setValueAt(values[i], i, columnNum);
- }
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- System.out.println("e.command=" + e.getActionCommand());
- // выполняем сравнение двух строк с помощью метода equals
- if ("Выход".equals(e.getActionCommand())) {
- // выполняем выход из программы
- System.exit(0);
- } else if ("Выполнить".equals(e.getActionCommand())) {
- System.out.println("Выполнить INVOKED!");
- String[] arr = getTableData(3);
- String[] resultArray = Client.getData(arr);
- for(int i = 0; i < resultArray.length; i++) {
- String string = resultArray[i];
- System.out.println("string=" + string);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement