هنا نحاول أن نرسم جدول عل EditorPane
فى البداية فكرت فى عرض الصفوف فقط من الجدول
و لكن لم اعرف كيف أظهرهم على ال editorPane
معلش استحملونى فى أسئلتى و رجاء التعاون معى فى هذا الأمر
الكود
----------
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainFrame extends JFrame {
private JPanel mainPanel;
private JEditorPane editorPane;
private JButton tableButton;
private DrawTable editorTable;
public MainFrame() {
intializePanel();
initializeEditorPane();
intializeButton();
addComponentOnPanel();
MyActionListener myActionListener = new MyActionListener();
tableButton.addActionListener(myActionListener);
editorTable = new DrawTable();
}
public static void main(String[] args) {
MainFrame tableTest = new MainFrame();
tableTest.setDefaultCloseOperation(MainFrame.EXIT_ON_CLOSE);
tableTest.setSize(new Dimension(600, 300));
tableTest.setLocationRelativeTo(null);
tableTest.setVisible(true);
tableTest.setExtendedState(MAXIMIZED_BOTH);
}
private void initializeEditorPane() {
editorPane = new JEditorPane();
}
private void intializePanel() {
mainPanel = new JPanel(new BorderLayout());
}
private void intializeButton() {
tableButton = new JButton("Draw table");
}
private void addComponentOnPanel() {
mainPanel.add(tableButton, BorderLayout.NORTH);
mainPanel.add(editorPane, BorderLayout.CENTER);
getContentPane().add(mainPanel);
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == tableButton) {
MainDilog drawTable = new MainDilog();
drawTable.setVisible(true);
drawTable.setSize(new Dimension(300, 150));
drawTable.setLocationRelativeTo(null);
}
}
}
}الclass mainDiloag
--------------------
import javax.swing.Box;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
class MainDilog extends JDialog {
private Box mainPanel;
private Box rowBox, colBox;
private JLabel row, col;
private JTextField rowField, columField;
public MainDilog() {
intializePanel();
intializeBox();
intializeLabels();
intializeFields();
addComponentOnPanel();
}
private void intializeLabels() {
row = new JLabel("Insert row:");
col = new JLabel("Insert colum:");
}
private void intializeFields() {
rowField = new JTextField(10);
columField = new JTextField(10);
}
private void intializePanel() {
mainPanel = Box.createVerticalBox();
}
private void addComponentOnPanel() {
mainPanel.add(rowBox);
Box.createVerticalStrut(1);
Box.createVerticalGlue();
mainPanel.add(colBox);
Box.createVerticalGlue();
getContentPane().add(mainPanel);
}
private void intializeBox() {
intializeLabels();
intializeFields();
rowBox = Box.createHorizontalBox();
rowBox.add(row);
Box.createHorizontalStrut(1);
rowBox.add(rowField);
Box.createHorizontalGlue();
colBox = Box.createHorizontalBox();
colBox.add(col);
Box.createHorizontalStrut(1);
colBox.add(columField);
Box.createHorizontalGlue();
}
public String getRowField() {
return rowField.toString();
}
}ال class DrawTable
---------------------
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JEditorPane;
class DrawTable extends JEditorPane {
int xStart = 100;
int xEnd = 500;
int yStart = 150;
public DrawTable() {
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (int i = 0; i < Integer.parseInt(new MainDilog().getRowField()); ++i) {
g2.drawLine(xStart, (yStart + xStart) * i, xEnd, (yStart + xStart) * i);
}
}
}رجاء التعاوون معى فى ضبط خاصية ال table
شكراً