Created
October 24, 2018 02:58
-
-
Save jokandre/dfb38fd5b9ad33ee22126ca3d5daba4c to your computer and use it in GitHub Desktop.
Java many components to one table Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * 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 singleui_manycomponents; | |
| import static java.lang.Math.random; | |
| import java.util.Date; | |
| import java.util.HashMap; | |
| import java.util.Random; | |
| import java.util.Timer; | |
| import java.util.TimerTask; | |
| import javax.swing.JTable; | |
| import javax.swing.table.DefaultTableModel; | |
| /** | |
| * | |
| * @author jokandre | |
| */ | |
| public class SingleUI_ManyComponents { | |
| NewJFrame mainApp = null; | |
| String ID; | |
| // Object[][] data; | |
| DefaultTableModel personalDTM; | |
| DefaultTableModel table; | |
| HashMap map; | |
| Integer rowCount = 0; | |
| SingleUI_ManyComponents(NewJFrame UIframe, String ident) { | |
| ID = ident; | |
| mainApp = UIframe; | |
| table = (DefaultTableModel) mainApp.getjTableMain().getModel(); | |
| map = new HashMap(); | |
| Object[][] data = { | |
| {},}; | |
| Object[] colNames = { | |
| "IP", "TagID", "Count", "Date" | |
| }; | |
| personalDTM = new DefaultTableModel(data, colNames); | |
| personalDTM.removeRow(0); | |
| } | |
| public void doSomething() { | |
| TimerTask timerTask; | |
| timerTask = new TimerTask() { | |
| @Override | |
| public void run() { | |
| //change data | |
| System.out.println("Run obj=" + ID); | |
| // table.insertRow(0, new Object[]{ID, new Date().toString()}); | |
| String tagID = String.valueOf(new Random().nextInt(10000)); | |
| /*Personal Table*/ | |
| rowCount = personalDTM.getRowCount(); | |
| Object IDinRow = map.putIfAbsent(tagID, rowCount); | |
| if (IDinRow == null) { | |
| personalDTM.insertRow(rowCount, new Object[]{ID, tagID, 1, new Date().toString()}); | |
| } else { | |
| // System.out.println(rowCount); | |
| personalDTM.setValueAt((int) personalDTM.getValueAt((int) IDinRow, personalDTM.findColumn("Count")) + 1, (int) IDinRow, personalDTM.findColumn("Count")); | |
| personalDTM.setValueAt(new Date().toString(), (int) IDinRow, personalDTM.findColumn("Date")); | |
| //map.toString() + | |
| System.out.println( " " + rowCount + " tag:" + tagID + "_" + personalDTM.getValueAt((int) IDinRow, personalDTM.findColumn("TagID"))); | |
| } | |
| // System.out.println(map.toString() + " " + rowCount+" tag:"+tagID+"_"+ personalDTM.getValueAt((int) IDinRow, personalDTM.findColumn("TagID")) ); | |
| /*Overall tableModel*/ | |
| rowCount = mainApp.tableMainModel.getRowCount(); | |
| IDinRow = mainApp.main_map.putIfAbsent(tagID, rowCount); | |
| if (IDinRow == null) { | |
| mainApp.tableMainModel.insertRow(rowCount, new Object[]{ID, tagID, 1, new Date().toString()}); | |
| } else { | |
| mainApp.tableMainModel.setValueAt((int) mainApp.tableMainModel.getValueAt((int) IDinRow, mainApp.tableMainModel.findColumn("Count")) + 1, (int) IDinRow, mainApp.tableMainModel.findColumn("Count")); | |
| mainApp.tableMainModel.setValueAt(new Date().toString(), (int) IDinRow, mainApp.tableMainModel.findColumn("Date")); | |
| mainApp.tableMainModel.setValueAt(ID, (int) IDinRow, mainApp.tableMainModel.findColumn("IP")); | |
| } | |
| } | |
| }; | |
| //running timer task as daemon thread | |
| Timer timer = new Timer(true); | |
| timer.scheduleAtFixedRate(timerTask, 0, 1 * 10); | |
| System.out.println("TimerTask id=" + ID + " started"); | |
| //cancel after sometime | |
| // try { | |
| // Thread.sleep(1200); | |
| // } catch (InterruptedException e) { | |
| // } | |
| // timer.cancel(); | |
| // System.out.println("TimerTask cancelled"); | |
| // try { | |
| // Thread.sleep(30000); | |
| // } catch (InterruptedException e) { | |
| // } | |
| } | |
| /** | |
| * @param args the command line arguments | |
| */ | |
| public static void main(String[] args) { | |
| // TODO code application logic here | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * 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 singleui_manycomponents; | |
| import java.awt.FlowLayout; | |
| import java.awt.GridLayout; | |
| import java.awt.event.ActionEvent; | |
| import java.awt.event.ActionListener; | |
| import java.util.ArrayList; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import javax.swing.JButton; | |
| import javax.swing.JTable; | |
| import javax.swing.table.DefaultTableModel; | |
| /** | |
| * | |
| * @author jokandre | |
| * - See what individual components are doing | |
| * - Sorting tables while running works but tend to cause Nullpoiters | |
| * in the background | |
| * - | |
| */ | |
| public class NewJFrame extends javax.swing.JFrame { | |
| /** | |
| * Creates new form NewJFrame | |
| */ | |
| List<SingleUI_ManyComponents> components; | |
| HashMap main_map = new HashMap(); | |
| DefaultTableModel tableMainModel; | |
| public NewJFrame() { | |
| initComponents(); | |
| tableMainModel = (DefaultTableModel) jTableMain.getModel(); | |
| initObjs(); | |
| } | |
| /** | |
| * This method is called from within the constructor to initialize the form. | |
| * WARNING: Do NOT modify this code. The content of this method is always | |
| * regenerated by the Form Editor. | |
| */ | |
| @SuppressWarnings("unchecked") | |
| // <editor-fold defaultstate="collapsed" desc="Generated Code"> | |
| private void initComponents() { | |
| jScrollPane1 = new javax.swing.JScrollPane(); | |
| jTableMain = new javax.swing.JTable(); | |
| jButton1 = new javax.swing.JButton(); | |
| jButton2 = new javax.swing.JButton(); | |
| jButton3 = new javax.swing.JButton(); | |
| jScrollPaneButtons = new javax.swing.JScrollPane(); | |
| jPanel1 = new javax.swing.JPanel(); | |
| setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); | |
| jTableMain.setAutoCreateRowSorter(true); | |
| jTableMain.setModel(new javax.swing.table.DefaultTableModel( | |
| new Object [][] { | |
| }, | |
| new String [] { | |
| "IP", "TagID", "Count", "Date" | |
| } | |
| ) { | |
| boolean[] canEdit = new boolean [] { | |
| false, false, false, false | |
| }; | |
| public boolean isCellEditable(int rowIndex, int columnIndex) { | |
| return canEdit [columnIndex]; | |
| } | |
| }); | |
| jTableMain.setColumnSelectionAllowed(true); | |
| jScrollPane1.setViewportView(jTableMain); | |
| jTableMain.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); | |
| jButton1.setText("jButton1"); | |
| jButton1.addActionListener(new java.awt.event.ActionListener() { | |
| public void actionPerformed(java.awt.event.ActionEvent evt) { | |
| jButton1ActionPerformed(evt); | |
| } | |
| }); | |
| jButton2.setText("jButton2"); | |
| jButton2.addActionListener(new java.awt.event.ActionListener() { | |
| public void actionPerformed(java.awt.event.ActionEvent evt) { | |
| jButton2ActionPerformed(evt); | |
| } | |
| }); | |
| jButton3.setText("jButton3"); | |
| jButton3.addActionListener(new java.awt.event.ActionListener() { | |
| public void actionPerformed(java.awt.event.ActionEvent evt) { | |
| jButton3ActionPerformed(evt); | |
| } | |
| }); | |
| javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); | |
| jPanel1.setLayout(jPanel1Layout); | |
| jPanel1Layout.setHorizontalGroup( | |
| jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
| .addGap(0, 489, Short.MAX_VALUE) | |
| ); | |
| jPanel1Layout.setVerticalGroup( | |
| jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
| .addGap(0, 100, Short.MAX_VALUE) | |
| ); | |
| jScrollPaneButtons.setViewportView(jPanel1); | |
| javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); | |
| getContentPane().setLayout(layout); | |
| layout.setHorizontalGroup( | |
| layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
| .addGroup(layout.createSequentialGroup() | |
| .addContainerGap() | |
| .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
| .addComponent(jScrollPaneButtons) | |
| .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING) | |
| .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() | |
| .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
| .addGroup(layout.createSequentialGroup() | |
| .addComponent(jButton1) | |
| .addGap(18, 18, 18) | |
| .addComponent(jButton2)) | |
| .addComponent(jButton3)) | |
| .addGap(0, 0, Short.MAX_VALUE))) | |
| .addContainerGap()) | |
| ); | |
| layout.setVerticalGroup( | |
| layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
| .addGroup(layout.createSequentialGroup() | |
| .addContainerGap() | |
| .addComponent(jButton3) | |
| .addGap(11, 11, 11) | |
| .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) | |
| .addComponent(jButton1) | |
| .addComponent(jButton2)) | |
| .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
| .addComponent(jScrollPaneButtons, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE) | |
| .addGap(18, 18, 18) | |
| .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE) | |
| .addContainerGap()) | |
| ); | |
| pack(); | |
| }// </editor-fold> | |
| private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { | |
| jTableMain.setModel(components.get(0).personalDTM); | |
| //System.out.println(components.get(0).personalDTM.getDataVector()); | |
| } | |
| private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { | |
| jTableMain.setModel(components.get(1).personalDTM); | |
| } | |
| private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { | |
| jTableMain.setModel(tableMainModel); | |
| } | |
| /** | |
| * @param args the command line arguments | |
| */ | |
| public static void main(String args[]) { | |
| /* Set the Nimbus look and feel */ | |
| //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> | |
| /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. | |
| * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html | |
| */ | |
| try { | |
| for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { | |
| if ("Nimbus".equals(info.getName())) { | |
| javax.swing.UIManager.setLookAndFeel(info.getClassName()); | |
| break; | |
| } | |
| } | |
| } catch (ClassNotFoundException ex) { | |
| java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
| } catch (InstantiationException ex) { | |
| java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
| } catch (IllegalAccessException ex) { | |
| java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
| } catch (javax.swing.UnsupportedLookAndFeelException ex) { | |
| java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
| } | |
| //</editor-fold> | |
| /* Create and display the form */ | |
| java.awt.EventQueue.invokeLater(new Runnable() { | |
| public void run() { | |
| new NewJFrame().setVisible(true); | |
| } | |
| }); | |
| } | |
| // Variables declaration - do not modify | |
| private javax.swing.JButton jButton1; | |
| private javax.swing.JButton jButton2; | |
| private javax.swing.JButton jButton3; | |
| private javax.swing.JPanel jPanel1; | |
| private javax.swing.JScrollPane jScrollPane1; | |
| private javax.swing.JScrollPane jScrollPaneButtons; | |
| private javax.swing.JTable jTableMain; | |
| // End of variables declaration | |
| private void initObjs() { | |
| components = new ArrayList<>(); | |
| components.add(new SingleUI_ManyComponents(this, "x.x.x.001")); | |
| components.add(new SingleUI_ManyComponents(this, "x.x.x.002")); | |
| components.add(new SingleUI_ManyComponents(this, "x.x.x.003")); | |
| components.add(new SingleUI_ManyComponents(this, "x.x.x.004")); | |
| components.add(new SingleUI_ManyComponents(this, "x.x.x.005")); | |
| jPanel1.setLayout( new FlowLayout() ); | |
| for (SingleUI_ManyComponents comp : components) { | |
| JButton b = new JButton(comp.ID); | |
| b.addActionListener(new ActionListener() { | |
| @Override | |
| public void actionPerformed(ActionEvent e) { | |
| jTableMain.setModel(comp.personalDTM); | |
| } | |
| }); | |
| jPanel1.add(b); | |
| comp.doSomething(); | |
| } | |
| } | |
| public JTable getjTableMain() { | |
| return jTableMain; | |
| } | |
| public void setjTableMain(JTable jTableMain) { | |
| this.jTableMain = jTableMain; | |
| } | |
| // JTable getjTableMain() { | |
| // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. | |
| // } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment