フォームにテーブルを追加し、特定のレイアウトに配置するアプリを作成しています。フォームに新しいテーブルを追加する必要があるたびに、MultiSplitPaneで新しいリーフを作成し、新しいテーブルをリーフに追加しています。仕切りを移動してから、内部にテーブルがある新しいリーフを追加しようとするまで、すべてが正常に機能します。
誰かがこの混乱の原因と私がそれを修正する方法を知っていますか?
コピーできるSSCCEは次のとおりです。
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.GraphicsConfiguration;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import org.jdesktop.swingx.MultiSplitLayout;
import org.jdesktop.swingx.MultiSplitPane;
/**
* @author Igor
*/
public class Example extends javax.swing.JFrame {
/**
* Creates new form Example
*/
@Override
public void add(Component cmpnt, Object o) {
super.add(cmpnt, o);
}
@Override
public void add(PopupMenu pm) {
super.add(pm);
}
@Override
public boolean action(Event event, Object o) {
return super.action(event, o);
}
public Example(GraphicsConfiguration gc) {
super(gc);
}
@Override
public Component add(Component cmpnt, int i) {
return super.add(cmpnt, i);
}
@Override
public Component add(Component cmpnt) {
return super.add(cmpnt);
}
// Initialize a MultiSplitPane
MultiSplitPane multiSplitPane = new MultiSplitPane();
int boxCount = 0;
String left = " (LEAF name=left0 weight=1)";
String right = "(LEAF name=right0 weight=0)";
String layoutDef = "(ROW" + left + " " + right + ")";
MultiSplitLayout.Node modelRoot = MultiSplitLayout.parseModel(layoutDef);
public Example() {
initComponents();
multiSplitPane.getMultiSplitLayout().setModel(modelRoot);
setLayout(new BorderLayout());
add(new JScrollPane(multiSplitPane), BorderLayout.CENTER);
add(new JButton("Go") {
{
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Example.this.addNode();
}
});
}
}, BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
}
//a function that adds a new Leaf to the multiSplitPane
private void addNode() {
if (boxCount == 1) {
left = " (LEAF name=left0 weight=0.5)";
right = " (LEAF name=right0 weight=0.5)";
} else if (boxCount == 2) {
left = "(COLUMN weight=0.5 left0 left" + new Integer(boxCount - 1).toString() + ")";
} else if (boxCount == 3) {
right = "(COLUMN weight=0.5 right0 right" + new Integer(boxCount - 2).toString() + ")";
} else if ((boxCount % 2 == 0) && (boxCount != 0)) {
left = left.substring(0, left.length() - 1);
left += " left" + new Integer(boxCount / 2).toString() + ")";
} else if ((boxCount % 2 != 0)) {
right = right.substring(0, right.length() - 1);
right += " right" + new Integer(boxCount / 2).toString() + ")";
}
String layoutDef = "(ROW " + left + " " + right + ")";
MultiSplitLayout.Node modelRoot = MultiSplitLayout.parseModel(layoutDef);
multiSplitPane.getMultiSplitLayout().setModel(modelRoot);
JTable t = new JTable(1, 1);
t.getColumnModel().getColumn(0).setHeaderValue("Box Title" + boxCount);
Dimension dimension = new Dimension(190, 190);
t.setPreferredScrollableViewportSize(dimension);
t.setRowHeight(800);
if (boxCount == 0) {
multiSplitPane.add(new JScrollPane(t), "left0");
} else if (boxCount == 1) {
multiSplitPane.add(new JScrollPane(t), "right0");
} else if (boxCount == 2) {
multiSplitPane.add(new JScrollPane(t), "left1");
} else if (boxCount == 3) {
multiSplitPane.add(new JScrollPane(t), "right1");
} else if (boxCount % 2 == 0) {
multiSplitPane.add(new JScrollPane(t), "left" + new Integer(boxCount / 2).toString());
} else if (boxCount % 2 != 0) {
multiSplitPane.add(new JScrollPane(t), "right" + new Integer(boxCount / 2).toString());
}
multiSplitPane.revalidate();
boxCount++;
}// End of AddNode *****************
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Example().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
PSアプリを実行するときにウィンドウサイズを大きくする(または最大化する)。