There is Jtree
based on the implemented TreeModel
, which is based on the sampling of MySQL-base. There are buttons by pressing which data is added / deleted in the MYSQL table. How to make data in jtree
automatically updated? Methods revalidate ()
and repaint ()
do not work. It works only updateui ()
, but for some reason, do not recommend using (by the way the second question – why not recommend?).
Code:
package catalogs;
Import java.awt.event.ActionEvent;
Import java.awt.event.actionlistener;
Import javax.swing.imageIcon;
Import javax.swing.jbutton;
import javax.swing.jinternalFrame;
Import javax.swing.joptionpane;
import javax.swing.jscrollplane;
Import javax.swing.jtable;
Import javax.swing.jtree;
Import javax.swing.listselectionmodel;
Import javax.swing.event.TableModellistener;
import javax.swing.event.treemodellistener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.treselectionListener;
import javax.swing.table.TableModel;
Import javax.swing.tree.Treemodel;
import javax.swing.tree.treepath;
Import main.global;
/ * ------ Class window of the reference book of goods .------ * /
Public Class Productscatalog Extends JinternalFrame {
Private Static Final Long SerialVersionuid = 1L;
// Ads The window component.
Private Jtree GroupTree;
Private GroupTreeModel GroupTreeModel;
Private JScrollPane GroupScrollpane;
Private JButton Btnaddgroup;
Private Jbutton Btndelgroup;
Private ButtonListener Btnlistener;
Private Treelistener GroupTreelistener;
Private JTABLE PRODTABLE;
Private prodtablemodel prodtablemodel;
Private JScrollPane ProdtablesCrlpane;
//Constructor.
Public Productscatalog () {
Global.db_name = "MyDB"; // Delete a string
// Initialization of the window component.
// Tree groups of goods.
GroupTreeModel = new groupTreemodel ();
GroupTree = New Jtree (GroupTreeModel);
Grouptree.setSelectionRow (0);
Grouptreelistener = New TreeListener ();
Grouptree.AddTreeSelectionListener (GroupTreelistener);
GroupTreescrLPane = New JSCrollplane (GroupTree);
grouptreescrlpane.setbounds (10, 10, 150, 200);
// Buttons for adding and removing product groups.
btnlistener = new buttonlistener ();
btnaddgroup = new jbutton ();
Imageicon btnaddgroupicon = new imageicon ("images / btnaddgroup.png");
btnaddgroup.seticon (btnaddgroupicon);
btnaddgroup.addactionlistener (btnlistener);
btnaddgroup.setbounds (10, 220, 30, 30);
btndelgroup = new jbutton ();
Imageicon btndelgroupicon = new imagecon ("images / btndelgroup.png");
Btndelgroup.Seticon (Btndelgroupicon);
btndelgroup.addactionlistener (Btnlistener);
btndelgroup.setbounds (50, 220, 30, 30);
// Table of goods.
PRODTABLEMODEL = NEW PRODTABLEMODEL ();
PRODTABLE = NEW JTABLE (PRODTABLEMODEL);
prodtablemodel.buildtable (grouptree.getlastselectedpathcomponent ());
PRODTABLE.SETAUTOCREATERSORTERTER (TRUE);
prodtable.setAutoresizemode (jtable.auto_resize_all_columns);
PRODTABLE.SELSELECTIONMODE (listselectionmodel.single_selection);
prodtable.getcolumnmodel (). GetColumn (0) .setPreferredWidth (25);
prodtable.getcolumnmodel (). GetColumn (1) .setPreferredWidth (200);
prodtable.getcolumnmodel (). GetColumn (2) .setPreferredWidth (25);
prodtable.getcolumnmodel (). GetColumn (3) .setPreferredWidth (50);
PRODTABLESCRLPANE = NEW JSCROLLPANE (PRODTABLE);
prodtablescrlpane.setbounds (170, 10, 500, 200);
// Window parameters.
this.settitle ("catalog of goods");
this.setclosable (TRUE);
this.setmaximizable (TRUE);
This.Seticonifiable (TRUE);
This.setResizable (TRUE);
this.getContentPane (). SETLAYOUT (NULL);
this.setsize (700, 350);
This.setvisible (TRUE);
This.Add (GroupTreescrLPane);
this.add (Btnaddgroup);
this.add (btndelgroup);
This.Add (ProdtablesCrlpane);
}
/ * ------ wood Model .------ * /
private class GroupTreeModel implements TreeModel {
@Override
public Object getChild (Object parent, int index) {
Object [] [] buf = Global.sqlQueryResult ( "SELECT * FROM` prod_group` WHERE `name` = '" + parent + "'");
Object parentCode = buf [0] [0];
Object [] [] res = Global.sqlQueryResult ( "SELECT * FROM` prod_group` WHERE `parent` = '" + parentCode + "'");
return res [index] [1];
}
@Override
public int getChildCount (Object parent) {
Object [] [] buf = Global.sqlQueryResult ( "SELECT * FROM` prod_group` WHERE `name` = '" + parent + "'");
Object parentCode = buf [0] [0];
Object [] [] res = Global.sqlQueryResult ( "SELECT * FROM` prod_group` WHERE `parent` = '" + parentCode + "'");
return res.length;
}
@Override
public int getIndexOfChild (Object parent, Object child) {
Object [] [] buf = Global.sqlQueryResult ( "SELECT * FROM` prod_group` WHERE `name` = '" + parent + "'");
Object parentCode = buf [0] [0];
Object [] [] res = Global.sqlQueryResult ( "SELECT * FROM` prod_group` WHERE `parent` = '" + parentCode + "'");
int index = 0;
for (int i = 0; i & lt; res.length; i ++) {
if (res [i] [1] .equals (child)) index = i;
}
return index;
}
@Override
public Object getRoot () {
Object [] [] res = Global.sqlQueryResult ( "SELECT * FROM` prod_group` WHERE `parent` = '0'");
return res [0] [1];
}
@Override
public boolean isLeaf (Object arg0) {
RETURN FALSE;
}
@Override
public void addTreeModelListener (TreeModelListener arg0) {}
@Override
public void removeTreeModelListener (TreeModelListener arg0) {}
@Override
public void valueForPathChanged (TreePath arg0, Object arg1) {}
}
/ * ------ listener changes the selected group tree line products .------ * /
public class TreeListener implements TreeSelectionListener {
@Override
public void valueChanged (TreeSelectionEvent selection) {
prodTableModel.buildTable (groupTree.getLastSelectedPathComponent ());
prodTable.revalidate ();
prodTable.repaint ();
}
}
/ * ------ listener click add / remove groups / goods buttons .------ * /
public class ButtonListener implements ActionListener {
@Override
Public Void ActionPerForMed (ActionEvent E) {
// If the button is pressed removal product groups.
if (e.getSource (). equals (btnDelGroup)) {
// Check if the folder is empty, which is removed.
boolean isEmpty = false;
Object [] [] buf = Global.sqlQueryResult ( "SELECT` id` FROM `prod_group` WHERE` name` = ' "+ groupTree.getLastSelectedPathComponent () +"' ");
Object [] [] buf2 = Global.sqlQueryResult ( "SELECT * FROM` product` WHERE `group` = '" + buf [0] [0] + "'");
Object [] [] buf3 = Global.sqlQueryResult ( "SELECT * FROM` prod_group` WHERE `parent` = '" + buf [0] [0] + "'");
if (buf2.length == 0 & amp; & amp; buf3.length == 0) isEmpty = true;
// If a group is selected, it is empty and we confirm the deletion, then remove.
if (! groupTree.isSelectionEmpty ()) {
if (isEmpty) {
if (JOptionPane.showConfirmDialog (null, "Are you sure?", "Delete Confirmation", JOptionPane.YES_NO_OPTION) == 0) {
Global.sqlQueryVoid ( "DELETE FROM` prod_group` WHERE `name` = '" + groupTree.getLastSelectedPathComponent () + "'");
//groupTree.updateUI ();
groupTree.setVisible (false);
groupTree.revalidate ();
groupTree.repaint ();
groupTree.setVisible (true);
}
} Else JOptionPane.showMessageDialog (null, "You can not delete the folder because it is not empty!", "Error", JOptionPane.ERROR_MESSAGE);
} ELSE JOPTIONPANE.SHOWMESSAGEDIALOG (NULL, "You have not selected any folder!", "Error", joptionpane.error_message);
}
}
}
/ * ------ Table model .------ * /
Public Class ProdtableModel IMPLEMENTS TableModel {
// Two-dimensional data array.
Private Object [] [] selection = null;
// Method for constructing a two-dimensional data array, based on which the product table is drawn.
Public Void Buildtable (Object Group) {
Object [] [] buf = global.sqlqueryresult ("Select` ID` from `prod_group` Where` name` = '" + group + "'");
selection = global.sqlqueryResult ("SELECT` ID`, `name`,` unit`, `comments` From` Product` Where `group` = '" + buf [0] [0] + "'");
}
@Override
Public Class & lt;? & gt; GetColumnClass (int colind) {
Return string.class;
}
@Override
Public int getColumnCount () {
Return 4;
}
@Override
Public String GetColumnname (int colind) {
STRING COLNAME = "";
Switch (colind) {
Case 0: colname = "code"; Break;
CASE 1: COLNAME = "NAME"; Break;
Case 2: Colname = "Ed. change"; Break;
CASE 3: COLNAME = "Comment"; Break;
}
RETURN COLNAME;
}
@Override
Public int getRowCount () {
Return selection.length;
}
@Override
Public Object GetValueat (Int Rowind, Int Colind) {
RETURN SELECTION [ROWIND] [colind];
}
@Override
Public void AddTableModellistener (Tablemodellistener Arg0) {}
@Override
Public Boolean IscelleDitable (int arg0, int arg1) {Return false;}
@Override
Public Void RemovetableModellistener (Tablemodellistener Arg0) {}
@Override
Public Void SetValueat (Object Arg0, int arg1, int arg2) {}
}
}
Answer 1, Authority 100%
You can shift the operations with data on the tree model (GroupTreeModel
), which will notify the view (JTree
) about changes. To do this, add to GroupTreeModel
Work of listeners:
set & lt; treemodellistener & gt; Listeners = New CopyonwriteArraySet & lt; & gt; ();
@Override
Public Void AddTreeModellistener (TreeModellistener Arg0) {
listeners.add (arg0);
}
@Override
Public Void Removetreemodellistener (TreeModellistener Arg0) {
listeners.Remove (arg0);
}
and removal method:
Public Void Removegroupatpath (Treepath SelectionPath) {
Object GROUPTOREMOVE = selectionpath.getlastpathcomponent ();
TreePath PathtopaRent = selectionpath.getparentpath ();
If (Pathtoparent! = NULL) {// One Does Not Simply Remove Jtree Root Node
Object parent = pathtoparent.getlastpathcomponent ();
Int Childindex = GetIndexofchild (Parent, GroupToremove);
Global.sqlqueryvoid ("delete from` prod_group` Where` name` = '"+ groupToremove +"' ");
// For the deletion event, you need a path to the parent node
// Index of the removed node in the list of parent nodes
// and deleted object
Firenoderemoved (Pathtoparent, New int [] {Childindex}, New Object [] {groupToremove});
}
}
Public Void Firenoderemoved (Treepath ParentPath, Int [] RemovedIndExes, Object [] Removedchildren) {
TreeModelevent E = New TreeModelevent (This, ParentPath, RemovedIndExes, Removedchildren);
For (TreeModellistener L: Listeners) {
L.TreenOnDesremoved (E);
}
}
Tree itself is registered in the list of listeners of the model and when removing is redrawn, as it should.
Further simply in the press handler, you can call the model method:
grouptreemodel.removegroupatpath (grouptree.getselectionpath ());
Forced redrawing is not required.