This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JRadiobuttons combined with JCombobox do not work

 
George Kosmadakis
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have in a JPanel on the top left a Combobox with 3 items and in the top right two JRadioButtons. The radiobuttons switch between two different charts, Pie chart and Bar Chart. The 3 items on combobox are for total expenses, September expenses and October expenses. What i want to achieve is switching between the two charts (which i already do) and at the same time while being in one of the two charts to be able to change the chart when the user selects every item from the combobox.

The problem is when i switch to the Bar Chart and select September the execution goes inside the createDatasetPie method and the monthCombo is Total Expenses falsely and it shows me the total expenses ofenter image description here Pie chart instead of the Bar Chart.


 
George Kosmadakis
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
George Kosmadakis wrote:I have in a JPanel on the top left a Combobox with 3 items and in the top right two JRadioButtons. The radiobuttons switch between two different charts, Pie chart and Bar Chart. The 3 items on combobox are for total expenses, September expenses and October expenses. What i want to achieve is switching between the two charts (which i already do) and at the same time while being in one of the two charts to be able to change the chart when the user selects every item from the combobox.

The problem is when i switch to the Bar Chart and select September the execution goes inside the createDatasetPie method and the monthCombo is Total Expenses falsely and it shows me the total expenses ofenter image description here Pie chart instead of the Bar Chart.


PieChart.png
[Thumbnail for PieChart.png]
BarChart.png
[Thumbnail for BarChart.png]
 
Fred Kleinschmidt
Bartender
Posts: 468
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your action listener added to variable "month", the if-else construct beginning at line 58 is incomplete. if monthCombo is not "TotalExpenses" it then checks if pieChart.isSelected() and monthCombo is "September". So if barChart is selected, this is false and it goes to the next else, checking if monthCombo.equals("October") So if monthCombo is "September", this too fails and the if-block ends without doing anything.
 
George Kosmadakis
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:In your action listener added to variable "month", the if-else construct beginning at line 58 is incomplete. if monthCombo is not "TotalExpenses" it then checks if pieChart.isSelected() and monthCombo is "September". So if barChart is selected, this is false and it goes to the next else, checking if monthCombo.equals("October") So if monthCombo is "September", this too fails and the if-block ends without doing anything.


yes you're right. The action listener should be like this:


i tried that but again it doesn't work...
 
Campbell Ritchie
Marshal
Posts: 52621
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't simply say, “It doesn't work”. Tell us what actually happens.
 
George Kosmadakis
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Please don't simply say, “It doesn't work”. Tell us what actually happens.


OK, As you can guess i have another two methods for the bar chart buildBarChart and createDatasetBar. The action listener which is inside the createDatasetBar has 3 if that check like this: if(barChart.isSelected() && monthCombo.equals("Total expenses/September/October)
When i press the button to open the charts by default it opens the pie chart because it is called after the bar chart in my constructor so when i switch to the bar chart it shows correctly the total expenses but when i select the September or October the program execution goes to createDatasetPie and then to that actionlistener instead of going to the createDatasetBar actionlistener.
 
Fred Kleinschmidt
Bartender
Posts: 468
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should really only have a single combo box and a single radio (containing "pie and "Bar" buttons) Then your top container contains the combobos, a title, and the radio buttons, and below that have a container where you can switch between the Pie and bar charts. The combobox action listener should then check the radio status to see which one is active and act on the appropriate one, and the radio button action listeners should check the combo box for the correct data to display in the pie or bar.

The combo box and buttons should not be created in buildPieChart() or buildBarChart(), but should be in the class that holds the charts. The BarChart should have no knowledge that the PieChart exists; that way it will be much easier to modify things if you were to add other views in the future.
 
George Kosmadakis
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:You should really only have a single combo box and a single radio (containing "pie and "Bar" buttons) Then your top container contains the combobos, a title, and the radio buttons, and below that have a container where you can switch between the Pie and bar charts. The combobox action listener should then check the radio status to see which one is active and act on the appropriate one, and the radio button action listeners should check the combo box for the correct data to display in the pie or bar.

The combo box and buttons should not be created in buildPieChart() or buildBarChart(), but should be in the class that holds the charts. The BarChart should have no knowledge that the PieChart exists; that way it will be much easier to modify things if you were to add other views in the future.


Thank you for your answer. That all sounds reasonable. I will implement it and post again.
 
George Kosmadakis
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my constructor:

The actionPermored method that listens to JRadioButtons clicks

These two methods create the data and the chart

And here is where i have the JCombobox listener. I understand that i should have one listener for the combobox in order to return something in the end. The problem is that the method is of PieDataset type and returns result only while i should return dataset as well for the bar chart which is CategoryDataset type. Also when i debug it and i select September from the the combo the monthCombo is Total Expenses, i dont understand why. At this state it works only the the first option Total Expenses from the combobox and i am able to switch between the charts, the september and October are not displayed.
 
Fred Kleinschmidt
Bartender
Posts: 468
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The action listener for the month combobox should be added in your layoutTop method, not in your createDatasetPie method. its actioonPerfocmed method should do something like this:



Similarly, the action listener for the radio buttons should check which month item is selected and act accordingly.
 
George Kosmadakis
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My constructor:

My layoutTop method:


When i first press the button of the chart it opens correctly the pie chart and i can view all 3 items, Total Expenses, September, October. When i switch to Bar chart it switches correctly to Total Expenses Bar chart. But when i select September it displays the previous state of pie chart. For example, if previous switching to bar i had seen Total Expenses of pie chart it displays that instead of September Bar chart. Same if i select October Bar chart it shows the previous state of pie chart. Whatever is the last option i had on pie chart (Total expenses, September, October) that gets displayed when i select the September or October in the Bar chart.
Also, when i debugged it when i select September or October from Bar chart the monthSelection is always Total Expenses so the program goes here :


I don't know i tried to setSelectedItem(0) after declaring the Combobox but nothing changed. Something is wrong with the monthSelection and i don't understand why the previous state of pie chart gets displayed.
 
George Kosmadakis
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK i managed to fix that, so now all work both JRadioButtons and the items from the JList. i changed the JComboBox with a JList. If you want to try my code i will put it here, the class MoneyControlChart.java that displays the two charts, pie chart and bar chart:

And from another class have a JButton like chartButton and do:

You only need to populate with data, i use the storedAmounts which is a Map <String, Double> and a private static LinkedHashSet<String> arrayOfdescSep for a descriptions and private static ArrayList<Double> arrayOfamountSep for the amounts. i attach an image with the charts.
Thanks for all the answers and i hope the code would be useful.
Charts.png
[Thumbnail for Charts.png]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!