How to solve JAVA menubar, layout and panel problem ?
- by Berkay
i'm not a java guy however i start implementing some security tools with java, in these days i'm creating a gui for my security tools :
Here is the how my Gui looks:
menuBar = new JMenuBar(); // construct menu bar
// hash functions
fileMenu = new JMenu("HASH FUNCTIONS"); // define file menu
fileMenu.setMnemonic('H'); // shortcut
hashes = new JMenuItem("Md5&Sha1"); // define file menu options
hashes.setMnemonic('M');
hashes.addActionListener(this);
fileMenu.add(hashes);
menuBar.add(fileMenu); // add file menu to menu bar
// symmetric encryption
asMenu = new JMenu("SYMMETRIC ENCRYPTION"); // define format menu
asMenu.setMnemonic('S'); // shortcut
desItem = new JMenuItem("DES"); // define format menu options
desItem.setMnemonic('D');
desItem.addActionListener(this);
asMenu.add(desItem);
...
...
menuBar.add(helpMenu); // add help menu to menu bar
setJMenuBar(menuBar); // put menu bar on application
textColor = Color.RED;
when from the Menu desitem is selected, desvar is just for not showing the panel multiple times, it calls Panels ()
else if(e.getSource() == desItem && desvar ==1 )
{
// make other panels unvisible.
if(hashvar!=1) MyPanel.setVisible(false);//hash functions
if(rsavar!=1) MyPanel3.setVisible(false);//rsa function
if (dhvar!=1) MyPanel4.setVisible(false);//dh diffie hellman
hashvar=1;
rsavar=1;
dhvar=1;
++desvar;
desPanel=true;
Panels();
}
and in Panels() Method:
if (hashPanel){
MyPanel.add("West",radioSHA1);
MyPanel.add("West",radioMD5);
MyPanel.add("Center", inputField);
MyPanel.add("East",SubmitButton);
MyPanel.add("South",resultHash);
add(MyPanel);
validate();
hashPanel=false;
}
i have many panels for example :
-hash functions=mypanel1 , des=mypanel2, rsa functions= mypanel3 , dh= mypanel4
However in may other panals such as rsa function: i have to use some layout properties of the java:in this panel i selected to use gridbaglayout
if (dhPanel){
System.out.println("rsa panel burda misin");
MyPanel3.add(pqLabel);
MyPanel3.add(pLabel);
MyPanel3.add(pTextArea);
MyPanel3.add(qLabel);
...
...
add(MyPanel3);
generate_pqButton.addActionListener(this);
calculate_nButton.addActionListener(this);
...
...
GridBagLayout layout = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(layout);
// x, y, w, h, wx, wy
gbc.fill = GridBagConstraints.NONE;
add (bit_length_label, gbc, 0, 0, 1, 1, 0, 10);
add (p, gbc, 0, 1, 1, 1, 0, 10);
add (g, gbc, 0, 2, 1, 1, 0, 10);
add (a, gbc, 1, 3, 1, 1, 100, 10);
add (x, gbc, 0, 4, 1, 1, 0, 10);
add (gx, gbc, 0, 5, 1, 1, 0, 10);
add (gxy, gbc, 0, 6, 1, 1, 0, 10);
add (b, gbc, 1, 7, 1, 1, 100, 10);
add (y, gbc, 0, 8, 1, 1, 0, 10);
add (gy, gbc, 0, 9, 1, 1, 0, 10);
add (gyx, gbc, 0, 10, 1, 1, 0, 10);
add (sk, gbc, 1, 11, 1, 1, 100, 10);
add (key, gbc, 0, 12, 1, 1, 0, 10);
add (status, gbc, 1, 13, 1, 1, 100, 10);
add (dhstart, gbc, 1, 14, 1, 1, 100, 10);
add (bit_length_value, gbc, 1, 0, 1, 1, 100, 10);
add (p_value, gbc, 1, 1, 1, 1, 100, 10);
add (g_value, gbc, 1, 2, 1, 1, 100, 10);
add (x_value, gbc, 1, 4, 1, 1, 100, 10);
add (gx_value, gbc, 1, 5, 1, 1, 100, 10);
add (gxy_value, gbc, 1, 6, 1, 1, 100, 10);
add (y_value, gbc, 1, 8, 1, 1, 100, 10);
add (gy_value, gbc, 1, 9, 1, 1, 100, 10);
add (gyx_value, gbc, 1, 10, 1, 1, 100, 10);
validate();
repaint();
rsaPanel=false;
}
Everthing seems okey however when i swith from one menu to another sometimes components are seen or apper in wrong places or mixed.
where i'm doing wrong?