Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
511 views
in Technique[技术] by (71.8m points)

java - Can we put combobox, radio button, text field, table on text area?

I need this type of functionality. Like in MS Word, we choose table in menu bar and then we draw in our sheet.

So how it can be done in Java? I thought for sheet I can use JTextArea.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No (to a JTextArea), but you should be able to do it using a JTextPane, see JTextPane#insertComponent for more details

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            JTextPane tp = new JTextPane();

            Document doc = tp.getDocument();

            try {
                tp.insertComponent(new JTextField("Hello world"));
                doc.insertString(doc.getLength(), "
", null);
                tp.insertComponent(new JComboBox(new String[]{"Banana", "Apple", "Grape"}));
                doc.insertString(doc.getLength(), "
", null);
                tp.insertComponent(new JRadioButton("Option A"));
                doc.insertString(doc.getLength(), "
", null);
                tp.insertComponent(new JTable(new DefaultTableModel(10, 5)));
                doc.insertString(doc.getLength(), "
", null);
            } catch (BadLocationException exp) {
                exp.printStackTrace();
            }

            add(new JScrollPane(tp));
        }

    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...