• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

basic graphics (user interfaces) HELP!

 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have devised the following class. It has no errors but for some reason only an empty box appears when I run it. Why? I am providing the code below- if anyone has any ideas please let me know.
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ButtonGroup;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class clusterFormationOptions {
public static void main(String[] args) {
optionsFrame frame = new optionsFrame();
frame.setTitle("Cluster Formation Options");
frame.show();
}
}
class optionsFrame extends JFrame{
public optionsFrame(){
final int DEFAULT_FRAME_WIDTH = 300;
final int DEFAULT_FRAME_HEIGHT = 300;
setSize(DEFAULT_FRAME_WIDTH,DEFAULT_FRAME_HEIGHT);

optionsListener listener = new optionsListener();

automatedButton = new JRadioButton("Automated");
automatedButton.setSelected(true);
automatedButton.addActionListener(listener);

agglomerativeButton = new JRadioButton("Agglomerative");
agglomerativeButton.setSelected(true);
agglomerativeButton.addActionListener(listener);

divisiveButton = new JRadioButton("Divisive");
divisiveButton.setSelected(true);
divisiveButton.addActionListener(listener);

ButtonGroup clusteringGroup = new ButtonGroup();
clusteringGroup.add(automatedButton);
clusteringGroup.add(agglomerativeButton);
clusteringGroup.add(divisiveButton);

cutOffCombo = new JComboBox();
cutOffCombo.addItem("1");
cutOffCombo.addItem("2");
cutOffCombo.addItem("3");
cutOffCombo.addItem("4");
cutOffCombo.addItem("5");
cutOffCombo.addItem("6");
cutOffCombo.addItem("7");
cutOffCombo.addItem("8");
cutOffCombo.addItem("9");
cutOffCombo.addItem("10");
cutOffCombo.addItem("11");
cutOffCombo.addItem("12");
cutOffCombo.addItem("13");
cutOffCombo.addItem("14");
cutOffCombo.addItem("15");
cutOffCombo.addItem("16");
cutOffCombo.addItem("17");
cutOffCombo.addItem("18");
cutOffCombo.addItem("19");
cutOffCombo.addItem("20");


JPanel clusteringGroupPanel = new JPanel();
clusteringGroupPanel.add(automatedButton);
clusteringGroupPanel.add(agglomerativeButton);
clusteringGroupPanel.add(divisiveButton);
clusteringGroupPanel.setBorder (new TitledBorder(new EtchedBorder(), "Select" +
"method for clustering"));

JPanel cutOffComboPanel=new JPanel();
cutOffComboPanel.add(cutOffCombo);
cutOffComboPanel.setBorder (new TitledBorder(new EtchedBorder(), "Select" +
"cut-off point"));

JPanel theBox = new JPanel();
theBox.add(clusteringGroupPanel);
theBox.add(cutOffComboPanel);

getChoice();
}
public void getChoice(){
String divisive1 = null;
String agglomerative1 = null;
String automated1 = null;
//gets all the users choices to be used for the data mining
String cutOff = (String)cutOffCombo.getSelectedItem();

//gets the type of clustering requested by the user
final String AUTOMATED = automated1;
final String AGGLOMERATIVE = agglomerative1;
final String DIVISIVE = divisive1;

String clusteringMethod = "null";
if(automatedButton.isSelected())
clusteringMethod = AUTOMATED;
if (agglomerativeButton.isSelected())
clusteringMethod = AGGLOMERATIVE;
if (divisiveButton.isSelected())
clusteringMethod = DIVISIVE;
}

private JRadioButton automatedButton;
private JRadioButton agglomerativeButton;
private JRadioButton divisiveButton;
private JComboBox cutOffCombo;

private class optionsListener implements ActionListener{
public void actionPerformed (ActionEvent event){
getChoice();
}
}
/*private class WindowCloser extends WindowAdapter{
public void windowClosing(WindowEvent event){
System.exit(0);

}}*/
}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The window is empty because after you assemble the set of nested JPanels, you don't use them -- i.e., you never add() them to the JFrame itself; i.e., you need to call getContentPane().add(theBox) as the last line of the optionsFrame constructor.
Let me give you a couple other tips while I'm here, if you don't mind. First, there's a very strongly established convention that Java class names start with a capital letter, to distinguish them from variable and method names. This improves code readability quite a lot, and it's a very good habit to get into.
Second, lots of typing is bad, and you should avoid it when possible. It tires you out, it tires out the reader of your code, and worst of all, the more you type, the more mistakes you might make. So rather than the 21 lines it takes to construct cutOffCombo, why not write

If the labels weren't numbers you could still put all the labels in an array and use a loop. Note that this code is not only easier to read, but its smaller, and so will load and execute faster.
There are other places where you've got great tracts of repeated code, too: the automatedButton, agglomerativeButton, divisiveButton trio are identical, so their creation and setup could be pulled out into a small method which you call three times, supplying the label as an argument.
Finally, getChoice() has a lot of problems. Note that null and "null" are not the same thing. Note also that final variables can be assigned to only once, and in your code, they're all being assigned the value null. The code which assigns a value to the variable cluseringMethod always assigns null to that variable, because all of the possibilities are null.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic