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",
            Double.toString(percent_array[i]));
    }

    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);

    chart.setBackgroundPaint(Color.white);
    chart.getTitle().setPaint(Color.black); 
    CategoryPlot p = chart.getCategoryPlot(); 
    p.setRangeGridlinePaint(Color.blue); 

    //cp.setMaximumDrawHeight(5);
    //cp.setMaximumDrawWidth(5);
    //cp.setZoomOutFactor(.1);
    JPanel graph = new JPanel();
    graph.add(cp);
    middle.add(graph, BorderLayout.CENTER);
}   

 Answers

12

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.

    @Override
    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.

image

Tuesday, June 1, 2021
 
jcubic
answered 6 Months ago
68

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;
    }

    //NOT REACHED
    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
 
Pwner
answered 4 Months ago
29

Take a look at JLayeredPanes. Here is a tutorial.

edit:

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?

Edit2:

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");
    frame.setSize(300,300);
    addStuffToFrame();
    SwingUtilities.invokeLater(new Runnable(){
        public void run() {
            frame.setVisible(true);
        }
    });

}       

private void addStuffToFrame() {    
    Panel awtPanel = new Panel();
    awtPanel.setBackground(Color.blue);
    //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
    awtPanel.setSize(200,300);
    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. 
    //frame.add(awtPanel);

    Panel awtPanel2 = new Panel();
    awtPanel2.setBackground(Color.red);
    awtPanel2.setSize(300,200);
    frame.getLayeredPane().add(awtPanel2,JLayeredPane.PALETTE_LAYER);
}   
Sunday, August 15, 2021
 
Chase
answered 4 Months ago
15

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

Monday, August 30, 2021
 
anon_swe
answered 3 Months ago
87

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
 
Xaisoft
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 :  
Share