Hugo Alves
Hugo Alves

Reputation: 1555

User-Interface question

i'm kinda new to manipulating UI in java, so pls forgive me for this question i cant find the answer anywhere.

Description | i'm trying to do a card game and i have an engine class that manipulates all the cards and plays made, and i want the engine to tell the UI to update the score, card position or card image.

this is an example of how i start the UI, problem here is i don't have any instance to manipulate the JLabels using the instance methods i made inside Board class, and i can't create a instance outside of the EventQueue because i would be violating "never manipulate/create UI outside UI thread"

public class Engine {
public StartUp(){
         EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException e) {
                } catch (InstantiationException e) {
                } catch (IllegalAccessException e) {
                } catch (UnsupportedLookAndFeelException e) {
                }

                new Board().setVisible(true);
            }
        });
    }
}

the Board class extends JPanel and adds some JLabels to the ui in the constructor , and has several methods to change text and imgs.

My question is how to properly call these methods(the ones i created to alter text and img), i'm also open o any other suggestions on how to approach this problem.

*edited:

here's simple example of my Board Class:

public class Board extends JFrame{
    public JLabel img1;

    public Board(){
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(400, 265);


        JPanel body = new JPanel(new GridBagLayout());
        getContentPane().add(body);

        img1 = new JLabel();
        body.add(img1);

    }

    public void setImg1(String s){
        img1.setIcon(new ImageIcon(s));
    }
}

i want to be able from Engine to access setImg1(String s) method that is inside the Board to be able to change the current image during runtime

sorry if i expressed my question wrong

final edit:

solved it merging the Engine into the Board.

ty to everyone that helped and for your time

Upvotes: 0

Views: 130

Answers (2)

Mohammed Aslam
Mohammed Aslam

Reputation: 1015

public class MainFrame extends JFrame {

    public MainFrame() {
        super("Demo frame");
        // set layout
        // add any components 
        add(new Board()); // adding your board component class
        frameOptions();
    }

    private void frameOptions() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack(); // or setSize()
        setVisible(true);
    }

    public static void main(String[] a) {
        JFrame.setDefaultLookAndFeelDecorated(true);

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                try {
                    UIManager.setLookAndFeel(
                        UIManager.getSystemLookAndFeelClassName());
                    new MainFrame();
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
            }
        });
    }
}

Upvotes: 2

Dilum Ranatunga
Dilum Ranatunga

Reputation: 13374

The basic idiom for getting a GUI up is:

SwingUtilities.invokeLater(new Runnable() {
  JFrame frame = new JFrame("My Window Title");
  frame.setSize(...);
  frame.add(new Board()); // BorderLayout.CENTER by default
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setLocationRelativeTo(null); // center on main screen
  frame.setVisible(true);
});

Upvotes: 1

Related Questions