Wednesday, December 12, 2012

Height of tab (JTabbedPane) in MAC

Code: 
JTabbedPane paneItem = new JTabbedPane();
JLabel iconInTab = new JLabel(new ImageIcon("myImage.png"));
iconInTab.setPreferredSize(new Dimension(100,80)); 
paneItem.addTab(null,new JPanel());
paneItem.setTabComponentAt(0,iconInTab);
 
I've also try this using html but it did not work either:
paneItem.addTab("<html><p><p><p></html>",new ImageIcon("myImage.png"),new JPanel()) 

Solution:
paneItem.setUI(new BasicTabbedPaneUI());
Also with some features:
paneItem.setTabPlacement(JTabbedPane.TOP);
paneItem.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
paneItem.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

Tuesday, December 11, 2012

Move all files except one

File Structure:
/root/tmp/test
[root@dswing test]# ll
total 12
-rw-r--r-- 1 root root    2 Dec 12 13:41 a.txt
-rw-r--r-- 1 root root    4 Dec 12 13:42 b.txt
-rw-r--r-- 1 root root    0 Dec 12 13:43 c.txt
drwxr-xr-x 2 root root 4096 Dec 12 13:50 ks

Command:
find /root/tmp/test/ -maxdepth 1 -mindepth 1 -type f -not -name c.txt | grep -i txt$ | xargs -i cp -af {} /root/tmp/test/ks

Description:
-maxdepth 1 makes it not search recursively.  
-mindepth 1 makes it not include the /root/tmp/test/ path itself into the result.
-type f means search only for file type.
-not -name c.txt means all files except c.txt
grep -i txt$ means files name ends with txt. 

Put a.txt -file to the end of b.txt -file

In a bash script such append.sh file, write the following code: 
#!/bin/bash
A=/root/tmp/test/a.txt
B=/root/tmp/test/b.txt
cat $A >> $B


The >> redirects the output of the cat command (which output file A) to the file B. But instead of overwriting contents of B, it appends to it. If you use a single >, it will instead overwrite any previous content in B.

Check if a program exists from a bash script

$ command -v foo >/dev/null 2>&1 || { 
  echo >&2 "I require foo but it's not installed.  Aborting."; exit 1;  
}
OR 
$ type foo >/dev/null 2>&1 || { 
  echo >&2 "I require foo but it's not installed.  Aborting."; exit 1;  
}
OR 
$ hash foo 2>/dev/null || { 
  echo >&2 "I require foo but it's not installed.  Aborting."; exit 1;  
}

Resize a list of images in line command

for file in *.jpg; do 
  convert -resize 800x600 -- "$file" "${file%%.jpg}-resized.jpg"; 
done
 
OR 
 
ls *.jpg|sed -e 's/\..*//'|xargs -I X convert X.jpg whatever-options X-resized.jpg 
 
 
# .jpg files, 10% size, no rename, it will overwrite the old pictures
#!/bin/bash
for i in *.jpg; do convert $i -resize 10% $(basename $i .jpg).jpg; done

How to output MySQL query results in csv format

SELECT 'Order Id', 'Product Name', 'Quantity'
UNION
(
SELECT order_id,product_name,qty
FROM orders
INTO OUTFILE 'C:\\tmp\\orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
)

How to do something with bash when a text line appear to a file

Use command
tail -f file.log | grep --line-buffered "my pattern" | while read line
do
  echo $line
done
The --line-buffered is the key here, otherwise the read will fail.