Java JTextArea how to apply syntax highlighting - java

I am trying to make a simple code editor with the JFrame library in Java and I've got all the base things working except for syntax highlighting. I've read through forums and forums but I am not able to find anything. So my question is how to change the color of certain words
An example
TextEditor(){
JTextArea textArea;
JScrollPane scrollPane;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("foo");
this.setSize(1000,1000);
this.setLayout(new FlowLayout());
this.setLocationRelativeTo(null);
textArea=new JTextArea();
textArea=new JTextArea();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setFont(new Font("Times New Roman",Font.PLAIN,20));
scrollPane=new JScrollPane(textArea);
scrollPane=new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(950,950));
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
}

Hey im making a IDE and i can help you :p.
Replace JTextArea by JTextPane : JTextPane can be Stylised.
For the syntax hightlight you can use the StyledDocument.

Related

How to make frame automatically resize with window?

When I resize the window, the scroll frame doesnt resize with it and just goes out of frame.
Is there any way to fix this? Here's a snippet of the code:
TextEditor(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Emu Editor J");
this.setSize(700,550);
this.setResizable(true);
this.setLayout(new FlowLayout());
this.setLocationRelativeTo(null);
textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setFont(new Font("Cascadia Code",Font.PLAIN,20));
scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(this.getWidth()-25,this.getHeight()-100));
// scrollPane.setSize(this.getWidth()-25,this.getHeight()-100);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
I tried allowing resizable and it didn't work
Try using BorderLayout and add the panel to it with BorderLayout.CENTER.

GridBagLayout allignement

