プロが教える店舗&オフィスのセキュリティ対策術

TextPaneElement1
TextPaneElement2
TextPaneElement3



TextPaneElement○

○は数字で、文字にキャストして宣言したいです。

一つ一つ宣言するのは面倒だし、メモリーを食いそうなので、動的に生成したいです。
最初は1個、テキストボックスが有って、+ボタンで増えて、-ボタンで減らす処理です。

____________________________________________________________________

【実行時エラーメッセージ】java.lang.ClassNotFoundException: PreFrame$InputSupportForm_RandomTag_For_PC$TextPaneElement1

____________________________________________________________________
public class InputSupportForm_RandomTag_For_PC extends Frame implements ActionListener ,KeyListener {
 /****/
private static final long serialVersionUID = 1L;

 Button btnDecrease = new Button("-");
 Button btnIncrease = new Button("+");


public void InsertData(String TargetFormName,int TargetFormSEQNUM,String TargetTextPaneName,int LastCaret, String BOT_DIC) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
 //右クリックで呼び出されたカーソル位置を覚えていて、ランダムな項目の設定した分、フォーマットを調整して、LastCaretの位置に挿入する

 ・・・
}


public JTextPane SetTextPane(int TargetFormSEQNUM,int TargetElementSEQNUM,int LastCaret) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
 TeraBotMain MainClass = new TeraBotMain();

 String TargetObject = ("PreFrame$InputSupportForm_RandomTag_For_PC$TextPaneElement" + String.valueOf(MainClass.TargetElementSEQNUM));
 Class<?> cls;
 try {
  cls = Class.forName(TargetObject); //【エラー箇所】
  JTextPane clsJTextPane = (JTextPane)cls.newInstance();

  TargetObject = ("PreFrame$InputSupportForm_RandomTag_For_PC$TextPaneScrollElement" + String.valueOf(MainClass.TargetElementSEQNUM));
  Class<?> cls2 = Class.forName(TargetObject);
  JScrollPane clsScrollPane = (JScrollPane)cls2.newInstance();

  clsScrollPane.setPreferredSize(new Dimension(120, 50));
  clsScrollPane.setMinimumSize(new Dimension(120, 50));


  clsJTextPane.setBorder(
  BorderFactory.createCompoundBorder(
  BorderFactory.createTitledBorder(""),
  BorderFactory.createEmptyBorder(5,5,5,5)));

  clsScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);clsScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);



  if (null != cls) {
   JPanel pn5 = new JPanel();
   pn5.setLayout(new FlowLayout());

  //+を押すとint TargetElementSEQNUMを1増やし、TextPaneを一つ増やす
  //-を押すとint TargetElementSEQNUMを1減らし、TextPane内の文字をクリアして、setVisible(false)
   pn5.add(clsScrollPane);
   //Button btnDecrease = new Button("-");
   pn5.add(btnDecrease);
   //Button btnIncrease = new Button("+");
   pn5.add(btnIncrease);

   Container contentPane = InputSupportForm_RandomTag.getContentPane();
   InputSupportForm_RandomTag.setVisible(true);

   InputSupportForm_RandomTag.add(pn5, BorderLayout.CENTER);

   return clsJTextPane;
  }
 } catch (ClassNotFoundException e1) {
  // TODO 自動生成された catch ブロック
  e1.printStackTrace();
 }
 return null;
}

InputSupportForm_RandomTag_For_PC(){
・・・

 @Override
 public void actionPerformed(ActionEvent e) {
  // TODO 自動生成されたメソッド・スタブ

  //if (e.getActionCommand()=="+"){
  if (e.getSource() == btnIncrease) {
   TargetElementSEQNUM++;
  }

  if (e.getSource() == btnDecrease) {
   TargetElementSEQNUM--;
  }

  SetTextPane(TargetFormSEQNUM,TargetElementSEQNUM,LastCaret)
 }
}

____________________________________________________________________

困ってます、どなたかご教授下さい。また、そんな簡潔に記述する事はできない なら、
次善の策をご教授下さい。

「連番をオブジェクト名の最後につけて、その」の質問画像

