Having trouble with Jpanel design view forms :(
Apologies for my first post being a help one , but I'm in a slight bit of a pickle and my head hurts
Very basic problem but I can't get my head around forms , anyway Here I have some code for displaying and tracking movement of the mouse over the form .
Code :
package Test;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
* @author WorldsTallestMidget
*/
public class Main {
private JPanel slotsPanel = new JPanel(new GridLayout(8, 8));
public Main() {
CreateUI();
}
public void CreateUI() {
JFrame window = new JFrame("Test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(50, 50, 400, 400);
JPanel panel = new JPanel(new BorderLayout());
panel.add(CreateDisplay(), BorderLayout.CENTER);
window.add(panel, BorderLayout.CENTER);
window.setVisible(true);
}
private JPanel CreateDisplay() {
JPanel mainPanel = new JPanel(new BorderLayout());
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
final JPanel slots = new JPanel();
slots.setBorder(BorderFactory.createLineBorder(Color.black));
slots.setBackground(new Color(255, 222, 188));
slots.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent me) {
}
public void mousePressed(MouseEvent me) {
}
public void mouseReleased(MouseEvent me) {
}
public void mouseEntered(MouseEvent me) {
slots.setBackground(new Color(255, 255, 188));
}
public void mouseExited(MouseEvent me) {
slots.setBackground(new Color(255, 222, 188));
}
});
slotsPanel.add(slots);
}
}
mainPanel.add(slotsPanel, BorderLayout.CENTER);
return mainPanel;
}
public static void main(String[] args) {
new Main();
}
}
Now I think I have it working to spec , its tracking the movements correctly . But the problem is I don't know how to import it into the design view of a jframe . Its seems easier in class form and we only have to do as such, but being from a vb background and a complete beginner I like the robustness and easier snap to controls especially . Can anyone give me some hints on how to get the jpanels declared and such as when using the design view jpanel forms they can't be editted / are protected :(
Thanks
Re: Having trouble with Jpanel design view forms :(
Quote:
jpanel forms they can't be editted
JPanels are not editable. You use a text component of some kind for editable input.
Re: Having trouble with Jpanel design view forms :(
apologies if my terminology is terrible :)
If I place a jpanel on the form using design view , The variable Declarations are protected . Is there anyway around this ?
specifically for
Code :
private JPanel slotsPanel = new JPanel(new GridLayout(8, 8));
OK Tried to add post-declaration code of */private JPanel jPanel1 = new JPanel(new GridLayout(8, 8)); but no luck . Compiles fine but no display
Essentially all code seems to be the same , but It will not draw the board when it comes to the design view form .
But If I build the exact same one using a class file it works fine :(
Heres the code for the actual NewjPanel . pretty much the same . Anyone know why it won't display yet will in a class file ?
Code :
public class NewJPanel extends javax.swing.JPanel {
/** Creates new form NewJPanel */
public NewJPanel() {
initComponents();
CreateUI();
}
public void CreateUI() {
JFrame window = new JFrame("Test");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(50, 50, 400, 400);
JPanel panel = new JPanel(new BorderLayout());
panel.add(CreateDisplay(), BorderLayout.CENTER);
window.add(panel, BorderLayout.CENTER);
window.setVisible(true);
}
private JPanel CreateDisplay() {
JPanel mainPanel = new JPanel(new BorderLayout());
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
final JPanel slots = new JPanel();
slots.setBorder(BorderFactory.createLineBorder(Color.black));
slots.setBackground(new Color(255, 222, 188));
slots.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent me) {
}
public void mousePressed(MouseEvent me) {
}
public void mouseReleased(MouseEvent me) {
}
public void mouseEntered(MouseEvent me) {
slots.setBackground(new Color(255, 255, 188));
}
public void mouseExited(MouseEvent me) {
slots.setBackground(new Color(255, 222, 188));
}
});
jPanel1.add(slots);
}
}
mainPanel.add(jPanel1, BorderLayout.CENTER);
return mainPanel;
}
/** This method is called from within the constructor to
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jPanel1.setBackground(new java.awt.Color(255, 255, 255));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 459, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 430, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(269, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(63, Short.MAX_VALUE))
);
}// </editor-fold>
public static void main(String[] args) {
new NewJPanel();
// TODO code application logic here
}
// private JPanel slotsPanel = new JPanel(new GridLayout(8, 8));
// Variables declaration - do not modify
/*
private javax.swing.JPanel jPanel1;
*/private JPanel jPanel1 = new JPanel(new GridLayout(8, 8));
// End of variables declaration
}
Re: Having trouble with Jpanel design view forms :(
Quote:
The variable Declarations are protected
Can you explain what you mean here? Why the capital D?
How are the declarations protected? Who protects them?
Quote:
Tried to add post-declaration code of */private JPanel jPanel1 = new JPanel(new GridLayout(8, 8)); but no luck
If you got errors, please copy and paste the full text here.
Are you having a problem with your IDE?
Quote:
If I build the exact same one using a class file
This is another strange one. You don't work with class files. They are created by the compiler.
Re: Having trouble with Jpanel design view forms :(
Quote:
Originally Posted by
Norm
Can you explain what you mean here? Why the capital D?
How are the declarations protected? Who protects them?
If you got errors, please copy and paste the full text here.
Are you having a problem with your IDE?
This is another strange one. You don't work with class files. They are created by the compiler.
Apologies and thanks for your time. I originally coded it for instance in the main.java file . The first one compiles fine and displays , tracks movement and such . In the picture provided its the top program and is the main.java file
The one beneath it however is the one using the jform ( newjpanel.java ). Same code but doesn't seem to be working at all
Everything seems to be compiling fine , but I don't understand why it won't display in the newjpanel form
http://img97.imageshack.us/img97/854/netbean.jpg shows the forms / program
Re: Having trouble with Jpanel design view forms :(
You need to talk to your IDE and tell it that you're in charge. It has a mind of its own and is changing how you want to define jPanel1. See what I assume is generated code that defines the layout for jPanel1. It is called after you have defined jPanel1.
Re: Having trouble with Jpanel design view forms :(
This is what confuses me , i can only see it being defined once . If i try to redeclare it anywhere I will get "Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - jPanel1 is already defined in Test.NewJPanel"
I use the post declarations to change that to "private JPanel jPanel1 = new JPanel(new GridLayout(8, 8));" . Yet it doesn't seem to work and its the only time its ever defined
I can create one from scratch in the code if it doesn't coincide with another created element from the design view . But that seems redundant as I would like to be able to place it using the form designer
Re: Having trouble with Jpanel design view forms :(
You define the layout for jPanel1 when you first initialize it:
private JPanel jPanel1 = new JPanel(new GridLayout(8, 8));
The generated code changes it:
jPanel1.setLayout(jPanel1Layout);
You'll have to work it out with your IDE. I don't use one and don't know how to use them.
Re: Having trouble with Jpanel design view forms :(
ok thanks
will have a look