0

I am trying to create a grid (5*5) size like GUI. I tried and made a very basic grid, which is working quite fine, but I am trying to change the background colour of each JPanel when user click and drop over it. But I am not aware of the GUI in Java yet. So wondering if someone could help me please.

This my code to the grid and matching the both files(Sentiment word analyzing)

    public static TwitterSystem getObject()
    {
        if (Object==null)
            Object = new TwitterSystem();
        return Object;
    }
    long startTime = System.currentTimeMillis();

    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;

    //read jason from file into String
            //create a lot of tweet objects

    //run the tweetSystem
    public void Run()
    {
        double r;

        for (int i = 0; i < 5; i++)
              for (int j = 0; j < 5; j++)
                DataGrid[i][j] = 0.0;

        // trying to load the wordlist and tweets
        try
        {
             WordList = new Sentiment_Analysis("E:\\JAVA\\src\\wordlist.txt");

             Tweet = new Tweet_Reader("E:\\JAVA\\tweets.json");

        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }

        tweets = Tweet.getTweets();


        // for each tweet, we getting the rating and working out where it is in the grid.
        for(Tweet t : tweets) {
            r = WordList.getRating(t);

            if((int)t.getCoordinate().getLatitude() == 24 && (int)t.getCoordinate().getLongitude() == 54 ) {
                DataGrid[2][2] += r;
            }
            if((int)t.getCoordinate().getLatitude() == 25 && (int)t.getCoordinate().getLongitude() == 54 ) {
                DataGrid[0][1] += r;
            }

        }

        // printing out the score for each square.
        for (int i = 0; i < 5; i ++)
            for (int j = 0; j < 5; j++)
                System.out.format("[%4d][%4d] = %.4f\n", i, j, DataGrid[i][j]);
        System.out.println("Finish calculating");
        System.out.println("STATS - TIME: Analysis took "


                + TimeUnit.SECONDS.convert(totalTime, TimeUnit.MILLISECONDS)
                + " seconds");
    }




}

Thank you in advance! i'm quite new to Programming

So far got the grid working but i want make the grid in GUI

HELP PLEASE!!!!

Darren Cook
  • 27,837
  • 13
  • 117
  • 217
  • Hate to be a pain, but there is no Swing/UI related elements here... – MadProgrammer Dec 05 '14 at 02:03
  • You might like to take a look at [How to Use GridLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html), [How to Write a Mouse Listener](http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html) and [Creating a GUI With JFC/Swing](http://docs.oracle.com/javase/tutorial/uiswing/) – MadProgrammer Dec 05 '14 at 02:04
  • @MadProgrammer that's where i'm lost, i have a basic grid. but it's not in a great structure. you think you can help me ?? – Arun Sri III Dec 05 '14 at 02:07
  • @MadProgrammer i know how to use a GUI grid code but it's not quite jamming with my codes!! – Arun Sri III Dec 05 '14 at 02:08
  • Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This will result in less confusion and better responses – MadProgrammer Dec 05 '14 at 02:11
  • @MadProgrammer Thanks for the response. I have minimized code to the right point! – Arun Sri III Dec 05 '14 at 02:19

1 Answers1

1

Starting from this complete example, I've added an ActionListener to each ButtonPanel in the grid. The listener updates the enclosing panel's background color. Note that each button uses its own instance of the same anonymous class. Comment out the timer's start() invocation to see the effect better. As an exercise, try changing class ButtonPanel to a factory method such as createButtonPanel(), as shown here for createGridPanel().

private static class ButtonPanel extends JPanel {

    public ButtonPanel(int i) {
        this.setBackground(new Color(rnd.nextInt()));
        JButton b = new JButton("Button " + String.valueOf(i));
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton b = (JButton) e.getSource();
                ButtonPanel.this.setBackground(new Color(rnd.nextInt()));
            }
        });
        this.add(b);
    }
}

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • thanks for the reply... this kind a grid what i'm looking for, but it's not displaying my sentiment word results?? – Arun Sri III Dec 05 '14 at 03:56
  • Now you have a [complete example](http://stackoverflow.com/help/mcve) with which to experiment; use it to update you question with a [complete example](http://stackoverflow.com/help/mcve); note that no one else has your word list, so you'll need to incorporate a some static data for completeness. – trashgod Dec 05 '14 at 04:04
  • thank you for the example code it was useful, but i still coudn't make it get the results. sorry for asking silly question. My JAVA Knowladge is very minimum. also i have attached a link to my file. see if this can help fix my problem. https://www.dropbox.com/sh/i6cb28dz6phwku4/AAAtdAaQmWjNI9pQEApirz6ka?dl=0 – Arun Sri III Dec 05 '14 at 04:12
  • Correcting an entire project is well beyond the scope of SO. Please edit your question to include a [minimal, complete example](http://stackoverflow.com/help/mcve) that focuses on a particular problem. – trashgod Dec 05 '14 at 13:05
  • my particular problem is that I not able to construct a GUI grid – Arun Sri III Dec 05 '14 at 13:27
  • Well, a `GridLayout` of components works pretty well, but you should also look at `JList` and `JTable`. – trashgod Dec 05 '14 at 20:43