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

  • Home
  • SEARCH
  • 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 6792201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:56:25+00:00 2026-05-26T17:56:25+00:00

I’m planning on making an editor for my current project and will need some

  • 0

I’m planning on making an editor for my current project and will need some java swing for it.

Basically I need a grid within some sort of frame; the single elements of the grid should be able to be selected via click and with a dropdown/selector element you should be able to change the color of the grid element

Can anyone tell me what parts of swing I’ll need for that? Any help would be really appreciated 😉

Edit: let’s go a bit into detail

This editor is planned to generate maps for an android strategy game I develope with some friends of mine
Let’s say we have a square field of 16×16 fields which are all by default green.
By selecting the single field I want to be able to change the color of this field to something else.

In addition, every field should be able to return it’s value (i.e. the color)

  • 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-05-26T17:56:26+00:00Added an answer on May 26, 2026 at 5:56 pm

    Your question is a little short on details, but perhaps you want a JPanel that uses GridLayout and holds an array of JLabels whose opaque property is true. You could add a MouseListener to the JLabels that shows a JPopupMenu that shows possible colors, and then depending on the selection use it to set the JLabel’s background color (which shows since it has been made opaque).

    For example:

    Main.java

    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class Main {
       private static void createAndShowGui() {
          int rows = 20;
          int cols = 40;
          int cellWidth = 20;
          ColorGrid mainPanel = new ColorGrid(rows, cols, cellWidth);
    
          JFrame frame = new JFrame("Color Grid Example");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    

    ColorGrid.java

    import java.awt.Dimension;
    import java.awt.GridLayout;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class ColorGrid extends JPanel {
       private MyColor[][] myColors;
       private JLabel[][] myLabels;
    
       public ColorGrid(int rows, int cols, int cellWidth) {
          myColors = new MyColor[rows][cols];
          myLabels = new JLabel[rows][cols];
    
          MyMouseListener myListener = new MyMouseListener(this);
          Dimension labelPrefSize = new Dimension(cellWidth, cellWidth);
          setLayout(new GridLayout(rows, cols));
          for (int row = 0; row < myLabels.length; row++) {
             for (int col = 0; col < myLabels[row].length; col++) {
                JLabel myLabel = new JLabel();
                myLabel = new JLabel();
                myLabel.setOpaque(true);
                MyColor myColor = MyColor.GREEN;
                myColors[row][col] = myColor;
                myLabel.setBackground(myColor.getColor());
                myLabel.addMouseListener(myListener);
                myLabel.setPreferredSize(labelPrefSize);
                add(myLabel);
                myLabels[row][col] = myLabel;
             }
          }
       }
    
       public MyColor[][] getMyColors() {
          return myColors;
       }
    
       public void labelPressed(JLabel label) {
          for (int row = 0; row < myLabels.length; row++) {
             for (int col = 0; col < myLabels[row].length; col++) {
                if (label == myLabels[row][col]) {
                   MyColor myColor = myColors[row][col].next();
                   myColors[row][col] = myColor;
                   myLabels[row][col].setBackground(myColor.getColor());
                }
             }
          }
       }
    }
    

    MyColor.java

    import java.awt.Color;
    
    public enum MyColor {
       GREEN(Color.green, "Green", "g"), RED(Color.red, "Red", "r"), 
       BLUE(Color.blue, "Blue", "b"), YELLOW(Color.yellow, "Yellow", "y");
       private Color color;
       private String name;
       private String shortName;
    
       private MyColor(Color color, String name, String shortName) {
          this.color = color;
          this.name = name;
          this.shortName = shortName;
       }
    
       public MyColor next() {
          int index = 0;
          for (int i = 0; i < MyColor.values().length; i++) {
             if (MyColor.values()[i] == this) {
                index = (i + 1) % MyColor.values().length;
             }
          }
          return MyColor.values()[index];
       }
    
       public Color getColor() {
          return color;
       }
    
       public String getName() {
          return name;
       }
    
       public String getShortName() {
          return shortName;
       }
    
       @Override
       public String toString() {
          return shortName;
       }
    
    }
    

    MyMouseListener.java

    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JLabel;
    
    public class MyMouseListener extends MouseAdapter {
       private ColorGrid colorGrid;
    
       public MyMouseListener(ColorGrid colorGrid) {
          this.colorGrid = colorGrid;
       }
    
       @Override
       public void mousePressed(MouseEvent e) {
          if (e.getButton() == MouseEvent.BUTTON1) {
             colorGrid.labelPressed((JLabel)e.getSource());
          } else if (e.getButton() == MouseEvent.BUTTON3) {
             MyColor[][] myColors = colorGrid.getMyColors();
             for (int row = 0; row < myColors.length; row++) {
                for (int col = 0; col < myColors[row].length; col++) {
                   System.out.print(myColors[row][col] + " ");
                }
                System.out.println();
             }
             System.out.println();
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

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.