List, Table and Tree: added colors for drag-and-drop

- added "enable drag and drop" checkbox to Demo on "Data Components" tab
- support copying UI default values lazy
This commit is contained in:
Karl Tauber
2019-12-23 13:10:50 +01:00
parent ad82c591cc
commit 39a0d514a8
9 changed files with 637 additions and 133 deletions

View File

@@ -16,6 +16,9 @@
package com.formdev.flatlaf.demo;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import javax.swing.*;
import javax.swing.table.*;
import net.miginfocom.swing.*;
@@ -30,22 +33,53 @@ class DataComponentsPanel
initComponents();
}
private void dndChanged() {
boolean dnd = dndCheckBox.isSelected();
list1.setDragEnabled( dnd );
list2.setDragEnabled( dnd );
tree1.setDragEnabled( dnd );
tree2.setDragEnabled( dnd );
table1.setDragEnabled( dnd );
DropMode dropMode = dnd ? DropMode.ON_OR_INSERT : DropMode.USE_SELECTION;
list1.setDropMode( dropMode );
tree1.setDropMode( dropMode );
table1.setDropMode( dropMode );
String key = "FlatLaf.oldTransferHandler";
if( dnd ) {
list1.putClientProperty( key, list1.getTransferHandler() );
list1.setTransferHandler( new DummyTransferHandler() );
tree1.putClientProperty( key, tree1.getTransferHandler() );
tree1.setTransferHandler( new DummyTransferHandler() );
table1.putClientProperty( key, table1.getTransferHandler() );
table1.setTransferHandler( new DummyTransferHandler() );
} else {
list1.setTransferHandler( (TransferHandler) list1.getClientProperty( key ) );
tree1.setTransferHandler( (TransferHandler) tree1.getClientProperty( key ) );
table1.setTransferHandler( (TransferHandler) table1.getClientProperty( key ) );
}
}
@SuppressWarnings( { "unchecked", "rawtypes" } )
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
JLabel listLabel = new JLabel();
JScrollPane scrollPane1 = new JScrollPane();
JList<String> list1 = new JList<>();
list1 = new JList<>();
JScrollPane scrollPane2 = new JScrollPane();
JList<String> list2 = new JList<>();
list2 = new JList<>();
JLabel treeLabel = new JLabel();
JScrollPane scrollPane3 = new JScrollPane();
JTree tree1 = new JTree();
tree1 = new JTree();
JScrollPane scrollPane4 = new JScrollPane();
JTree tree2 = new JTree();
tree2 = new JTree();
JLabel tableLabel = new JLabel();
JScrollPane scrollPane5 = new JScrollPane();
JTable table1 = new JTable();
table1 = new JTable();
dndCheckBox = new JCheckBox();
//======== this ========
setLayout(new MigLayout(
@@ -57,7 +91,8 @@ class DataComponentsPanel
// rows
"[]" +
"[::200]" +
"[::150]"));
"[::150]" +
"[]"));
//---- listLabel ----
listLabel.setText("JList:");
@@ -65,15 +100,25 @@ class DataComponentsPanel
//======== scrollPane1 ========
{
scrollPane1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
scrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//---- list1 ----
list1.setModel(new AbstractListModel<String>() {
String[] values = {
"abc",
"de",
"f"
"item 1",
"item 2",
"item 3",
"item 4",
"item 5",
"item 6",
"item 7",
"item 8",
"item 9",
"item 10",
"item 11",
"item 12",
"item 13",
"item 14",
"item 15"
};
@Override
public int getSize() { return values.length; }
@@ -86,15 +131,25 @@ class DataComponentsPanel
//======== scrollPane2 ========
{
scrollPane2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
scrollPane2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//---- list2 ----
list2.setModel(new AbstractListModel<String>() {
String[] values = {
"abc",
"de",
"f"
"item 1",
"item 2",
"item 3",
"item 4",
"item 5",
"item 6",
"item 7",
"item 8",
"item 9",
"item 10",
"item 11",
"item 12",
"item 13",
"item 14",
"item 15"
};
@Override
public int getSize() { return values.length; }
@@ -115,6 +170,7 @@ class DataComponentsPanel
//---- tree1 ----
tree1.setShowsRootHandles(true);
tree1.setEditable(true);
scrollPane3.setViewportView(tree1);
}
add(scrollPane3, "cell 1 1,growx");
@@ -138,8 +194,18 @@ class DataComponentsPanel
//---- table1 ----
table1.setModel(new DefaultTableModel(
new Object[][] {
{"Item 1a", "Item 2a", "January", "July", 123, null},
{"Item 1b", "Item 2b", "February", "August", 456, true},
{"item 1", "item 1b", "January", "July", 123, null},
{"item 2", "item 2b", "February", "August", 456, true},
{"item 3", null, "March", null, null, null},
{"item 4", null, "April", null, null, null},
{"item 5", null, "May", null, null, null},
{"item 6", null, "June", null, null, null},
{"item 7", null, "July", null, null, null},
{"item 8", null, "August", null, null, null},
{"item 9", null, "September", null, null, null},
{"item 10", null, "October", null, null, null},
{"item 11", null, "November", null, null, null},
{"item 12", null, "December", null, null, null},
},
new String[] {
"Not editable", "Text", "Combo", "Combo Editable", "Integer", "Boolean"
@@ -197,11 +263,60 @@ class DataComponentsPanel
scrollPane5.setViewportView(table1);
}
add(scrollPane5, "cell 1 2 2 1,growx,width 300");
//---- dndCheckBox ----
dndCheckBox.setText("enable drag and drop");
dndCheckBox.setMnemonic('D');
dndCheckBox.addActionListener(e -> dndChanged());
add(dndCheckBox, "cell 0 3 3 1");
// JFormDesigner - End of component initialization //GEN-END:initComponents
((JComboBox)((DefaultCellEditor)table1.getColumnModel().getColumn( 3 ).getCellEditor()).getComponent()).setEditable( true );
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JList<String> list1;
private JList<String> list2;
private JTree tree1;
private JTree tree2;
private JTable table1;
private JCheckBox dndCheckBox;
// JFormDesigner - End of variables declaration //GEN-END:variables
//---- class DummyTransferHandler -----------------------------------------
private static class DummyTransferHandler
extends TransferHandler
{
@Override
protected Transferable createTransferable( JComponent c ) {
if( c instanceof JList && ((JList<?>)c).isSelectionEmpty() )
return null;
if( c instanceof JTree && ((JTree)c).isSelectionEmpty() )
return null;
if( c instanceof JTable && ((JTable)c).getSelectionModel().isSelectionEmpty() )
return null;
return new StringSelection( "dummy" );
}
@Override
public int getSourceActions( JComponent c ) {
return COPY;
}
@Override
public boolean canImport( TransferSupport support ) {
return support.isDataFlavorSupported( DataFlavor.stringFlavor );
}
@Override
public boolean importData( TransferSupport support ) {
String message = String.valueOf( support.getDropLocation() );
SwingUtilities.invokeLater( () -> {
JOptionPane.showMessageDialog( null, message, "Drop", JOptionPane.PLAIN_MESSAGE );
} );
return false;
}
}
}

View File

@@ -9,7 +9,7 @@ new FormModel {
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "hidemode 3"
"$columnConstraints": "[][200][200]"
"$rowConstraints": "[][::200][::150]"
"$rowConstraints": "[][::200][::150][]"
} ) {
name: "this"
add( new FormComponent( "javax.swing.JLabel" ) {
@@ -20,17 +20,28 @@ new FormModel {
} )
add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) {
name: "scrollPane1"
"verticalScrollBarPolicy": 21
"horizontalScrollBarPolicy": 31
add( new FormComponent( "javax.swing.JList" ) {
name: "list1"
"model": &DefaultListModel0 new javax.swing.DefaultListModel {
addElement( "abc" )
addElement( "de" )
addElement( "f" )
"model": new javax.swing.DefaultListModel {
addElement( "item 1" )
addElement( "item 2" )
addElement( "item 3" )
addElement( "item 4" )
addElement( "item 5" )
addElement( "item 6" )
addElement( "item 7" )
addElement( "item 8" )
addElement( "item 9" )
addElement( "item 10" )
addElement( "item 11" )
addElement( "item 12" )
addElement( "item 13" )
addElement( "item 14" )
addElement( "item 15" )
}
auxiliary() {
"JavaCodeGenerator.typeParameters": "String"
"JavaCodeGenerator.variableLocal": false
}
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
@@ -38,14 +49,29 @@ new FormModel {
} )
add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) {
name: "scrollPane2"
"verticalScrollBarPolicy": 21
"horizontalScrollBarPolicy": 31
add( new FormComponent( "javax.swing.JList" ) {
name: "list2"
"model": #DefaultListModel0
"model": new javax.swing.DefaultListModel {
addElement( "item 1" )
addElement( "item 2" )
addElement( "item 3" )
addElement( "item 4" )
addElement( "item 5" )
addElement( "item 6" )
addElement( "item 7" )
addElement( "item 8" )
addElement( "item 9" )
addElement( "item 10" )
addElement( "item 11" )
addElement( "item 12" )
addElement( "item 13" )
addElement( "item 14" )
addElement( "item 15" )
}
"enabled": false
auxiliary() {
"JavaCodeGenerator.typeParameters": "String"
"JavaCodeGenerator.variableLocal": false
}
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
@@ -62,6 +88,10 @@ new FormModel {
add( new FormComponent( "javax.swing.JTree" ) {
name: "tree1"
"showsRootHandles": true
"editable": true
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 1,growx"
@@ -71,6 +101,9 @@ new FormModel {
add( new FormComponent( "javax.swing.JTree" ) {
name: "tree2"
"enabled": false
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 1,growx"
@@ -87,21 +120,101 @@ new FormModel {
name: "table1"
"model": new com.jformdesigner.model.SwingTableModel( new java.util.Vector {
add( new java.util.Vector {
add( "Item 1a" )
add( "Item 2a" )
add( "item 1" )
add( "item 1b" )
add( "January" )
add( "July" )
add( 123 )
add( null )
} )
add( new java.util.Vector {
add( "Item 1b" )
add( "Item 2b" )
add( "item 2" )
add( "item 2b" )
add( "February" )
add( "August" )
add( 456 )
add( true )
} )
add( new java.util.Vector {
add( "item 3" )
add( null )
add( "March" )
add( null )
add( null )
add( null )
} )
add( new java.util.Vector {
add( "item 4" )
add( null )
add( "April" )
add( null )
add( null )
add( null )
} )
add( new java.util.Vector {
add( "item 5" )
add( null )
add( "May" )
add( null )
add( null )
add( null )
} )
add( new java.util.Vector {
add( "item 6" )
add( null )
add( "June" )
add( null )
add( null )
add( null )
} )
add( new java.util.Vector {
add( "item 7" )
add( null )
add( "July" )
add( null )
add( null )
add( null )
} )
add( new java.util.Vector {
add( "item 8" )
add( null )
add( "August" )
add( null )
add( null )
add( null )
} )
add( new java.util.Vector {
add( "item 9" )
add( null )
add( "September" )
add( null )
add( null )
add( null )
} )
add( new java.util.Vector {
add( "item 10" )
add( null )
add( "October" )
add( null )
add( null )
add( null )
} )
add( new java.util.Vector {
add( "item 11" )
add( null )
add( "November" )
add( null )
add( null )
add( null )
} )
add( new java.util.Vector {
add( "item 12" )
add( null )
add( "December" )
add( null )
add( null )
add( null )
} )
}, new java.util.Vector {
add( "Not editable" )
add( "Text" )
@@ -132,10 +245,24 @@ new FormModel {
add( null )
} )
"autoCreateRowSorter": true
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2 2 1,growx,width 300"
} )
add( new FormComponent( "javax.swing.JCheckBox" ) {
name: "dndCheckBox"
"text": "enable drag and drop"
"mnemonic": 68
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "dndChanged", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 3 3 1"
} )
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 )
"size": new java.awt.Dimension( 790, 715 )