Created
February 8, 2014 14:28
-
-
Save Getaji/8884559 to your computer and use it in GitHub Desktop.
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
import javax.imageio.ImageIO; | |
import javax.swing.*; | |
import javax.swing.border.CompoundBorder; | |
import javax.swing.border.EmptyBorder; | |
import javax.swing.border.LineBorder; | |
import java.awt.*; | |
import java.awt.datatransfer.DataFlavor; | |
import java.awt.datatransfer.Transferable; | |
import java.awt.datatransfer.UnsupportedFlavorException; | |
import java.awt.dnd.DnDConstants; | |
import java.awt.dnd.DropTarget; | |
import java.awt.dnd.DropTargetDropEvent; | |
import java.awt.event.KeyEvent; | |
import java.awt.event.KeyListener; | |
import java.io.File; | |
import java.io.IOException; | |
/** | |
* javadoc here. | |
* | |
* @author Getaji | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
final JDialog dialog = new JDialog((JFrame) null, "Input"); | |
dialog.setSize(400, 200); | |
final EmptyBorder marginBorder = new EmptyBorder(5, 5, 5, 5); | |
final JLabel imageLabel = new JLabel(); | |
imageLabel.setHorizontalAlignment(JLabel.CENTER); | |
final CompoundBorder imageBorder = new CompoundBorder( | |
new LineBorder(Color.GRAY), | |
marginBorder | |
); | |
dialog.add(imageLabel, BorderLayout.NORTH); | |
final JTextArea area = new JTextArea(); | |
final int padding = 2; | |
area.setBorder(new CompoundBorder( | |
marginBorder, | |
new CompoundBorder( | |
new LineBorder(Color.GRAY), | |
marginBorder | |
))); | |
dialog.add(area, BorderLayout.CENTER); | |
dialog.setBackground(Color.GRAY); | |
final JPanel panel = new JPanel(); | |
panel.setBorder(marginBorder); | |
panel.setLayout(new FlowLayout(FlowLayout.RIGHT)); | |
final String _140 = "/140"; | |
final JLabel label = new JLabel(0 + _140); | |
final JButton button = new JButton("TWEET"); | |
panel.add(label); | |
panel.add(button); | |
dialog.add(panel, BorderLayout.SOUTH); | |
area.addKeyListener(new KeyListener() { | |
@Override | |
public void keyTyped(KeyEvent e) { | |
label.setText(String.valueOf(area.getText().length()) + _140); | |
} | |
@Override public void keyPressed(KeyEvent e) {} | |
@Override public void keyReleased(KeyEvent e) {} | |
}); | |
area.setDropTarget(new DropTarget() { | |
@Override public void drop(DropTargetDropEvent e) { | |
try { | |
Transferable transfer = e.getTransferable(); | |
// ファイルリストの転送を受け付ける | |
if (transfer.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { | |
// copyとmoveを受け付ける | |
e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); | |
// ドラッグ&ドロップされたファイルのリストを取得 | |
java.util.List<File> fileList = | |
(java.util.List<File>) transfer.getTransferData( | |
DataFlavor.javaFileListFlavor); | |
if (0 < fileList.size() && fileList.get(0).isFile()) { | |
System.out.println(fileList.get(0).getAbsolutePath()); | |
ImageIcon imageIcon = new ImageIcon( | |
ImageIO.read(fileList.get(0)) | |
); | |
imageLabel.setIcon(imageIcon); | |
imageLabel.setPreferredSize(new Dimension(Integer.MAX_VALUE, 50)); | |
if (dialog.getHeight() <= 200) { | |
dialog.setSize(dialog.getWidth(), 250); | |
} | |
} | |
} | |
} catch (UnsupportedFlavorException e1) { | |
e1.printStackTrace(); | |
} catch (IOException e2) { | |
e2.printStackTrace(); | |
} | |
} | |
}); | |
dialog.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment