I'm trying to get multiple jfreecharts displaying within a single window. Apparently this is not possible with the included ChartFrame so I attempted to add multiple copies of the same chart to a JFrame which didn't work. Any ideas?
JFrame frame = new JFrame("Chart");
frame.getContentPane().add(new ChartPanel(chart1));
frame.getContentPane().add(new ChartPanel(chart2));
frame.pack();
frame.setVisible(true);
With this code, I only get one chart in the JFrame.
EDIT:
I added another data set and chart but it still only displays one of them.
Cause of your problem is layout of frame.getContentPane().
Default layout at the JFrame content pane - BorderLayout. Read more about BorderLayout here.
This operation
frame.getContentPane().add(new ChartPanel(chart));
equals
frame.getContentPane().add(new ChartPanel(chart), BorderLayout.CENTER);
and add ChartPanel to the CENTER area of the content pane. Second ChartPanel you add to CENTER area too. Then you add two component to the same area the last added one hides all previous added. Thus second ChartPanel hides first one.
You need change layout for frame.getContentPane().
You may use the same chart to two ChartPanel.
Try to change you code to
JFrame frame = new JFrame("Chart");
frame.getContentPane().add(new ChartPanel(chart), BorderLayout.WEST);
frame.getContentPane().add(new ChartPanel(chart), BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
It is not code for production. It just example to show two ChartPanel on the frame.
You need to have different chart instances. If you use the same reference, the last one that is added to a ChartPanel will be displayed.
Related
I'm wondering if it is possible to resize Jpanels, I've been searching for a while but haven't found what I'm looking for.
This is my code:
JFrame frame = new JFrame("My Frame");
JPanel panel = new JPanel(new GridLayout(1,2)); // split the panel in 1 rows and 2 cols
panel.add(new PixelPanel());
panel.add(new MyPanel());
frame.getContentPane().add(panel);
frame.setVisible(true);
frame.setSize(1920, 1080);
This creates a layout with the 2 columns next to eachother with a border exactly in the middle seperating the 2 Jpanels.A lot of software nowadays offers the user an option to resize the width of the 2 panels. So by grabbing the border and pulling it to the left or right 1 panel will grow larger and the other one will slink down. Is there any build in feature for this? And if not, would it be better to build this behavior onto Jpanel, or would it be better to make custom panel system?
Thank you in advance!
(if the behavior im after isnt clear, I could make a gif explaining it)
The JSplitPane is what you’re looking for.
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new PixelPanel(), new MyPanel());
How to add the button to the left of the name of graphics in that area marked by red color?
chartPanel.setDomainZoomable(false);
chartPanel.setRangeZoomable(false);
chartPanel.setPopupMenu(null);
frame = new JFrame(Title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(chartPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
To facilitate use on a headless server, a JFreeChart is not a Swing JComponent. The typical Swing approach is to enclose the chart in a ChartPanel and add the panel to a suitable layout. Add controls to to an adjacent panel, as shown here and here using BorderLayout.SOUTH.
Hey everyone I've been using this as my guide and the base for my code that I've been working on:
Java Source
What I want to do, is add a shared button across all of the panes. I dont want to declare a unique button for each one, but one that is shared. My first thought was to change the frame to boxlayout and just toss a button in after it adds the pane to the frame:
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BoxLayout(frame, BoxLayout.PAGE_AXIS));
//Add content to the window.
//frame.add(new GUI(), BorderLayout.CENTER);
frame.add(new GUI());
//setup Find button
//findButton.setSize(110,55);
findButton.setText("Find");
findButton.setVisible(true);
//add button to frame
frame.add(findButton);
However, I get a runtime error: BoxLayout can't be shared. So now I wind up here. While I look into why I'm getting this error, can someone let me know if this is the right approach?
Suggestions:
Consider placing the JButton in a JPanel that is below or above the JTabbedPane so that it is always visible, and you will only need one button.
Or if it must be in a component held in the tabs, then each will need its own unique JButton, but they can share the same Action, which is what I recommend you do: Create an inner private class that extends AbstractAction, create an instance of this inner class, pass it into each JButton via either the JButton's constructor or its setAction(...) method.
Your BoxLayout problem is completely unrelated to your original question and should not even be part of this discussion. Yes, a BoxLayout must be used in one container, and that same container should be passed into the BoxLayout. Likely you're adding it to the JFrame, but in reality this adds it to the JFrame's contentPane, and so for this to work, you must pass frame.getContentPane() into the BoxLayout's first constructor parameter:
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));
This is one reason I don't like adding components or setting layouts directly on the top level window since it is nothing but misleading syntactic sugar.
I much prefer:
JPanel contentPane = (JPanel) frame.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
I have parent panel and child panel. child parent exists on parent panel. Both are Jpanels of different size. Now i want to show a ChartPanel on the child panel. I have tried various ways of displaying it but unsucessfull. Please suggest some way of adding chartpanel to Jpanel.
sorry i couldnt paste the code. I have also tried various ways suggested in stackoverflow Q&A, but in vain.
Because ChartPanel establishes a preferred size and BoxLayout relies on the preferred size, let a newPanel extend Box using the desired orientation and add() both child and chartPanel to newPanel.
I think your problem has nothing to do with JFreeChart. Probably the code below helps you to start:
JFrame frame = new JFrame();
JPanel parentPanel = new JPanel();
parentPanel.setBorder(BorderFactory.createTitledBorder("parent panel"));
JPanel childPanel = new JPanel();
childPanel.setBorder(BorderFactory.createTitledBorder("child panel"));
// Add a button to the child panel
childPanel.add(new JButton("button"));
// In the instruction below you have to create and add your ChartPanel
childPanel.add(yourChartPanel);
parentPanel.add(childPanel);
frame.add(parentPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
I'm a novice Java programmer, and I'm trying to work out the behaviour of BorderLayout in a particular situation.
Say you have:
JFrame frame = new JFrame();
frame.add(new JLabel("Test"));
The defualt layout manager for JFrame is BorderLayout. According to the Java tutorial for BorderLayout, a position must always be specified, e.g. by:
frame.add(new JLabel("Test"),BorderLayout.CENTER);
So I'm confused by the fact that it is possible to add a component without specifying a position. If I modify the above code to be:
JFrame frame = new JFrame();
frame.add(new JLabel("Test"));
frame.add(new JLabel("Test 2"));
frame.add(new JLabel("Test 3",BorderLayout.NORTH);
I get Test 2 being displayed in the center left of the screen, and Test 3 being displayed in the top left of the screen.
Am I right in my understanding that, if no position is specified, BorderLayout will just default to BorderLayout.CENTER and, if so, can anyone show me where this is documented? I'm sure it must be documented somewhere, but I can't find it anywhere!
Thanks
http://download.oracle.com/javase/6/docs/api/java/awt/BorderLayout.html
"As a convenience, BorderLayout interprets the absence of a string specification the same as the constant CENTER"