I can't see what is wrong with the GUI. Its too small.
Can you make a small simple program that compiles, executes and shows the problem?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I can't see what is wrong with the GUI. Its too small.
Can you make a small simple program that compiles, executes and shows the problem?
If you don't understand my answer, don't ignore it, ask a question.
huh? if you click on the image it enlarge. but the chessboard is crappy normally you see the squares but when want to add piece images it is...well
Screen Shot 2013-08-12 at 5.36.59 PM.jpg Screen Shot 2013-08-15 at 12.18.15 AM.jpg
System.out.println(" dream in code ");
I have no idea what the code does to change the GUI like shown in the post.
One thing that can be seen is that the color of the squares changes so that the squares in a column are the same color. Does the code set the color of the squares more than once? The first time makes the diagonals and the second time makes the columns the same color.
You'll have to make a small, simple program that compiles, executes and shows the problem.
If you don't understand my answer, don't ignore it, ask a question.
Yes, the code two JLabels at the same place. With a if-statement i check the squareDoes the code set the color of the squares more than once?
System.out.println(" dream in code ");
Why do it a second time? If the first time does it correctly, why do it a second time?
If you don't understand my answer, don't ignore it, ask a question.
It works!!!!!! but still need to implement exceptions. Is that difficult? dont close thread yet.
System.out.println(" dream in code ");
See the tutorial:
Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
If you don't understand my answer, don't ignore it, ask a question.
Any idea how i can add the ActionListener in the controller package but the buttons are in the view package. different classes
controller.Controller
package controller; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.*; import javax.swing.*; import view.ControlPanel; import view.InfoPanel; import view.ChessBoardView; public class Controller extends JFrame implements ActionListener { private InfoPanel infopanel; private ControlPanel controlpanel; private ChessBoardView chessboardview; public Controller() { super("Chess Manager"); makeFrame(); makeMenuBar(); this.pack(); } private void makeFrame(){ setSize(1050, 1050); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable( false ); Container contentpane = super.getContentPane(); contentpane.setLayout(new BorderLayout()); contentpane.setBackground(new Color(204,229,255)); controlpanel = new ControlPanel(this); contentpane.add(controlpanel, BorderLayout.EAST); infopanel = new InfoPanel(this); contentpane.add(infopanel, BorderLayout.SOUTH); chessboardview = new ChessBoardView(this); contentpane.add(chessboardview, BorderLayout.WEST); } private void makeMenuBar(){ JMenuBar menubar = new JMenuBar(); super.setJMenuBar(menubar); JMenu fileMenu = new JMenu("File"); menubar.add(fileMenu); JMenu editMenu = new JMenu("Edit"); menubar.add(editMenu); JMenu optionMenu = new JMenu("Options"); menubar.add(optionMenu); JMenu extraMenu = new JMenu("Extra"); menubar.add(extraMenu); } public void setupProgram(){ } public void actionPerformed(ActionEvent e){ } }
view.ControlPanel
package view; import java.awt.*; import java.awt.event.*; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JSlider; import controller.Controller; import model.ChessBoard; import model.ChessPiece; import model.Fen; import model.Square; import view.ChessBoardView; public class ControlPanel extends JPanel { private JPanel buttonpanel; private JButton btnEmptyBoard, btnInitialPosition; private JSlider sldVolume; private Fen fen; private ChessBoardView chessboardview; private InfoPanel infopanel; public ControlPanel (Controller controller) { setLayout(new GridBagLayout()); setBackground(new Color(204,229,255)); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(10,0,0,30); btnEmptyBoard = new JButton("Empty Board"); gbc.gridy = 0; btnEmptyBoard .setPreferredSize(new Dimension(180, 25)); //btnEmptyBoard .addActionListener(controller); btnEmptyBoard .setRequestFocusEnabled(false); add(btnEmptyBoard, gbc); btnInitialPosition = new JButton("Initial Position"); gbc.gridy =1; btnInitialPosition .setPreferredSize(new Dimension(180, 25)); //btnInitialPosition .addActionListener(controller); btnInitialPosition .setRequestFocusEnabled(false); add(btnInitialPosition, gbc); } }
all ready made a actionlistener and parameter the controller class. but how can i add the actionevent e in the controller class and add it to the buttons in the controlpanel class
--- Update ---
has i something to do with e.getSource()
System.out.println(" dream in code ");
To use the contents of a class, you need a reference to an instance of the class.
The following code in class3 this would add a listener in class2 to a component in class1
class1Ref.component1.AddActionListener(class2Ref);
The getSource() method returns a reference to the component that caused the event. For example the button that was clicked.
Why is that statement commented out? It adds the controller class as the listener for the button.//btnInitialPosition .addActionListener(controller);
It looks like you are writing code BEFORE you have designed what the program is supposed to do. What do you want the listener to do?
If you don't understand my answer, don't ignore it, ask a question.
Okay,
component is that a method or a button? can you give an example with the code i posted above?
Now i use Class Controller implements ActionListener
The class controlpanel use the controller class as parameter. i can use @override but i dont know how to use that:p
System.out.println(" dream in code ");
It's a reference to a class object that extends the Component class.component is that a method or a button?
an example with the code i posted above?btnInitialPosition .addActionListener(controller);
What is the listener supposed to do? Is it in the correct class to do what it is supposed to do?
If you don't understand my answer, don't ignore it, ask a question.
the code you posted btnInitialPosition .addActionListener(controller)..i was that far but now i want to add the ActionEvent in the controller class because that class implements ActionListener Class but i dont know how to do that
it has to read the FEN notation scan it and place the pieces on the boardWhat is the listener supposed to do? Is it in the correct class to do what it is supposed to do?
System.out.println(" dream in code ");
The ActionEvent object is passed by the JVM to the listener method when it is called.i want to add the ActionEvent in the controller classYou don't create it or pass it. It is passed to the above method.public void actionPerformed(ActionEvent e){
Where is the FEN string? Can the listener get to it?it has to read the FEN notation
If you don't understand my answer, don't ignore it, ask a question.
yes the listener can get to it.
Question: I understand the principalI want to use that to so,class1Ref.component1.AddActionListener(class2Ref);
class controlpanel hasclass controller hasbtnInitialPosition .addActionListener(controller);
public class Controller implements ActionListener{ public void ActionPerformed(ActionEvent e){ } }
but my controlpanel class has two buttons with actionlisteners
the controller class has one @override actionperformed. How can i do separate things for each button in the controlpanel? while i have one actionperformed in the controller classtnInitialPosition .addActionListener(controller);
btnEmptyBoard.addActionListener(controller);
System.out.println(" dream in code ");
Have a separate listener for each button. Each listener will do the task to be done when its button is pressed: gather the data and call methods as needed.How can i do separate things for each button in the controlpanel?
If you don't understand my answer, don't ignore it, ask a question.
i know but i want to implement in the controller class what needs to done. But i only have one actionperformerd. How will the button know which task to perform
System.out.println(" dream in code ");
Several ways:
1)Have one listener for each button. Put the listeners in the class with the buttons.
Have the listener call methods in the controller class to do the task.
2)Use the JComponent class's client properties to store data in the button object that the listener can extract to know what button was pressed.
3)Extend the JButton class with your own class. Have your class contain data that the listener can use to know what button was pressed.
If you don't understand my answer, don't ignore it, ask a question.
how can i build that? call methods?Have the listener call methods in the controller class to do the task.
System.out.println(" dream in code ");
You call a method in another class by having a reference to that class:call methods?
refToOtherClass.theMethodToCall(theArgsHere...);
If you don't understand my answer, don't ignore it, ask a question.
tnInitialPosition .addActionListener(controller); i have this. now i want in the controller class the actionperformed but i have two buttons how can i separate them so the
btnEmptyBoard.addActionListener(controller); also has a actionperformed in the controller class but different operations
System.out.println(" dream in code ");
See post#142 for several ways.
If you don't understand my answer, don't ignore it, ask a question.
sorry but i dont understand that post.
i have to buttons with addActionListeners in my package view.ControlPanel;
My controller class implements ActionListener and has one @override public void ActionPerformed(ActionEvent e){tnInitialPosition .addActionListener(controller);
btnEmptyBoard. addActionListener(controller);
}
but i have two buttons in my ControlPanel class. How can i separate the operations for the buttons in the Controller class.
can you code an example?
System.out.println(" dream in code ");
The three solutions I gave say how to do it:
1) Put the listeners in the class with the buttons. Have the listeners call the controller class's methods
Don't have the listener inside the controller class.
or 2) & 3) Put data in the object that is passed in the ActionEvent object to the listener that can be used to know which button was pressed.
If you don't understand my answer, don't ignore it, ask a question.
the listener gets as parameter controller class, in the controller class is the actionperformed. I dont think i understand it.controller class's methods
--- Update ---
how can i give a string parameter to a method, like i fill in the FEN notation, press initial position and the FEN parameter goes into the variable fen in the fen class
--- Update ---
this is in my infopanel class its for reading the FEN notation and pass it to the FEN class to scan the String
@Override public void actionPerformed(ActionEvent e) { if(e.getSource() == fenbar){ String input = fenbar.getText(); fen.scanner(input); fen.scanParser(); chessboardview = new ChessBoardView(); chessboardview.addSquaresAndPiecesToContentPane(); } } }
how can perform this action when i push the button initial position in the ControlPanel Class
btnInitialPosition = new JButton("Initial Position"); gbc.gridy =1; btnInitialPosition .setPreferredSize(new Dimension(180, 25)); btnInitialPosition .addActionListener(this); btnInitialPosition .setRequestFocusEnabled(false); add(btnInitialPosition, gbc); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource() == btnInitialPosition){ }
--- Update ---
can someone help me i cant get it to work
System.out.println(" dream in code ");
Is this what is supposed to happen?
The user enters a FEN string and presses a button
The button's listener gets the FEN String and calls a method to parse the string and position the pieces on the board.
If you don't understand my answer, don't ignore it, ask a question.