質問者からの補足コメント

  • 【実行時エラー】
    GroupLayout can only be used with one Container at a time

    【訂正前】
    GroupLayout layout = new GroupLayout(this);
    setLayout(layout);

    【訂正後】
    // http://kelvinh.github.io/blog/2010/06/09/swing-g …
    GroupLayout layout = new GroupLayout(getContentPane());
    getContentPane().setLayout(layout);

    有難う御座います!動的に生成する事ができました。しかし画面からはみ出た場合に、スクロールバーはどうやって設定すれば?難しいのなら、参考ページを教えてくれると嬉しいです。

    「連番をオブジェクト名の最後につけて、その」の補足画像1
    No.2の回答に寄せられた補足コメントです。 補足日時:2015/04/25 13:48
  • アドバイスありがとうございます。無謀な事をしなくて済みました(^^ゞ 思った通りの画面作れました!(遅くなってスミマセン。参考までにスクリーンショット付けます。)

    「連番をオブジェクト名の最後につけて、その」の補足画像2
    No.3の回答に寄せられた補足コメントです。 補足日時:2015/04/28 14:27

A 回答 (3件)

List<JTextPane> textPaneList = new ArrayList<JTextPane>();


のようにリスト型で保持しておくと動的にインスタンスを追加できますよ。

「+」ボタンが押されたときに
JTextPane p = new JTextPane();
textPaneList.add(p); // 動的に追加
    • good
    • 1
この回答へのお礼

貴重なご回答有難うございます!現在はテキストボックスをフレームに追加して、再描画したいのですが(表示位置を変えたり、再描画の様な命令を書いてみましたが)、うまく行かないのと「+」「-」のボタンを常に一番右(右下)に配置しておきたいので、テキストボックスを全て削除してから、1個ずつ追加できません。画面上は変化なしです。

if (e.getSource() == btnIncrease) {

 System.out.println("+ボタン 要素数=" + scrollPaneList.size() + "個");

 JTextPane p = new JTextPane();
 textPaneList.add(p);

 p.setBorder(
 BorderFactory.createCompoundBorder(
 BorderFactory.createTitledBorder(""),
 BorderFactory.createEmptyBorder(5,5,5,5)));


 JScrollPane q = new JScrollPane(p);
 scrollPaneList.add(q);

 q.setPreferredSize(new Dimension(120, 50));
 q.setMinimumSize(同上);

 q.setBorder(同上);

 q.setVerticalScrollBarPolicy(省略);

 JPanel pn5 = new JPanel();
 pn5.setLayout(new FlowLayout());

 remove(btnDecrease);
 remove(btnIncrease);

 pn5.add(q);
 //for (int i = 0 ; i < scrollPaneList.size() ; i++){
 // pn5.add(scrollPaneList.get(i));
 //}

 pn5.add(btnDecrease);
 pn5.add(btnIncrease);

 add(pn5, BorderLayout.CENTER);
 //repaint();
 //pn5.updateUI();
 invalidate();
 validate();
}

お礼日時:2015/04/24 17:01

サンプル的に作ってみました。

細かいチェックはしていないのでエラーも発生します。

ポイントとしては
・JTextPane だけを束ねる JPanel (下記ではButtonsPanel)を用意して管理する
・コンポーネント追加・削除した場合は revalidate() で再描画する
でしょうか。

----------------------
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextPane;

public class ButtonsDialog extends JDialog {

public static void main(String[] args) throws Throwable {
new ButtonsDialog().setVisible(true);
}

public ButtonsDialog() {
initDialog();
getContentPane().add(new SamplePanel());
}

protected void initDialog() {
setBounds(300, 300, 800, 200);
}

protected class SamplePanel extends JPanel {

private JButton incButton;
private JButton decButton;
private ButtonsPanel panel;

public SamplePanel() {
panel = new ButtonsPanel();
incButton = new JButton("+");
incButton.addActionListener((e) -> {
panel.addTextPane();
this.revalidate();
});
decButton = new JButton("-");
decButton.addActionListener((e) -> {
panel.delTextPane();
this.revalidate();
});
initLayout();
}

protected void initLayout() {

GroupLayout layout = new GroupLayout(this);
setLayout(layout);

// 自動的にコンポーネント間のすき間をあけるようにする
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);

// 水平方向のグループ
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
hGroup.addGroup(layout.createParallelGroup()
.addComponent(incButton).addComponent(decButton).addComponent(panel));
// レイアウト・マネージャに登録
layout.setHorizontalGroup(hGroup);

// 垂直方向のグループ
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
vGroup.addGroup(layout.createParallelGroup().addComponent(incButton));
vGroup.addGroup(layout.createParallelGroup().addComponent(decButton));
vGroup.addGroup(layout.createParallelGroup().addComponent(panel));

// レイアウト・マネージャに登録
layout.setVerticalGroup(vGroup);
}
}

private static class ButtonsPanel extends JPanel {

private List<JTextPane> textPanes;

public ButtonsPanel() {
this.textPanes = new ArrayList<>();
}

public void addTextPane() {
JTextPane p = createTextPane();
textPanes.add(p);
this.add(p);
}

public void delTextPane() {
JTextPane removedPane = textPanes.remove(textPanes.size() - 1);
this.remove(removedPane);
}

private JTextPane createTextPane() {
JTextPane p = new JTextPane();
p.setPreferredSize(new Dimension(100, 30));
return p;
}
}
}
この回答への補足あり
    • good
    • 0
この回答へのお礼

動的にテキストボックスが生成できただけでも、大助かりなので、有難うございました!リストを使うとは思いつきませんでした。スクロールバーですが、スクロールバーを配置し、自分でテキストボックスの座標を調整したするようにすれば、面倒ですが出来そうです。もっと簡単な方法があれば思って補足しました。サンプルは他にも修正しましたが、文字数の都合上載せられません。もう少ししたら、締め切ろうかと思います。

お礼日時:2015/04/26 21:50

試してないですが、今回作ったJTextPaneを載せたJPanelに対して、JScrollPane を設定すればスクロールできると思いますよ。


座標位置をズラすなどはやめたほうがよさそうですね。
この回答への補足あり
    • good
    • 0
この回答へのお礼

ありがとうございました!

お礼日時:2015/04/28 14:38

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!