Asked  6 Months ago    Answers:  5   Viewed   24 times

I've added a JFreeChart to a JPanel (using a BorderLayout), and it's huge. Is there something I can do to make it smaller?

public void generateChart()
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    //set the values of the chart
    for(int i=0; i<8; i++)
        dataset.setValue(income_array[i], "Income",

    JFreeChart chart = ChartFactory.createBarChart(
        "Required Annual Income for a Variety of Interest Rates",
        "Percent", "Income", dataset, PlotOrientation.VERTICAL,
        false,true, false);
    ChartPanel cp = new ChartPanel(chart);

    CategoryPlot p = chart.getCategoryPlot(); 

    JPanel graph = new JPanel();
    middle.add(graph, BorderLayout.CENTER);



When you create your ChartPanel, you have several options that affect the result:

  1. Accept the DEFAULT_WIDTH and DEFAULT_HEIGHT: 680 x 420.

  2. Specify the preferred width and height in the constructor.

  3. Invoke setPreferredSize() explicitly if appropriate.

  4. Override getPreferredSize() to calculate the size dynamically.

    public Dimension getPreferredSize() {
        // given some values of w & h
        return new Dimension(w, h);
  5. Choose the layout of the container to which the ChartPanel will be added. Note that the default layout of JPanel is FlowLayout, while that of JFrame is BorderLayout. As a concrete example, ThermometerDemo uses both preferred values in the constructor and a GridLayout for the container to allow dynamic resizing.


Tuesday, June 1, 2021
answered 6 Months ago

You can change the width by an appropriate delegate method

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    switch(component) {
        case 0: return 22;
        case 1: return 44;
        case 2: return 88;
        default: return 22;

    return 22;

As for a custom text size, you can use the delegate to return custom views with whatever sized text you want:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
        UILabel *retval = (id)view;
        if (!retval) {
            retval= [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)] autorelease];

        retval.text = @"Demo";
        retval.font = [UIFont systemFontOfSize:22];
        return retval;

Of course you will need modify these to have appropriate values for your app, but it should get you where you need to go.

Sunday, August 1, 2021
answered 4 Months ago

Take a look at JLayeredPanes. Here is a tutorial.


If panelA is an AWT component, you will be hard pressed to get panelB to overlap. From Sun's article entitled Mixing Heavy and Light Components:

Do not mix lightweight (Swing) and heavyweight (AWT) components within a container where the lightweight component is expected to overlap the heavyweight one.

However, if you are looking to have panelA fill the Frame completely, why not add panelB as a component of panelA?


If you can make panelB a heavyweight component, then you can use the JLayeredPane.

Here is a quick mockup that shows how:

public static void main(String[] args){
    new GUITest();

public GUITest() {
    frame = new JFrame("test");
    SwingUtilities.invokeLater(new Runnable(){
        public void run() {


private void addStuffToFrame() {    
    Panel awtPanel = new Panel();
    //here you can fool around with the pane:
    //first, you can see how the layered pane works by switching the 
    //DEFUALT_LAYER and PALLETTE_LAYER back and forth between the two panels
    //and re-compiling to see the results
    frame.getLayeredPane().add(awtPanel, JLayeredPane.DEFAULT_LAYER);
    //next you comment out the above two lines and 
    //uncomment the following line. this will give you the desired effect of
    //awtPanel filling in the entire frame, even on a resize. 

    Panel awtPanel2 = new Panel();
Sunday, August 15, 2021
answered 4 Months ago

You could use a List<int> during your method logic and then return myIntList.ToArray();

Monday, August 30, 2021
answered 3 Months ago

I had posted this question again since this one was voted to be closed. Over there I have marked the right answer. Please check the link below for the answer

Use the Bounds of the Screen minus Insets

Wednesday, September 1, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :