Swing is to **** hard!!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Swing is to **** hard!!

Hybrid View

  1. #1
    Join Date
    May 2005
    Posts
    115

    Swing is to **** hard!!

    I have an application which has 4 steps 2 perform. The list of steps are in the BorderLayout.WEST(). After each step has completed, that particular step name will turn grey and the next step will turn black color.

    BorderLayout.CENTER() contains a JPanel which holds the component that prompts for user input for the corresponding step.

    My previous (limited) Swinging experiences is to always do everything inside the constructor. But apparantly it doesn't work here. How should i be coding this?

    The following is the code i've done so far
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class BfsGUI extends JFrame{
    	public static final int WIDTH=600;
    	public static final int HEIGHT=400;
    
    	private static JPanel welcomePanel=new JPanel();
    
    	private static JPanel displayPanel=new JPanel(new GridLayout(5,1));
    
    	private static JLabel operationTitle=new JLabel();
    
    	private static JPanel operationPanel=new JPanel();
    	//List of operations in operation panel
    	static JLabel sTable=new JLabel("Select distance table");
    	static JLabel sDest=new JLabel("Select destination");
    	static JLabel sStartCity=new JLabel("Select start city");
    	static JLabel sNextCity=new JLabel("Select next city");
    
    	public static JLabel statusLabel=new JLabel("Status");
    
    	private DistanceTable dt;
    	private BfsTSPSolver solver;
    
    	public BfsGUI(){
    		super("TSP Solver Application");
    		setSize(WIDTH,HEIGHT);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLayout(new BorderLayout());
    
    		//Sets welcome panel
    		welcomePanel.setBorder(BorderFactory.createRaisedBevelBorder());
    		welcomePanel.setBackground(Color.WHITE);
    
    		JLabel welcome=new JLabel("Welcome to BFS TSP Solver");
    		welcomePanel.add(welcome);
    		add(welcomePanel, BorderLayout.NORTH);
    
    		//Sets the layout for operation panel
    		operationPanel.setLayout(new BoxLayout(operationPanel, BoxLayout.Y_AXIS));
    		//Sets operation panel
    		operationPanel.setBorder(BorderFactory.createEtchedBorder());
    		operationPanel.setBackground(Color.WHITE);
    		sTable.setForeground(Color.GRAY);
    		sDest.setForeground(Color.GRAY);
    		sStartCity.setForeground(Color.GRAY);
    		sNextCity.setForeground(Color.GRAY);
    		operationPanel.add(sTable);
    		operationPanel.add(sDest);
    		operationPanel.add(sStartCity);
    		operationPanel.add(sNextCity);
    		add(operationPanel, BorderLayout.WEST);
    
    		//Sets display panel
    		displayPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
    		displayPanel.setBackground(Color.WHITE);
    		displayPanel.add(operationTitle);
    
    		add(displayPanel, BorderLayout.CENTER);
    		add(statusLabel, BorderLayout.SOUTH);
    	}
    
    	public static void run1(){
    			sTable.setForeground(Color.BLACK);
    			operationTitle.setText("Select distance table");
    		
    			final JRadioButton selectDefaultTable=new JRadioButton("Default distance table", true);
    			final JRadioButton selectUserTable=new JRadioButton("User distance table");
    			final JTextField inputFileName=new JTextField("fileName.txt", 10);
    		
    			//Groups radio buttons, so that one on the other will off
    			ButtonGroup group=new ButtonGroup();
    			group.add(selectDefaultTable);
    			group.add(selectUserTable);
    
    			//NEXT button
    			JPanel buttonPanel=new JPanel();
    			buttonPanel.setOpaque(false);
    			JButton nextButton=new JButton("NEXT");
    			buttonPanel.add(nextButton);
    
    			nextButton.addActionListener(new ActionListener(){
    				public void actionPerformed(ActionEvent e){
    					if(selectDefaultTable.isSelected()){
    						statusLabel.setText("Default distance table selected");
    					}else{
    						UserDT.readDT(inputFileName.getText());
    						//nextOperationCount=false;
    					}
    				}
    			});		
    			displayPanel.add(selectDefaultTable);
    			displayPanel.add(selectUserTable);
    			displayPanel.add(inputFileName);
    			displayPanel.add(buttonPanel);
    	}
    
    	public static void run2(){
    			sTable.setForeground(Color.GRAY);
    			sDest.setForeground(Color.BLACK);
    			operationTitle.setText("Select destination");
    
    			JLabel l1=new JLabel("Case 1");
    	}
    
    	public static void run3(){
    			sDest.setForeground(Color.GRAY);
    			sStartCity.setForeground(Color.BLACK);
    			operationTitle.setText("Select start city");
    	}
    
    	public static void run4(){
    			sStartCity.setForeground(Color.GRAY);
    			sNextCity.setForeground(Color.BLACK);
    			operationTitle.setText("Select next city");
    	}
    
    	public static void main(String[] args){
    		BfsGUI gui=new BfsGUI();
    		gui.setVisible(true);
    		run1();
    		run2();
    		run3();
    		run4();
    	}
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Must use getContenPane for JFrame

    Code:
    public BfsGUI() {
        super("TSP Solver Application");
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new BorderLayout());
    
        //Sets welcome panel
        welcomePanel.setBorder(BorderFactory.createRaisedBevelBorder());
        welcomePanel.setBackground(Color.WHITE);
    
        JLabel welcome = new JLabel("Welcome to BFS TSP Solver");
        welcomePanel.add(welcome);
        getContentPane().add(welcomePanel, BorderLayout.NORTH);
    
        //Sets the layout for operation panel
        operationPanel.setLayout(new BoxLayout(operationPanel, BoxLayout.Y_AXIS));
        //Sets operation panel
        operationPanel.setBorder(BorderFactory.createEtchedBorder());
        operationPanel.setBackground(Color.WHITE);
        sTable.setForeground(Color.GRAY);
        sDest.setForeground(Color.GRAY);
        sStartCity.setForeground(Color.GRAY);
        sNextCity.setForeground(Color.GRAY);
        operationPanel.add(sTable);
        operationPanel.add(sDest);
        operationPanel.add(sStartCity);
        operationPanel.add(sNextCity);
        getContentPane().add(operationPanel, BorderLayout.WEST);
    
        //Sets display panel
        displayPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
        displayPanel.setBackground(Color.WHITE);
        displayPanel.add(operationTitle);
    
        getContentPane().add(displayPanel, BorderLayout.CENTER);
        getContentPane().add(statusLabel, BorderLayout.SOUTH);
      }
    eschew obfuscation

  3. #3
    Join Date
    May 2005
    Posts
    115
    I've tried your code and its still the same, its doesnt work..

    How should i actually approach in creating such a GUI?

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I tried it too, and it worked. , if you have done other changes than
    the one I pointed out then you must show the code that fails, mkay ?
    eschew obfuscation

  5. #5
    Join Date
    May 2005
    Posts
    115
    oh its ok, i'm passed that already...

    Do u know how to display formatted numbers? Because i have system.out.printfs which i want to output to a textarea...

Similar Threads

  1. Swing is dead, long live SWT
    By Darren Bell in forum Talk to the Editors
    Replies: 2
    Last Post: 09-20-2002, 10:32 AM
  2. Replies: 3
    Last Post: 09-16-2002, 08:15 PM
  3. swing Components
    By Valarmathy in forum Java
    Replies: 0
    Last Post: 01-19-2002, 05:35 AM
  4. Using Swing with JSP
    By Shaun Botha in forum Java
    Replies: 5
    Last Post: 06-25-2001, 09:19 PM
  5. VisualCafe3Pro and Swing 1.1.1?
    By Fabio Luis De Paoli in forum Java
    Replies: 0
    Last Post: 08-17-2000, 01:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center