Skip to main content

Posts

Showing posts with the label swing

BoxLayout can"t be shared error

I have this Java JFrame class, in which I want to use a boxlayout, but I get an error saying java.awt.AWTError: BoxLayout can't be shared . I've seen others with this problem, but they solved it by creating the boxlayout on the contentpane, but that is what I'm doing here. Here's my code: class edit_dialog extends javax.swing.JFrame{ javax.swing.JTextField title = new javax.swing.JTextField(); public edit_dialog(){ setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE); setTitle("New entity"); getContentPane().setLayout( new javax.swing.BoxLayout(this, javax.swing.BoxLayout.PAGE_AXIS)); add(title); pack(); setVisible(true); } }

Why this null pointer exception when using BufferStrategy with swing timer in Java?

Why might I be getting the following exception with the below code? Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at java.awt.Component$BltBufferStrategy.showSubRegion(Unknown Source) at java.awt.Component$BltSubRegionBufferStrategy.show(Unknown Source) at javax.swing.BufferStrategyPaintManager.flushAccumulatedRegion(Unknown Source) ... It only happens about every other time I run it but always right at the start. I'm using if(bs.contentLost()){...} so I don't understand why it would be having problems. import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; import java.util.Random; public class MLM2 extends JFrame implements ActionListener { private final Timer timer = new Timer(20, this); private Insets insets; private BufferStrategy bs; private BufferedImage drawing; int lastW; int lastH; int pos = 0; public static void main (String[] args) { MLM2 ex = new MLM2()

Why is getSize() not working for me here and why the flicker when resizing?

This is my first attempt at using BufferStrategy and I'd really appreciate some hints. 1) Why, in the below code, does getSize() return dimensions of 0 until you have resized the window? How can I detect the size of the window right away? 2) Why when getSize() is returning something is it not the full dimensions of the window? IE why is there a blackless strip to the bottom and right? 3) Is there a way to get rid of the flicker when you resize the window? import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferStrategy; import javax.swing.JFrame; import javax.swing.JPanel; public class BSTest extends JFrame { BufferStrategy bs; DrawPanel panel = new DrawPanel(); public BSTest() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(800,420); setLocationRelativeTo(null); setIgnoreRepaint(true); setVisible(true); createBufferStrategy(2); bs = getBufferStrategy(); panel.set

How to make a JPanel inside a JFrame fill the whole window?

In the below example, how can I get the JPanel to take up all of the JFrame? I set the preferred size to 800x420 but it only actually fills 792x391. import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.image.BufferStrategy; import javax.swing.JFrame; import javax.swing.JPanel; public class BSTest extends JFrame { BufferStrategy bs; DrawPanel panel = new DrawPanel(); public BSTest() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); // edited line setVisible(true); setSize(800,420); setLocationRelativeTo(null); setIgnoreRepaint(true); createBufferStrategy(2); bs = getBufferStrategy(); panel.setIgnoreRepaint(true); panel.setPreferredSize(new Dimension(800,420)); add(panel, BorderLayout.CENTER); // edited line panel.drawStuff(); } public class DrawPanel extends JPanel { public void drawStu

Java Jframe centers my buttons ):<

Here is my code: public class Main { public static void main(String[] args){ JFrame frame = new JFrame("Vex Development Studio 2.0"); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(10,10); //make variables File newproject; Container content = frame.getContentPane(); GridBagConstraints gbc = new GridBagConstraints (); Dimension buttonsize = new Dimension(75,25); Button about; about = new Button("About"); about.setPreferredSize(buttonsize); //add content content.setLayout(new GridBagLayout()); content.setBackground(Color.white); gbc.gridx = 0; gbc.gridy = 0; content.add(about,gbc); //main stuff //about button about.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ JOptionPane.showMessageDialog(null, "Example", "About", 1); } }); //some extra crap frame.setSize(700, 500

How do I make a GUI that looks like the attached picture of a simple stock market app?

Using these layouts; FlowLayout, BoxLayout, GridLayout, BorderLayout and GridBagLayout im trying to make my GUI look like below. I tried Flow, Grid and Border but I couldn't make it look the same.

JTextPane wrap long words

For some reason JTextPane only warp text if there are spaces between words, consider the following example: package textpane; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextPane; public class TextPaneLongLine { public static void main(String[] args) { final JFrame frame = new JFrame("TextPane"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 200); JTextPane textPane = new JTextPane(); JScrollPane scollPane = new JScrollPane(textPane); scollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); frame.add(scollPane); frame.setVisible(true); } } As you can see, if a long line without spaces is typed it will go of the view area. But if the line have spaces JTextPane will automatic split the line to fit the view area. Replacing the JTextPane with JEditorPane will work as

updated gui application

I need to write an application that will check for something every X time and update it to GUI comtols. I have some little knowledge in java and written some applications before but I've never dealt with stuff like multiple threads and updating the GUI. I though it will be an easy task so I've started to write my software. Everything else is pretty much functioning. I just cant get the app to perform checks every X time and then update the GUI. Can someone please give some guidance on this? What shold I look into? what is the common practice? Is it threads? recursive? anything else? Also if possible provide me some code examples, I can understand better. thanks for any effort!

JGoodies is blocking my action events from firing

I added an actionListener to my JTextField. The JTextField is creating by JGoodies and is buffered. If I change the value in my JTextField and hit enter on it, it fires my event. If I change the value, lose focus, come back and then hit enter, no event fires. Has this happened to anybody else? Any suggestions would be welcome, I am really stuck... ValueModel valueModel = getValueModel(adapter, propertyName, useBuffer); JTextField textField = BasicComponentFactory.createLongField(valueModel); I add an actionListener to it. It works if I change the value and then hit enter. If I change the value, lose focus, and then regain focus and hit enter, the action does not fire, which is unexpected.

Swing high-performance charting library

I'm currently looking for a Swing component that will allow me to draw about 5000 points per sequence for at least 3 sequences with an update frequency greater or equal to 100 Hz (and preferably pretty looks). A clean API is definitely a plus and another plus is the possibility of having two (or more) vertical axes. The targets are java 6 and 7. Solutions which are not 100% java should support at least Windows 7 and Mac OS X Lion. Commercial and F/OSS solutions equally welcome. Thanks in advance

How to set Default opening window in Swing?

I have an application that has two different login windows for two different types of users of application, say one is client and the other is administrator , now i want to have an option on both the Login windows to set the default opening window, something like a checkbox having label as set this is as your default Login window .. How can i achieve this?? May i use a .properties file to set the default window? (and How?) Or, a Main.java class which loads first and decides the default GUI window to load first?? I preferred the 2nd way...but your views are welcomed, also please provide some example codes to accomplish this task.

KeyListener does not respond if there is any input in JTextField

I develop a simple MVC Calculator app. I decided to add some functionality by implementing KeyListener in CalculatorView. But this KeyListener only responds when there is no input in JTextField (before any input was made by pushing GUI buttons) or it responds when I press "ESC". I know some people here advice to use KeyBindings instead of KeyListener, but then I need to have 12 KeyBindings in my code (10 for numbers, 1 for ESC and 1 for "." character). Is there any way to make KeyListener to work properly in my app? And here is the code: /** * * @author Kate Nezdoly */ public class CalculatorView implements ActionListener, KeyListener { private JButton[] operButtons = new JButton[13]; private JButton[] numberButtons = new JButton[12]; private String[] operators = {"C", "(", ")", "+", "-", "*", "/", "^", "cos", "sin", "tan", "sqrt"}; pr

How can i make an event to a JComboBox which triggers AFTER selection?

I want to make an event which will be triggered after i make the selection to the JComboBox. the problem I'm now facing is that when i added an ActionListener, it was triggered when the user clicked on the box but BEFORE he actually chose the new item, thus the action listener was activated all the time on the previous value which was selected in the box. what i want to do is simply changing the title of an JTextArea according to the selection. I tried doing something like this: jBox.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { String alt = GetAlgoAreaTitleByChoice(); panel.remove(jArea); currentBest = setArea("",alt); currentBest.setBounds(50, 350, 1000, 290); panel.add(jArea); } }); and the method inside: private String GetArgsAreaTitleByChoice(){ String chi = jBox.getSelectedItem().toString(); if(chi.equals(generalChoice)){ return "Hello"; } else if

how get Image icon in JTable

i have created JTable. i get the value from DB to JTable . i am having problem while click on row it show the image icon path. cannot show the image. how i can set the image. one more thing i am having two images 1. Pointer image 2. Blank Image At the run time Pointer image in a first row by defaulty, After that i clicked in the 5th row, the first row should set with Blank Image, and the 5th rows should be set pointer image. and i don't know its possible in DefaultCellRender. package ex10012012; import java.awt.*; import javax.swing.*; import javax.swing.table.*; import java.sql.*; public class frame10012012 extends javax.swing.JFrame { private DefaultTableModel TmpFlxMdl; private TableColumn TmpFlxColumn; private int TmpRow,TmpFlxDRow; private TableColumn TmpFlxCol; private Statement TmpFlxSt=null; private ResultSet TmpFlxDRs=null; GENLMODL TmpGenModl= new GENLMODL(); FrmM01011 TmpFrmM0101 = new FrmM01011(); public frame10012012() { initCompo