Iam trying to find out how GridBagLayout works,because i have had never used it. I will post source code and picture of that gui. i have problem with components to allign. i want to put button to be in the middle of two components(on one side checkboxgroup and on the other is JTextArea. and i managed but it is alligned in the bottom and i want to allign it to be in the middle of the height of both components.
public class GUI extends JFrame {
public GUI(){
setSize(600,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("ridBagLayout[![enter image description here][1]][1]");
JPanel panel=new JPanel(new GridBagLayout());
GridBagConstraints c=new GridBagConstraints();
String[]niz={"C Sharp","Java","PHP","VisualBasic"};
c.anchor=GridBagConstraints.PAGE_START;
c.weighty=1.0;
c.gridx=0;
c.gridy=0;
c.insets=new Insets(30,30,0,0);
panel.add(new JComboBox(niz),c);
c.gridx=1;
//c.insets=new Insets(10,0,0,0);
panel.add(new JButton("Get drop down item"),c);
c.gridx=2;
c.ipadx=20;
panel.add(new JTextField(15),c);
JCheckBox visualBasic=new JCheckBox("Visual Basic");
JCheckBox cSharp=new JCheckBox("C Sharp");
JCheckBox java=new JCheckBox("Java");
JCheckBox php=new JCheckBox("PHP");
JPanel checkbox=new JPanel();
checkbox.setLayout(new GridLayout(4,0,5,5));
checkbox.add(visualBasic);
checkbox.add(cSharp);
checkbox.add(java);
checkbox.add(php);
javax.swing.border.Border raisedetched = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
checkbox.setBorder(raisedetched);
c.insets=new Insets(0,20,0,0);
c.gridx=0;
c.gridy=1;
panel.add(checkbox,c);
c.gridx=1;
c.ipadx=30;
c.anchor=GridBagConstraints.CENTER;
panel.add(new JButton("Selected Item"),c);
c.gridx=2;
c.gridy=1;
c.ipadx=20;
c.anchor=GridBagConstraints.NORTH;
panel.add(new JTextArea(7,15),c);
add(panel);
setVisible(true);
}
}
GridBagLayout is one of the most powerful layouts to use in Java, but it requires some nesting in order to obtain a good result.
In your case, maybe you need to put that button inside a panel and set its alignments properties to CENTER.
There is lot of documentation about GridBagLayout but another way to see how it works could be to create a new project in an IDE like NetBeans and create that GUI through its graphical editor and see the generated code.

Why JTextArea doesn't work with JPanel?

Why when I add JTextArea to JPanel it doesn't work? When I use JButton instead of JTextArea everything works corectly. Why doesn't JTextArea work with JPanel but with JFrame does?
public class Searching extends JPanel {
private JPanel searchPanel;
private JTextArea addMedicament;
public Searching(){
searchPanel = new JPanel();
searchPanel.setLayout(new GridLayout(1,1));
setBackground(Color.BLUE);
addMedicament = new JTextArea();
searchPanel.add(addMedicament);
this.add(searchPanel);
}
}
A text area will work fine with a panel.
Try creating the text area as follows:
JTextArea textArea = new JTextArea(5, 20);
JScrollPane = new JScrollPane( textArea );
panel.add( scrollPane );
Now the text area will be created with a preferred size. As the data is changed scrollbars will appear/disappear as required because the problem is with your code and the context of how you use your code, not the panel or text area.
If this doesn't help then post a proper SSCCE that demonstrates the problem.

Extra column with GridLayout and JScrollPane not showing - Java Swing

I am messing around with Swing and I find that some elements are not appearing in my application. Most notably the JScrollPane and for some reason there seems to be a grid column between my JTextArea and JButtons.
See the image below:
My method is as follows:
private void panels(){
JFrame frame=new JFrame("Viewing All Program Details");
JPanel panel = new JPanel(new GridLayout(1,1));
panel.setBorder(new EmptyBorder(5, 5, 5, 5));
JPanel rightPanel = new JPanel(new GridLayout(15,0,10,10));
rightPanel.setBorder(new EmptyBorder(15, 5, 5, 10));
JTextArea textArea = new JTextArea(storeAllString,0,70);
JScrollPane scrollBarForTextArea=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(textArea);
frame.add(scrollBarForTextArea);
frame.getContentPane().add(panel,BorderLayout.LINE_START);
frame.getContentPane().add(rightPanel,BorderLayout.EAST);
frame.setSize(1000, 700);
frame.setVisible(true);
rightPanel.add(saveCloseBtn);
rightPanel.add(closeButton);
}
Let's start with the obvious...
JTextArea textArea = new JTextArea(storeAllString,0,70);
JScrollPane scrollBarForTextArea=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(textArea);
This isn't how you use a JScrollPane, essentially what you've done is removed the textArea from the JScrollPane by adding it to the panel. A component can only reside within a single parent container at a time.
Instead, you should be adding the scroll pane to the panel...
JTextArea textArea = new JTextArea(storeAllString,0,70);
JScrollPane scrollBarForTextArea=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scrollBarForTextArea);
The "extra" column is actually a sympton of the previous problem...
panel.add(textArea);
frame.add(scrollBarForTextArea); // This is the "extra" column
frame.getContentPane().add(panel,BorderLayout.LINE_START);
frame.getContentPane().add(rightPanel,BorderLayout.EAST);
The empty JScrollPane is what you are seeing. With the fixes from before, you only need to do...
frame.add(panel);
frame.getContentPane().add(rightPanel,BorderLayout.EAST);
You should, also, call setVisible after you've finished building the UI if possible

ScrollPane in the JTextArea

This is part of GUI project I'm working with and I'm trying to make JScrollPane to appear in the JTextArea when the text is longer than the size of the JTextArea. It looks fine to me but the JScrollPane still doesn't show up.
JTextArea textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setBounds(77, 27, 561, 146);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(380, 100));
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel panel= new JPanel()
panel.add(textArea);
Can anyone verify this peace of code?
The reason your JScrollPane isn't showing up is because you haven't added it to your GUI...
panel.add(textArea);
should be
panel.add(scrollPane);
Why one might ask? Because in this line:
JScrollPane scrollPane = new JScrollPane(textArea); we see that the JScrollPane's constructor takes in the JTextArea/etc thus removing any need to add the textArea to the GUI because the textArea is now part of the scrollPane which, in turn, should be added to the GUI.

Categories

Resources