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); } }
Register and join over 40,000 other developers!
Recent Topics
-
The Game You Are Waiting For?
WendellHarper - Dec 06 2020 01:21 PM
-
Quora and Reddit Backlinks
WendellHarper - Dec 06 2020 01:14 PM
-
Delete account
pindo - Jul 23 2020 01:33 AM
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
-
Learn algorithms and programming concepts
johnnylo - Apr 23 2019 07:49 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

10 replies to this topic
#1
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.
#2
Posted 28 April 2012 - 10:35 PM
You're not setting a preferredSize for your scrollpane.
You might also want to remove this line:
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[]....)
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() );
#3
Posted 28 April 2012 - 11:25 PM
you are not required to set a preffered size to display a JScrollPane.
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
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:
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.
#5
Posted 30 April 2012 - 07:53 PM
Bump...
#6
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.
#8
Posted 01 May 2012 - 09:04 PM
Wow, this is probably my first programming problem that people have no idea what's causing....yet.
#9
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.
Otherwise your list would've been on top of the gui, and not below Tax.
#10
Posted 02 May 2012 - 08:28 AM
Wow, I tried out a bunch of different combinations and missed that one. I feel so stupid 
Thanks wimDC

Thanks wimDC
#11
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.
Also tagged with one or more of these keywords: JScrollPane, GUI
Language Forums →
Java →
How can I provide my program to wait for getting an input from user?Started by Mervealk, 17 Mar 2017 ![]() |
|
![]() |
||
Language Forums →
Java →
how do I print my console output into a GUI TextboxStarted by AdamDewhurst, 17 Feb 2017 ![]() |
|
![]() |
||
General Forums →
General Programming →
Coding a server rack GUIStarted by jamessmith, 23 Mar 2015 ![]() |
|
![]() |
||
![]() Making a C# program wait without blocking the GUIStarted by SickHeadPro, 01 Mar 2015 ![]() |
|
![]() |
||
General Forums →
General Programming →
GUI questionsStarted by Error, 16 Jun 2014 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download