Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7970487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:24:26+00:00 2026-06-04T07:24:26+00:00

I am working on an application that uses Swing. I have a JTabbedPane, and

  • 0

I am working on an application that uses Swing. I have a JTabbedPane, and each tab is considered a ‘page.’ Each page contains 4 normal panels (I refer to them as ‘views’) that are arranged according to 2×2 GridLayout.

I want to minimize the amount of pages, so every time a view is removed, I want to re-sort all of the views across all of the pages (think two-dimensional arrays if it makes more sense) so that the views near the last page and removed from there, and are added to a page nearer the front.

Consider this example:

Object[][] array = new Object [][] {

    { new Object(), null, new Object(), new Object() },
    { null, null, new Object(), new Object() },
    { new Object(), new Object(), new Object(), new Object() }

};

How can I sort that array so that it looks more like:

Object[][] array = new Object[][] {

    { new Object(), new Object(), new Object(), new Object() },
    { new Object(), new Object(), new Object(), new Object() },
    { new Object(), null, null, null },

};

At first, I thought of using two loops, one going from 0 to array.length, and one going from array.length to 0. The idea was that: as the one going from length to 0 approaches 0, it would check whether or not the indices of the array going from 0 to length are empty. If so, it would place the non-null element in the index that contains null.

This approach gave me a headache because of all the looping so I asked a close friend of mine for a suggestion. He suggested a much more elegant solution: Arrays.sort(Object[][], Comparator).

This code was the result:

    Object[][] array = new Object[][] { { new Object(), null, new Object(), new Object() }, { null, null, new Object(), new Object() }, { new Object(), new Object(), new Object(), new Object() } };

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            System.out.println("Before sorting: (i = " + i + " j = " + j + " null = " + (array[i][j] == null) + ")");
        }
    }

    Arrays.sort(array, new Comparator<Object>()
    {

        public int compare(Object a, Object b)
        {
            return a == null ? (b == null ? 0 : -1) : (b == null ? 1 : 0);
        }

    });

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            System.out.println("After sorting: (i = " + i + " j = " + j + " null = " + (array[i][j] == null) + ")");
        }
    }

The output is:

Before sorting: (i = 0 j = 0 null = false)
Before sorting: (i = 0 j = 1 null = true)
Before sorting: (i = 0 j = 2 null = false)
Before sorting: (i = 0 j = 3 null = false)
Before sorting: (i = 1 j = 0 null = true)
Before sorting: (i = 1 j = 1 null = true)
Before sorting: (i = 1 j = 2 null = false)
Before sorting: (i = 1 j = 3 null = false)
Before sorting: (i = 2 j = 0 null = false)
Before sorting: (i = 2 j = 1 null = false)
Before sorting: (i = 2 j = 2 null = false)
Before sorting: (i = 2 j = 3 null = false)
After sorting: (i = 0 j = 0 null = false)
After sorting: (i = 0 j = 1 null = true)
After sorting: (i = 0 j = 2 null = false)
After sorting: (i = 0 j = 3 null = false)
After sorting: (i = 1 j = 0 null = true)
After sorting: (i = 1 j = 1 null = true)
After sorting: (i = 1 j = 2 null = false)
After sorting: (i = 1 j = 3 null = false)
After sorting: (i = 2 j = 0 null = false)
After sorting: (i = 2 j = 1 null = false)
After sorting: (i = 2 j = 2 null = false)
After sorting: (i = 2 j = 3 null = false)

Exactly the same.
I have also tried replacing the compare(Object, Object) implementation with:

        public int compare(Object a, Object b)
        {
            if (a == null && b != null)
            {
                return -1;
            }
            if (b == null && a != null)
            {
                return 1;
            }
            return 0;
        }

… and have achieved the same results.
I am kind of at a loss. This is not something that I do not have the knowledge to do, I just cannot wrap my head around how to actually create a solution for a problem like this.

I’d appreciate any help. Whichever way you prefer approaching it, the loop method or the Comparator method, I’d love to see it!

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:24:27+00:00Added an answer on June 4, 2026 at 7:24 am

    Did you mean

    Object[][] array = new Object [][] { .. };
    

    In your case, you need to convert 2-D array to array (1-D array). After sorting new array, you fill the 2-D array with the sorted array.

    // convert to 1-D array
    Object[] all = new Object[12];
    int k = 0;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            all[k++] = array[i][j];
        }
    }
    
    // then sort the new array
    Arrays.sort(all, yourComparator);
    
    // then fill the 2-D array with the sorted array
    k = 0;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            array[i][j] = all[k++];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that uses Swing. The display I am working on, uses
I am working on an application that uses Swing. I have successfully created a
I am thinking of working on a Rails application that uses PostgreSQL. I have
I am currently working on rails3 application that uses jQuery. I have a javascript
So I have been working on this phone gap application that uses jquery-mobile. Recently,
I have a working web application that uses username/password SpringSecurity configuration. Now I want
I'm working on an application that uses the Google Maps API. I have no
I need a working example of an application that uses an expandableListView. I have
I am working on an application that uses Oracle's built in authentication mechanisms to
I'm working on an application that uses multiple threads to process its data. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.