I am trying to get a JPanel to display a series of Buttons.On each of these buttons is one of the values associated to my Sudoku Board Values. I have created my board added a menu and now i am trying to display the selectable options below the menu but before the board..in the same Jframe. I was hoping i could put all my buttons onto the JPanel and put that panel onto the Frame. It will display the JPanel but none of the buttons. At one time i got the panels to display but none of them were sized and there were to many. The question i have to be more specific is the code i am using correct for displaying my series of buttons on my JPanel, that is placed on in a Frame containing my Sudoku Board, which is also a series of buttons.
Is this final toolbar and buttons to much for this single JFrame, is that why it is not working? Anyways here is just the code for my toolbar which is my JPanel.
class ToolBar extends JPanel {
// instance initializer (constructor equivalent)
public ToolBar() {
super();
this.setLayout(myLayout);
myLayout.setAlignment(FlowLayout.TRAILING);
Button[] panelButton = new Button[size];
//Rectangle rec = new Rectangle(330,45,BUTTON, BUTTON);
//setBounds(rec);
setPreferredSize(new Dimension(330, 45));
for(int i = 0; i < size; i++) {
Rectangle r = new Rectangle(12, 12, 22, 22);
center = new ImageIcon(view.drawSymbol(i));
panelButton[i]= new Button();
panelButton[i].setIcon(center);
panelButton[i].setOpaque(true);
panelButton[i].setBorder(BorderFactory.createLineBorder(Color.black));
panelButton[i].setBounds(r);
this.add(panelButton[i]);
this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
this.setVisible(true);
}
}
};
I used setBounds
on the toolbar to make it visible and also set the background to red just for testing it, replaced AWT buttons with Swing JButtons and I also set some text on the buttons. I commented out something on my testing code in order to compile and but put them back below:
class ToolBar extends JPanel {
// instance initializer (constructor equivalent)
public ToolBar() {
super();
this.setLayout(myLayout);
myLayout.setAlignment(FlowLayout.TRAILING);
JButton[] panelButton = new JButton[5];
this.setBackground(Color.red);
this.setBounds(0, 0, 200, 200);
//Rectangle rec = new Rectangle(330,45,BUTTON, BUTTON);
//setBounds(rec);
setPreferredSize(new Dimension(330, 45));
for (int i = 0; i < 5; i++) {
Rectangle r = new Rectangle(22, 22);
panelButton[i] = new JButton();
panelButton[i].setText(" ");
panelButton[i].setIcon(new ImageIcon(view.drawSymbol(i)));
panelButton[i].setOpaque(true);
panelButton[i].setBorder(BorderFactory.createLineBorder(Color.black));
panelButton[i].setBounds(r);
this.add(panelButton[i]);
this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
this.setVisible(true);
}
}
};
I am also posting the whole testing code below:
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
/*
* To change this template, choose Tools | Templates and open the template in
* the editor.
*/
/**
*
* @author hahahaha
*/
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
this.add(new ToolBar());
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
class ToolBar extends JPanel {
// instance initializer (constructor equivalent)
public ToolBar() {
super();
//this.setLayout(myLayout);
//myLayout.setAlignment(FlowLayout.TRAILING);
JButton[] panelButton = new JButton[5];
this.setBackground(Color.red);
this.setBounds(0, 0, 200, 200);
//Rectangle rec = new Rectangle(330,45,BUTTON, BUTTON);
//setBounds(rec);
setPreferredSize(new Dimension(330, 45));
for (int i = 0; i < 5; i++) {
Rectangle r = new Rectangle(22, 22);
//center = new ImageIcon(view.drawSymbol(i));
panelButton[i] = new JButton();
panelButton[i].setText(" ");
//panelButton[i].setIcon(center);
panelButton[i].setOpaque(true);
panelButton[i].setBorder(BorderFactory.createLineBorder(Color.black));
panelButton[i].setBounds(r);
this.add(panelButton[i]);
this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
this.setVisible(true);
}
}
};
}
Look for the line this.add(new ToolBar());
where I instantiate and add your toolbar to my JFrame.
A piece of advice:
Avoid AWT components as much as possible