Witam,
Mam problem z JPanel/JTabbedPane. Walczę, walczę i nie widzę rezultatu.
Cel: Zamierzenie jest takie, że gdy kliknę x2 w obiekt z listy, na wcześniej przygotowanej do tego zakładce ma pokazywać się wykres (zrobiony na podstawie bazy danych), oraz tabela w JTable (również na podstawie DB) adekwatna do obiektu wybranego z listy. Tabela mi się bez problemu zmienia. Ale z wykresem jest gorzej.
Jeśli robię tak jak nizej, czyli na poczatku inicjuje te JPane jako wykres z klasy CrosshairDemo2, to wykres sie pojawia w zakladce, ale przy wybraniu obiektu z listy, nie zmienia się na inny:*
public class OknoGlowne extends javax.swing.JFrame {
static javax.swing.JList jList1;
static javax.swing.JPanel jPanel1;
static javax.swing.JTabbedPane jTabbedPane1;
static javax.swing.JTable jTable1;
public OknoGlowne() throws SQLException {
initComponents();
}
public void initComponents() throws SQLException {
jList1 = new javax.swing.JList();
jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel1 = CrosshairDemo2.createDemoPanel(); // <------------------------------------------------
jTable1 = new javax.swing.JTable();
(...)
}
}
class ActionJList extends MouseAdapter {
public static Object item;
JList jList1;
public ActionJList(JList l){
jList1 = l;
}
public void mouseClicked (MouseEvent e) {
if(e.getClickCount() == 2){
int index = jList1.locationToIndex(e.getPoint());
ListModel dlm = jList1.getModel();
item = dlm.getElementAt(index);;
jList1.ensureIndexIsVisible(index);
try {
lista.ajdi();
lista.tabelka();
}
catch (SQLException e1) {
e1.printStackTrace();
}
try {
OknoGlowne.jPanel1 = CrosshairDemo2.createDemoPanel(); // <----------------------------------
} catch (SQLException e1) {
e1.printStackTrace();
}
OknoGlowne.jTable1.setModel(new javax.swing.table.DefaultTableModel(lista.tab2, lista.naglowki) {Class[] types = {java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Object.class};public Class getColumnClass(int columnIndex) {return types [columnIndex];}});
OknoGlowne.jTabbedPane1.setSelectedIndex(1);
}
}
}
**Natomiast gdy, zainicjuje go normalnie, jako pusty panel a w klasie ActionJList dodam inna metodę, to pojawia się wszystko pięknie, tyle że zamiast w tej zakładce, to w nowym oknie, przy każdorazowym wyborze. **
public class OknoGlowne extends javax.swing.JFrame {
static javax.swing.JList jList1;
static javax.swing.JPanel jPanel1;
static javax.swing.JTabbedPane jTabbedPane1;
static javax.swing.JTable jTable1;
public OknoGlowne() throws SQLException {
initComponents();
}
public void initComponents() throws SQLException {
jList1 = new javax.swing.JList();
jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel1 = CrosshairDemo2.createDemoPanel(); // <------------------------------------------------
jTable1 = new javax.swing.JTable();
(...)
}
}
class ActionJList extends MouseAdapter {
public static Object item;
JList jList1;
public ActionJList(JList l){
jList1 = l;
}
public void mouseClicked (MouseEvent e) {
if(e.getClickCount() == 2){
int index = jList1.locationToIndex(e.getPoint());
ListModel dlm = jList1.getModel();
item = dlm.getElementAt(index);;
jList1.ensureIndexIsVisible(index);
try {
lista.ajdi();
lista.tabelka();
}
catch (SQLException e1) {
e1.printStackTrace();
}
OknoGlowne.jPanel1 = CrosshairDemo2.run(); // <------------------------------------
OknoGlowne.jTable1.setModel(new javax.swing.table.DefaultTableModel(lista.tab2, lista.naglowki) {Class[] types = {java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Object.class};public Class getColumnClass(int columnIndex) {return types [columnIndex];}});
OknoGlowne.jTabbedPane1.setSelectedIndex(1);
}
}
}
Klasa CrosshairDemo2 wyglada tak:
import java.awt.BorderLayout;
import java.awt.Color;
import java.util.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.Border;
import modules.Modul;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.Minute;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
public class CrosshairDemo2 extends ApplicationFrame {
public static class DemoPanel extends JPanel {
public static int SERIES_COUNT = 2;
public TimeSeriesCollection[] datasets;
public TimeSeries[] series;
public ChartPanel chartPanel;
/**
* Creates a new demo panel.
* @throws SQLException
*/
public DemoPanel() throws SQLException {
super(new BorderLayout());
this.datasets = new TimeSeriesCollection[SERIES_COUNT];
this.series = new TimeSeries[SERIES_COUNT];
JPanel content = new JPanel(new BorderLayout());
JFreeChart chart = createChart();
this.chartPanel = new ChartPanel(chart);
Border border = BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(1, 1, 1, 1),
BorderFactory.createEtchedBorder()
);
this.chartPanel.setBorder(border);
content.add(this.chartPanel);
add(content);
}
/**
* Creates the demo chart.
*
* @return The chart.
* @throws NumberFormatException
* @throws SQLException
*/
public XYDataset createDataset(int index, String name) throws NumberFormatException, SQLException {
this.series[index] = new TimeSeries(name);
Modul o = new Modul();
ResultSet tabela = o.Podaj("select * from "+lista.ajdi()); // <-------
int value=0;
int valueFirst=0;
if (name.equals("wartosc")){
while (tabela.next()){
Object data = tabela.getObject("data");
value = tabela.getInt(name);
this.series[index].add(new Second((Date) data), value);
}
}
else{
int p=0;
while (tabela.next()){
if (p==0) valueFirst=tabela.getInt(name);
else break;
p++;
}
tabela.beforeFirst();
while (tabela.next()){
Object data = tabela.getObject("data");
value = tabela.getInt(name);
this.series[index].add(new Second((Date) data), (value-valueFirst+100));
}
}
this.datasets[index] = new TimeSeriesCollection();
this.datasets[index].addSeries(this.series[index]);
return this.datasets[index];
}
private JFreeChart createChart() throws SQLException {
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Title",
"Time of Day",
"numbers1",
null,
true,
true,
false
);
XYDataset[] datasets = new XYDataset[SERIES_COUNT];
for (int i = 0; i < SERIES_COUNT; i++) {
if (i == 0) {
datasets[i] = createDataset(i, "wartosc1");
chart.getXYPlot().setDataset(datasets[i]);
}
else {
datasets[i] = createDataset(i, "wartosc2");
chart.getXYPlot().setDataset(i, datasets[i]);
chart.getXYPlot().setRangeAxis(1, new NumberAxis("numbers2"));
chart.getXYPlot().mapDatasetToRangeAxis(i, i);
chart.getXYPlot().setRenderer(i, new XYLineAndShapeRenderer(true, false));
}
}
chart.setBackgroundPaint(Color.white);
XYPlot plot = chart.getXYPlot();
plot.setOrientation(PlotOrientation.VERTICAL);
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setAxisOffset(new RectangleInsets(3.0, 3.0, 3.0, 3.0));
plot.setDomainCrosshairVisible(true);
plot.setDomainCrosshairLockedOnData(false);
plot.setRangeCrosshairVisible(false);
XYItemRenderer renderer = plot.getRenderer();
renderer.setPaint(Color.black);
return chart;
}
}
/**
* A demonstration application showing how to...
*
* @param title the frame title.
*/
public CrosshairDemo2(String title) {
super(title);
try {
setContentPane(new DemoPanel());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* @return A panel.
* @throws SQLException
*/
public static JPanel createDemoPanel() throws SQLException {
return new DemoPanel();
}
/**
* Starting point for the demonstration application.
*
* @param args ignored.
* @return
*/
public static JPanel run(){
CrosshairDemo2 demo = new CrosshairDemo2("Crosshair Demo 2");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
return null;
}
}
Próbowałem używać metod repaint() oraz revalidate() ale nie nie dały.
Screen aplikacji: http://img338.imageshack.us/i/appe.jpg/
Byłbym ogromnie wdzięczny za pomoc,
Pozdrawiam.