Whenever I Call my add corePanel or add electivePanel methods They simply replace the last button that was added instead off adding a new one along side in the 4 columns I have set in the Jpanel.
I am aware that I have named the variables Panel when they are really buttons but that is irrelevant.
I really need some help.
Thanks
EDIT:
NOW when I mouse over the buttons all the information (Jpanels) inside of them disappears?
package Assignment2.view;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import ams.model.Course;
public class CoursePanel extends JPanel
{
private JButton corePanel = new JButton();
private JButton electivePanel = new JButton();
private JLabel ccLabel = new JLabel("Code: ");
private JLabel titleLabel = new JLabel("Title: ");
private JLabel cpLabel = new JLabel("Credit Points: ");
private JLabel prLabel = new JLabel("Prereqs: ");
private Border blackline;
public CoursePanel()
{
setLayout(new GridLayout(0,4));
}
public void addcorePanel(Course createcourse)
{
// core panel properties
blackline = BorderFactory.createLineBorder(Color.black,10);
corePanel.setLayout(new GridLayout(4,1));
corePanel.setBackground(Color.GRAY);
corePanel.setBorder(blackline);
corePanel.add(ccLabel);
corePanel.add(titleLabel);
corePanel.add(cpLabel);
corePanel.add(prLabel);
corePanel.setAlignmentX(LEFT_ALIGNMENT);
corePanel.setAlignmentY(TOP_ALIGNMENT);
//Needed to rebuild the prerequisites for display.
String[] prereqs = null;
String result = null;
prereqs = createcourse.getPreReqs();
if (prereqs != null)
{
StringBuilder builder = new StringBuilder();
for(String s : prereqs)
{
builder.append(s);
builder.append("\n");
}
result = builder.toString();
}
ccLabel.setText("Code: " + createcourse.getCode());
titleLabel.setText("Title: " + createcourse.getTitle());
cpLabel.setText("Credit points: " + Integer.toString(createcourse.getCreditPoints()));
prLabel.setText("Prereqs: " + result);
add(corePanel);
}
public void addelectivePanel(Course createcourseE)
{
//elective panel properties
electivePanel.setLayout(new GridLayout(4,1));
electivePanel.setBackground(Color.GRAY);
electivePanel.add(ccLabel);
electivePanel.add(titleLabel);
electivePanel.add(cpLabel);
electivePanel.add(prLabel);
electivePanel.setAlignmentX(LEFT_ALIGNMENT);
electivePanel.setAlignmentY(TOP_ALIGNMENT);
//Needed to rebuild the prerequisites for display.
String[] prereqs = null;
String result = null;
prereqs = createcourseE.getPreReqs();
if (prereqs != null)
{
StringBuilder builder = new StringBuilder();
for(String s : prereqs)
{
builder.append(s);
builder.append("\n");
}
result = builder.toString();
}
ccLabel.setText("Code: " + createcourseE.getCode());
titleLabel.setText("Title: " + createcourseE.getTitle());
cpLabel.setText("Credit points: " + Integer.toString(createcourseE.getCreditPoints()));
prLabel.setText("Prereqs: " + result);
add(electivePanel);
}
}
This happens because your labels and buttons (
corePanel,electivePanel) are class variables, so when you call for exampleaddcorePanel, instead of creating a newJButtonyou’re manipulating the samecorePanelbutton. You should move your variables inside the method body, like this:JLabelsandJButtonscannot be shared so you must create new instances each time you call your methods.