Originally posted by beamo
I didn't actually ask a gridbag question come to think of it, sorry.
lol.. its okay.. its not like theres a strict requirement to post gridbag only..
actually, i'd love to see this thread rival the Crystal Reports one in vb forum.. but that is about 50 pages long..
Thanks for the great tutorial. Several days ago you commented that I use a GridbagLayout for my User Interface and I have had a mixed bag of success.
I am not certain how or what I did but I managed to get most of it to work and was wondering if you might be able to answer one or two questions.
I created the following screen:
The screen consists of JPanels, Jlabels, JComboBox, JTextFields and a Gridbaglayout. Some of the JComboBoxs read data from files and it works great. My problem is with the layout manager.
I create a JPanel that holds three other JPanels. The first JPanel works great, but the second and third do not have the correct size/alignment. I think I might be messing up with the gridbaglayout's rows/columns or anchor.
Also, I an a little confused why a JFrame does not work and I am not certain (eventhough it works) how to the first JPane is added to the frame.
I am attaching a zipped file. Any help/suggestions would be appreciated. Thanks
I have a GUI with two components but when I add weights nothing happens. How can I fix this?
Code:
//3.29.04
//draws a triangular fractal based on four points chosen by user
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Fractal extends JPanel implements MouseListener{
private int x, y;
private int index=0;
private int[] vertX = new int[4];
private int[] vertY = new int[4];
private static Random rn = new Random();
private Dimension panelDimension = new Dimension(600, 600);
private JLabel instructions = new JLabel("",0);
private Dimension labelDimension = new Dimension(600, 15);
GridBagConstraints c = new GridBagConstraints();
String directions = new String("<html><font face='Copperplate Gothic Bold' color=#990000 size=4>"+
"Click on four non-linear points to see your triangular fractal"),
directions2 = new String("<html><font face='Copperplate Gothic Bold' color=#000033 size=4>"+
"To repeat the process click on four more non-linear points");
public Fractal(Container contents){
contents.setLayout(new GridBagLayout());
setPreferredSize(panelDimension);
setBackground(Color.yellow);
addMouseListener(this);
c.gridx=0;
c.gridy=10;
contents.add(this, c);
instructions.setText(directions);
c.gridx=0;
c.gridy=0;
instructions.setOpaque(true);
instructions.setBackground(Color.yellow);
instructions.setPreferredSize(labelDimension);
contents.add(instructions, c);
}
public void mousePressed (MouseEvent event){
//sets four vertices
if(index < 4){
vertX[index] = event.getPoint().x;
vertY[index] = event.getPoint().y;
index++;
repaint();
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
//paints first four vertices
if(index < 4){
for(int i = 0; i < index; i++){
g.fillOval(vertX[i],vertY[i], 1, 1);
}
}
//paints fractal
else{
x = vertX[3];
y = vertY[3];
for(int i = 0; i < 10000; i++){
index = rn.nextInt(3);
x = (vertX[index] + x)/2;
y = (vertY[index] + y)/2;
g.fillOval(x, y, 1, 1);
}
//clears vertices
for(int i = 0; i < vertX.length; i++){
x = vertX[i] = 0;
y = vertY[i] = 0;
}
//resets index and instruction label
index = 0;
instructions.setText(directions2);
}
}
public void mouseClicked(MouseEvent event){}
public void mouseReleased(MouseEvent event){}
public void mouseEntered(MouseEvent event){}
public void mouseExited(MouseEvent event){}
private static void createGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Sepinski's gasket");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Fractal mine = new Fractal(frame.getContentPane());
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI();
}
});
}
}
Originally posted by Ronnie
I create a JPanel that holds three other JPanels. The first JPanel works great, but the second and third do not have the correct size/alignment. I think I might be messing up with the gridbaglayout's rows/columns or anchor.
Also, I an a little confused why a JFrame does not work and I am not certain (eventhough it works) how to the first JPane is added to the frame.
Sorry for the delay.. here's some guidance:
The main idea of a gridbag layout, is that you dont need JPanels. The factor to consider here is that JPanels have their own layout too.. so a JFrame that has a JPanel that has 3 JPanels will need 5 gridbag layouts.. one for each of the 4 panels and one for the frame..
Can you take the image that you have drawn and cut it up/edit it to be how you would like? i can then give guidance as to what to assign and where (but it is certain that the panel structure needs revising)
Originally posted by beamo
I have a GUI with two components but when I add weights nothing happens. How can I fix this?
Weights only affect how a component's preferred size changes if there is space over and above the minimum size...
Say you had 2 components of min size 10 each, and the window is 100 wide thanks to a larger component somewhere else.. if their weights were 2 and 6 respectively, then there are 100 pixels, minus the 2*10 minimum, leaving 80 pixels left over, they would claim pixels on a 2/8 6/8 ratio each, so one would grow (its preferred size would alter) by 20 pixels, giving it a total of 30 (10 min plus 20 grown) and the other would become 70 (10 min plus 60 grown) to give 30+70 = 100 pixels
they ahve then filled the space.
your gui doesnt use weights much thanks to every component being specified in size so that all sizes match and when the frame is packed, the minimum sizes all tally up so that no row or column is left with an excess of free space. the only way to give this free space is to resize the window
oh, and remember, it is the grid cell that the component is in, that grows... whether the component then paints like it fills its cell depends on the FILL. if the component fills NONE, then it sticks to wherever it is anchored, as the cell size changes..
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.