Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Jscrollpane Not Showing Up?

JScrollPane GUI

  • Please log in to reply
10 replies to this topic

#1 An Alien

An Alien

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 323 posts
  • Programming Language:Java
  • Learning:C, Java, PHP, Python, JavaScript, Lisp, Transact-SQL, Others

Posted 28 April 2012 - 06:56 PM

See my Gui() method please. Inside there I create and add the JScrollPane but it doesn't show up.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class StateTaxCalcGui{
	  
		private static File file = new File("C:\\Users\\Fahad Ahmed\\Dropbox\\Workspace\\SalesTaxCalc\\src\	axrates.txt");
	  
		private final static JLabel AMOUNT = new JLabel("Amount: ");
		private final static JLabel TAX = new JLabel("Tax: ");
	  
		private static JTextField amountField;
		private static JTextField taxField;
	  
		private static String [] states = new String[51];
		private static double [] taxRates = new double[51];
	  
		private static JList list;	  
	  
		private static JPanel panel;
	  
		//DefaultListModel listModel = new DefaultListModel();
	  
		//createList(states, listModel);
  
//	private static JList createList(String[] array, DefaultListModel mod) {
//	  
//		JList data;
//	  
//		for(int x = 0; x < array.length; x++){
//		  
//		 mod.addElement(array[x]);
//		
//		}
//	  
//		data = new JList(mod);
//		data.setSelectedIndex(0);
//		data.setVisibleRowCount(5);
//		return data;
//	  
//	}

	private static void readFile(File txt, String[] state, double[] taxRate) throws FileNotFoundException{
	  
		File file = txt;
		String[] states = state;
		double[] taxrates = taxRate;
	  
		Scanner in = new Scanner(file);
	  
			for(int x = 0; x < 51; x++){
				states[x] = in.next();
				taxrates[x] = in.nextDouble();		  
			}
		  
	  
	}
  
	private static void Gui(String[] state, double[] rates){
	 panel = new JPanel();
	 panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
	
	 list = new JList<String>(state);
	 list.setLayoutOrientation(JList.VERTICAL);
	 list.setVisibleRowCount(10);
	 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	 JScrollPane scroll = new JScrollPane();
	 //JScrollPane scroll = new JScrollPane(list);
	 scroll.setViewportView(list);
	
	 panel.add(scroll);
	
	
	 panel.add(AMOUNT);
	 panel.add(amountField = new JTextField(10));
	
	 panel.add(TAX);
	 panel.add(taxField = new JTextField(10));
	 taxField.setEditable(false);
	
	 panel.add(list);
	
	 list.addListSelectionListener(
	   new ListSelectionListener(){
		public void valueChanged(ListSelectionEvent event){
		
		 if(!amountField.getText().equals(""))
		 taxField.setText("" + Double.parseDouble(amountField.getText()) * taxRates[list.getSelectedIndex()]);
		 else
		  JOptionPane.showMessageDialog(panel, "Please enter a value before selecting state.");
		}
	   }
	 );
	
	  
	}
	private static JPanel getJPanel() {
	
  return panel;
}
public static void main(String[] abc) throws FileNotFoundException{
	
	 readFile(file, states, taxRates);
	 //test, remove later on : System.out.println(states[0] + states[49] + taxRates[0] + taxRates[49]);
	
	 Gui(states, taxRates);
	
	 JFrame frame = new JFrame("Tax Calc GUI - By: Fahad A.");
	 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	 frame.setContentPane(getJPanel());
	 frame.setSize(300, 400);
	 frame.setVisible(true);
	
	
	
	}
}

  • 0

#2 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 28 April 2012 - 10:35 PM

You're not setting a preferredSize for your scrollpane.
You might also want to remove this line:
panel.add(list);
... as you're already adding it when you add the scrollPane.

Any reason you're using BoxLayout?

From the looks of it, you'll probably be able to get away with using a simpler BorderLayout. (Much simpler to layout components)
Also, you might want to place some of your components inside of separate JPanels.

Separating your gui components will make your gui look better and actually make it easier to work with.

Another thing, all of methods and variables are static.
To get rid of this static design, use something like this in the main(String[]....)
StateTaxCalcGui gui = new StateTaxCalcGui();
gui.readFile(...);
JFrame frame ...
frame.setContentPane( gui.getJPanel() );

  • 0

#3 UniBrain

UniBrain

    CC Regular

  • Validating
  • PipPipPip
  • 48 posts
  • Programming Language:C, Java, C++, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Pascal, Assembly, Scheme, Prolog, Others
  • Learning:Others

Posted 28 April 2012 - 11:25 PM

you are not required to set a preffered size to display a JScrollPane.
  • 0

I wish nothing for myself; I wish solely that this consumptive existence is realigned... Such is perhaps attainable with science an technology... If cosmological constants yielded an energy transference bounded universe, by modus ponens, it perhaps follows that such constants are affixable... This universe is but a program whose underlying construct is starkly outdated....


#4 An Alien

An Alien

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 323 posts
  • Programming Language:Java
  • Learning:C, Java, PHP, Python, JavaScript, Lisp, Transact-SQL, Others

Posted 29 April 2012 - 12:45 AM

the "panel.add(list);" line was actually just a quick experiment and now commented out.

Also, I'm not so good at picking which layouts to use. Also, thanks for the advice of making more JPanels, I'll keep that in mind next time.

I was going to make an instance of the class first, but I decided to keep everything static so everything could stay in one class/file which I need to upload online.

I added:
	 scroll.setPreferredSize(new Dimension(400, 110));
But the problem is still the same.
  • 0

#5 An Alien

An Alien

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 323 posts
  • Programming Language:Java
  • Learning:C, Java, PHP, Python, JavaScript, Lisp, Transact-SQL, Others

Posted 30 April 2012 - 07:53 PM

Bump...
  • 0

#6 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 01 May 2012 - 07:31 AM

It works fine for me with dummy data. Are you sure the file reading works? do some printlns over there.
  • 0

#7 An Alien

An Alien

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 323 posts
  • Programming Language:Java
  • Learning:C, Java, PHP, Python, JavaScript, Lisp, Transact-SQL, Others

Posted 01 May 2012 - 07:57 AM

Yeah, all of the states show up in the JList. Everything works fine except for the scrollbar.

Attached Thumbnails

  • Untitled.png

  • 0

#8 An Alien

An Alien

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 323 posts
  • Programming Language:Java
  • Learning:C, Java, PHP, Python, JavaScript, Lisp, Transact-SQL, Others

Posted 01 May 2012 - 09:04 PM

Wow, this is probably my first programming problem that people have no idea what's causing....yet.
  • 0

#9 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 01 May 2012 - 10:38 PM

Judging by your screenshot you didn't comment panel.add(list); and left panel.add(scroll); commented. As suggested above.
Otherwise your list would've been on top of the gui, and not below Tax.
  • 0

#10 An Alien

An Alien

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 323 posts
  • Programming Language:Java
  • Learning:C, Java, PHP, Python, JavaScript, Lisp, Transact-SQL, Others

Posted 02 May 2012 - 08:28 AM

Wow, I tried out a bunch of different combinations and missed that one. I feel so stupid :blushing:

Thanks wimDC
  • 0

#11 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 03 May 2012 - 09:59 AM

...
You might also want to remove this line:

panel.add(list);
... as you're already adding it when you add the scrollPane.


  • 0





Also tagged with one or more of these keywords: JScrollPane, GUI

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download