Wednesday, May 31, 2017

How to insert a tab into JTabbedPane instead of adding it to the end

public class TabTest { 
      public static void main(String[] args) { 
            JTabbedPane pane = new JTabbedPane(); 

            JLabel fruitLabel = new JLabel(new ImageIcon("fruit.gif"));
            pane.add(fruitLabel);

            String[] birds = {"Sparrow", "Pigeon", "Sea gull", "Eagle", "Parrot"};
            JList<String> birdList = new JList<>(birds);
            pane.add(birdList);

            int index = 1;

            JPanel sportTeamPane = new JPanel();
            sportTeamPane.add(new JTextArea("Here's the description of the team:"));
            //Add the component to a specific position instead of to the end
            pane.add(sportTeamPane, index);

            JTable table = new JTable(myPopulationTableModel);
            JScrollPane sp = new JScrollPane(table);
             //Insert the component to a specific position instead of to the end
            pane.insertTab("Population", new ImageIcon("population.gif"), sp, "population tab", index); 
      } 
}

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Thursday, May 11, 2017

SQL: retrieve the first / last number of digits from a number field

A. Retrieve the first number of digits

If the values in your number field have a fixed length, you can divide the number by 10 to the p power, where p is the number of digits you want to remove from the end of the number.

For example you have a hireDate in your employee table, which is in the Julian format such as 2017030. If you just want to retrieve the year of employment, you can use the SQL below.

SQL> select floor(hireDate/1000) as Year from employee;

If the values in your field does not have a fixed length, the following SQL can serve you purpose of retrieving the first number of digits.

SQL> select departmentName from department where substr(to_char(departmentNo), 1, 2) = '14';

Use the substr function, you can get digits starting from any position. For example. your employee code has the first 3 digits represent the college, followed by another 3 digits representing the department, and another 3 digits for employee status. The SQL below will help you to retrieve the depart part of the employee code

SQL> select name, substr(to_char(employeeCode), 4, 3) as dept from employee;

To get the employee status:

SQL> select name, substr(to_char(employeeCode), 7, 3) as status from employee;

B. Retrieve the last number of digits

To retrieve the last three digits of the above employeeCode,

SQL> select name, substr(to_char(employeeCode), length(employeeCode) - 3, 3) as status from employee;

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

SQL: substring of a string field - get the first number of characters of a string in Oracle

For example, you would like a list of employees whose first names start with "Chr", the SUBSTR function would help you to achieve it.

SUBSTR takes three arguments, the field name, the beginning position (starting from 1), and the length of the substring.

SQL> select firstName, middleName, lastName, title, department from employee
           where SUBSTR (firstName, 1, 3) = 'Chr';

The output will look something like:

firstName                middleName                   lastName            title                    department
----------------           ------------------                ---------------      ------------             --------------
Christine                  Anna                              Arendt                   librarian               Library
Christopher                                                     Jones                  security guard        Security


If you would like to display only the first four characters of the department:

SQL> select firstName, middleName, lastName, title, SUBSTR(department, 1,4) as department
           from employee
           where SUBSTR (firstName, 1, 3) = 'Chr';

The output would look something like:

firstName                middleName                   lastName            title                    department
----------------           ------------------                ---------------      ------------             --------------
Christine                  Anna                              Arendt                   librarian               Libr
Christopher                                                     Jones                  security guard        Secu


---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Wednesday, May 10, 2017

Java: If a string contains a number

If all you need to know is whether the string has a number in it or not, the code below should serve your purpose.

String str = "Great1Holoday!";
boolean hasNumber = str.matches(".*\\d.*");

However, if you need to know which position in the string is a number, the following code should help.

int position = 0;
for (int i=0; i < str.length(); i++) {
      if (Character.isDigit(str.charAt(i))) {
            position = i;
            break;
      }
}

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Tuesday, May 9, 2017

Set JTable header background, foreground and border

Setting a JTable header backgound color is similar to setting a JTable column background, both are done through the TableCellRenderer.

JTable table = new JTable(model);
table.getTableHeader().setDefaultRenderer(new MyTableHeaderRenderer());

public class MyTableHeaderRenderer extends DefaultTableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(
                JTable table, Object value,
                boolean isSelected,
                boolean hasFocus,
                int row, int col) {

            Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
         
            //Set the header of column 3 to a particular color
            if (col == 3) {
                comp.setBackground(Color.darkGray);
                comp.setForeground(Color.WHITE);
            } else {
                comp.setBackground(new Color(250,250,250));
                comp.setForeground(Color.BLACK);
            }

            //Set all headers with a border
            ((JLabel)comp).setBorder(BasicBorders.getInternalFrameBorder());

            return comp;
        }
    }

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Programmtically start editing a JTable cell as if you have double clicked in it

There are times after you have finished something, you want the editing caret to flash in one of your JTable cell where you would like the user to pay attention to.

This is how you can programmtically make the editing caret flash in a JTable cell as if the user has double clicked in it.

JTable table = new JTable();
table.setModel(<table model>);

//programmatically edit the cell in row 2 and column 3
table.setSelectedRow(2);
table.setSelectedCol(3);
if (table.editCellAt(2, 3)){
      table.getEditorComponent().requestFocusInWindow();
}

If you would like any cell in a JTable always in editing mode whenever it gains focus, you can modify the changeSelection method of JTable.

JTable table = new JTable() {
      public void changeSelection(int row, int col, boolean toggle, boolean extend) {
            super.changeSelection(row, col, toggle, extend);

            if (table.editCellAt(row, col)){
                  table.getEditorComponent().requestFocusInWindow();
           }
      }
};

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.